Python加密—HMACSHA1 加密
2021-06-21 本文已影响0人
还是那个没头脑
1、HMACSHA1 加密通过hmac模块实现,需要一个key
import hmac
from hashlib import sha1
def hash_hmac(key, code, sha1):
hmac_code = hmac.new(key.encode(), code.encode(), sha1)
return hmac_code.hexdigest()
if __name__ == '__main__':
print(hash_hmac('qhn757Yhlmo8IgbusRLE2nUPb8TorbyA', 'test', sha1))
输出:947a316d9de36cd36268e459893335bd081fe57f
2、HMACSHA1加密,返回Base64编码
import base64
import hmac
from hashlib import sha1
def hash_hmac(key, code, sha1):
hmac_code = hmac.new(key.encode(), code.encode(), sha1).digest()
return base64.b64encode(hmac_code).decode()
if __name__ == '__main__':
print(hash_hmac('qhn757Yhlmo8IgbusRLE2nUPb8TorbyA', 'test', sha1))
输出:lHoxbZ3jbNNiaORZiTM1vQgf5X8=