1、HMACSHA1 加密通过hmac模块实现,需要一个key
import hmacfrom hashlib import sha1def hash_hmac(key, code, sha1): hmac_code = hmac.new(key.encode(), code.encode(), sha1) return hmac_code.hexdigest()if __name__ == '__main__': print(hash_hmac('08F5B4886112BC6F1E04FE42DACDB2E8', 'xinxin', sha1)
加密结果:
HMACSHA1 加密: db30b4ad0d73b9e30d1c197dc478d262022077af
2、HMACSHA1加密,返回Base64编码
import base64import hmacfrom hashlib import sha1def hash_hmac(code, key, sha1): hmac_code = hmac.new(key.encode(), code.encode(), sha1).digest() return base64.b64encode(hmac_code).decode()if __name__ == '__main__': print(hash_hmac( 'test', '86ba4f7ef9b02409f0da6a632e0ab23a',sha1))
输出:+SJOJxzBsDX3icLWM+mwizNufhY=