Python加密—HMACSHA1 加密

为什么80%的码农都做不了架构师?>>> hot3.png

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('08F5B4886112BC6F1E04FE42DACDB2E8', 'xinxin', sha1)

加密结果:

HMACSHA1 加密: db30b4ad0d73b9e30d1c197dc478d262022077af

2、HMACSHA1加密,返回Base64编码

import base64
import hmac
from hashlib import sha1


def 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=

 

 

转载于:https://my.oschina.net/xxWang/blog/731299

你可能感兴趣的:(Python加密—HMACSHA1 加密)