简体   繁体   English

如何在Django中签署sha1签名?

[英]how do I sign sha1 signature in Django?

I have not really dug in deep into django and signatures 我还没有真正深入研究Django和签名

I am trying to create a signature based on a given requirements 我正在尝试根据给定的要求创建签名

i = my username, v = expiration, and s = signature i =我的用户名,v =到期,s =签名

i: MYUSERNAME 我: MYUSERNAME
v: UNIX TIMESTAMP + 2 hours v: UNIX TIMESTAMP + 2小时
a: MY_API_KEY 一个: MY_API_KEY
s: SHA1 SIGNATURE that is ("i=MYUSERNAME&v=(UNIX_TIMESTAMP + API_KEY)") s: SHA1签名,即(“ i = MYUSERNAME&v =(UNIX_TIMESTAMP + API_KEY)”)

What I have so far. 到目前为止我所拥有的。

import hashlib
import datetime
from datetime import timedelta
import time

now = datetime.datetime.now()
now = now + timedelta(hours=2)
seconds = time.mktime(now.timetuple())
seconds = seconds
API = "87df234207v4444444"
signature = hashlib.sha1()
signature.update("i=MYUSERNAME&v=%s%s" % (seconds, API))

Then when I ping the url, I get a "Bad Signature" response 然后,当我ping通URL时,会收到“错误签名”响应

Again my knowledge in this area is limited, your help is much appreciated 同样,我在这方面的知识有限,非常感谢您的帮助

and the valid iframe to get the correct code is http://api.myurl.com/i=MYUSERNAME&v=UNIX TIMESTAMP + 2 HOURS&s=MY VALID SIGNATURE 而获得正确代码的有效iframe是http://api.myurl.com/i=MYUSERNAME&v=UNIX TIMESTAMP + 2 HOURS&s=MY VALID SIGNATURE

PDF Documentation PDF文件

Imagine that we need to calculate the signature for the following URI: 假设我们需要计算以下URI的签名:

http://framed.incloode.com/index.php?i=TEST&arg1=val1&arg2=val2&t=123 http://framed.incloode.com/index.php?i=TEST&arg1=val1&arg2=val2&t=123
The method to calculate the signature is as follows: 计算签名的方法如下:

  • Go through the full list of parameters and the values, 浏览完整的参数和值列表,
  • Append these into one long string HTML safestring,like a normal GET request, 将它们附加到一个长字符串HTML安全字符串中,例如普通的GET请求,
  • For the above URI this long string would end-up looking like this: i=TEST&arg1=val1&arg2=val2&t=123 对于上面的URI,这个长字符串最终看起来像这样:i = TEST&arg1 = val1&arg2 = val2&t = 123
  • To specify until when this link should be valid, a validity must be added. 要指定此链接何时有效,必须添加有效性。 The validity is a UNIX timestamp – for a validity of 2 hours, the value time() + 7200 should be passed. 有效期为UNIX时间戳–有效期为2小时,应传递值time()+ 7200。 This validity (parameter and value) must also appended to the string to sign. 此有效性(参数和值)也必须附加到字符串后才能签名。 For the above URI this would now look like this: i=TEST&arg1=val1&arg2=val2&t=123&v=1444567890 The requests in the initial URI should be replaced by this list. 对于上面的URI,现在看起来像这样:i = TEST&arg1 = val1&arg2 = val2&t = 123&v = 1444567890初始URI中的请求应替换为该列表。
  • The above collected string is then decoded from HTML(htmlspecialchars_decode()) so one ends up with a string looking like this: i=TEST&arg1=val1&arg2=val2&t=123&v=1444567890 上面收集的字符串然后从HTML(htmlspecialchars_decode())进行解码,因此最终得到的字符串如下所示:i = TEST&arg1 = val1&arg2 = val2&t = 123&v = 1444567890
  • The “INCLOODE_API_SECRET” that you have been supplied with must be appended to this string: i=TEST&arg1=val1&arg2=val2&t=123&v=1444567890**INCLOODE_API_SECRET** 您提供的“ INCLOODE_API_SECRET”必须附加到此字符串:i = TEST&arg1 = val1&arg2 = val2&t = 123&v = 1444567890 ** INCLOODE_API_SECRET **
  • Then,finallytheSHA1checksumofthatstringmustbegenerated.Then,itis appended to the parameter/value list retrieved at step (3). 然后,最后必须生成该字符串的SHA1校验和。然后,将炎症附加到在步骤(3)检索的参数/值列表中。 This could end up looking like this: http://framed.incloode.com? 最终可能看起来像这样:http://framed.incloode.com? i=TEST&arg1=val1&arg2=val2&t=123&v=1444567890&s=abcdef0123456789 i = TEST&arg1 = val1&arg2 = val2&t = 123&v = 1444567890&s = abcdef0123456789

The only thing I can see from your description is that you are not grabbing the hex digest at the end, which I can only assume is what the server expects. 从您的描述中我唯一可以看到的是,您最终并没有掌握十六进制摘要,我只能假设这是服务器所期望的。 Send this value instead: 而是发送此值:

hashed = signature.hexdigest()

Otherwise please post the documentation for this web service and make sure you are honoring case requirements (you never know...) 否则,请发布此Web服务的文档,并确保您遵守案例要求(您永远不会知道...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何验证Python中的RSA SHA1签名? - How do you verify an RSA SHA1 signature in Python? 如何在pyOpenSSL中验证RSA SHA1签名 - How do you verify an RSA SHA1 signature in pyOpenSSL Python:如何使用 pyopenssl 验证 sha1 rsa 签名? - Python: How to verify sha1 rsa signature with pyopenssl? pyOpenSSL 的 PKCS7 对象提供的信息非常少,如何在签名中获取公钥的 sha1 摘要 - pyOpenSSL's PKCS7 object provide very little information, how can I get the sha1 digest of the public key in the signature Django:使用 SHA1 加密密码 - Django : crypt password with SHA1 尝试使用Python验证SHA1消息签名。我究竟做错了什么? - Trying to verify SHA1 message signature using Python. What am I doing wrong? django-registration和sha1作为哈希算法 - django-registration and sha1 as hash algorithm 如何在scrapy下载的Django模板中引用以SHA1哈希形式存储的图像 - How to refer to images stored in SHA1 hash form in django template downloaded by scrapy 如何使用 HMAC-SHA512 和 Python requests 库签署 POST 请求? - How do I sign a POST request using HMAC-SHA512 and the Python requests library? 为什么我在系统DLL上的Powershell和32bit-Python之间获得不同的SHA1哈希? - Why do I get a different SHA1 hash between Powershell and 32bit-Python on a system DLL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM