尝试更改其危险的默认摘要方法。代码片段如下。问题 - 我应该获得相同的签名结果吗?我是否正确设置了摘要方法?
from itsdangerous import Signer
import hashlib
s = Signer('secret-key')
s.default_digest_method = staticmethod(hashlib.sha1)
print ('sha1-->', s.sign(b'my string'))
s.default_digest_method = staticmethod(hashlib.sha256)
print ('sha2-->', s.sign(b'my string'))
结果是:
sha1--> b'my string.wh6tMHxLgJqB6oY1uT73iMlyrOA'
sha2--> b'my string.wh6tMHxLgJqB6oY1uT73iMlyrOA'
答案 0 :(得分:2)
default_digest_method
,除非digest_method
未通过。使用digest_method
参数:
import hashlib
from itsdangerous import Signer
s = Signer('secret-key', digest_method=hashlib.sha1)
print('sha1-->', s.sign(b'my string'))
s = Signer('secret-key', digest_method=hashlib.sha256)
print('sha2-->', s.sign(b'my string'))
输出:
sha1--> b'my string.wh6tMHxLgJqB6oY1uT73iMlyrOA'
sha2--> b'my string.PVHY4OzY_ekIpRd3A5G0vzTkhg8wSod5E3p90JgBCKw'
如果要覆盖默认摘要方法,则需要在创建签名者对象之前修改类属性:
Signer.default_digest_method = hashlib.sha1
s = Signer('secret-key')
print('sha1-->', s.sign(b'my string'))
Signer.default_digest_method = hashlib.sha256
s = Signer('secret-key')
print('sha2-->', s.sign(b'my string'))