'''
Descripttion: 
hashlib提供了常用的摘要算法（单向算法）
摘要算法：通过摘要函数，对任意长度的数据，计算出固定长度的摘要，可以检测原数据被人篡改
MD5：固定128bit(128位二进制)，通常用 32位 16进制字符串，【1个16进制占4位二进制】
SHA1：固定160bit(160位二进制)，通常用 40位 16进制字符串，SHA256和SHA512更安全，但是计算更慢
MD5加盐：防止用户定义一个简单的字符串，MD5编码时默认在原字符串上加一个固定的复杂字符串
hmac：可以看做MD5加盐的升级版，把盐当做一个秘钥，对原字符串编码，而不是简单的字符串拼接，而且针对所有哈希算法

version: 1.0
Author: xieyupeng
Date: 2020-08-05 22:24:51
LastEditors: xieyupeng
LastEditTime: 2020-08-14 17:42:33
'''
import hashlib
import hmac

md5 = hashlib.md5()
md5.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print(md5.hexdigest())

md5.update('my name is xieyupeng,'.encode('utf-8'))  # 可以分批update
md5.update('i is studying python!'.encode('utf-8'))
print(md5.hexdigest())

md5.update(('my name is xieyupeng' + 'jiayan+_&@*').encode('utf-8'))  # MD5加盐
print('md5加盐：', md5.hexdigest())

sha1 = hashlib.sha1()
sha1.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha1：', sha1.hexdigest())

sha256 = hashlib.sha256()  # 更安全，但是更慢
sha256.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha256：', sha256.hexdigest())

sha512 = hashlib.sha512()  # 更安全，但是更慢
sha512.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha512：', sha512.hexdigest())

msg = b'python is so butifual!'
key = b'!@#$%'
h = hmac.new(key, msg, digestmod='MD5')
print('hmac实现带key哈希：', h.hexdigest())

d7daf9ff1d7f2be292683211a8150b8f1bdf4e7d
<mask token>

<mask token>
md5.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print(md5.hexdigest())
md5.update('my name is xieyupeng,'.encode('utf-8'))
md5.update('i is studying python!'.encode('utf-8'))
print(md5.hexdigest())
md5.update(('my name is xieyupeng' + 'jiayan+_&@*').encode('utf-8'))
print('md5加盐：', md5.hexdigest())
<mask token>
sha1.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha1：', sha1.hexdigest())
<mask token>
sha256.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha256：', sha256.hexdigest())
<mask token>
sha512.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha512：', sha512.hexdigest())
<mask token>
print('hmac实现带key哈希：', h.hexdigest())

<mask token>
md5 = hashlib.md5()
md5.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print(md5.hexdigest())
md5.update('my name is xieyupeng,'.encode('utf-8'))
md5.update('i is studying python!'.encode('utf-8'))
print(md5.hexdigest())
md5.update(('my name is xieyupeng' + 'jiayan+_&@*').encode('utf-8'))
print('md5加盐：', md5.hexdigest())
sha1 = hashlib.sha1()
sha1.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha1：', sha1.hexdigest())
sha256 = hashlib.sha256()
sha256.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha256：', sha256.hexdigest())
sha512 = hashlib.sha512()
sha512.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha512：', sha512.hexdigest())
msg = b'python is so butifual!'
key = b'!@#$%'
h = hmac.new(key, msg, digestmod='MD5')
print('hmac实现带key哈希：', h.hexdigest())

<mask token>
import hashlib
import hmac
md5 = hashlib.md5()
md5.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print(md5.hexdigest())
md5.update('my name is xieyupeng,'.encode('utf-8'))
md5.update('i is studying python!'.encode('utf-8'))
print(md5.hexdigest())
md5.update(('my name is xieyupeng' + 'jiayan+_&@*').encode('utf-8'))
print('md5加盐：', md5.hexdigest())
sha1 = hashlib.sha1()
sha1.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha1：', sha1.hexdigest())
sha256 = hashlib.sha256()
sha256.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha256：', sha256.hexdigest())
sha512 = hashlib.sha512()
sha512.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha512：', sha512.hexdigest())
msg = b'python is so butifual!'
key = b'!@#$%'
h = hmac.new(key, msg, digestmod='MD5')
print('hmac实现带key哈希：', h.hexdigest())

'''
Descripttion: 
hashlib提供了常用的摘要算法（单向算法）
摘要算法：通过摘要函数，对任意长度的数据，计算出固定长度的摘要，可以检测原数据被人篡改
MD5：固定128bit(128位二进制)，通常用 32位 16进制字符串，【1个16进制占4位二进制】
SHA1：固定160bit(160位二进制)，通常用 40位 16进制字符串，SHA256和SHA512更安全，但是计算更慢
MD5加盐：防止用户定义一个简单的字符串，MD5编码时默认在原字符串上加一个固定的复杂字符串
hmac：可以看做MD5加盐的升级版，把盐当做一个秘钥，对原字符串编码，而不是简单的字符串拼接，而且针对所有哈希算法

version: 1.0
Author: xieyupeng
Date: 2020-08-05 22:24:51
LastEditors: xieyupeng
LastEditTime: 2020-08-14 17:42:33
'''
import hashlib
import hmac

md5 = hashlib.md5()
md5.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print(md5.hexdigest())

md5.update('my name is xieyupeng,'.encode('utf-8'))  # 可以分批update
md5.update('i is studying python!'.encode('utf-8'))
print(md5.hexdigest())

md5.update(('my name is xieyupeng' + 'jiayan+_&@*').encode('utf-8'))  # MD5加盐
print('md5加盐：', md5.hexdigest())

sha1 = hashlib.sha1()
sha1.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha1：', sha1.hexdigest())

sha256 = hashlib.sha256()  # 更安全，但是更慢
sha256.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha256：', sha256.hexdigest())

sha512 = hashlib.sha512()  # 更安全，但是更慢
sha512.update('my name is xieyupeng,i is studying python!'.encode('utf-8'))
print('sha512：', sha512.hexdigest())

msg = b'python is so butifual!'
key = b'!@#$%'
h = hmac.new(key, msg, digestmod='MD5')
print('hmac实现带key哈希：', h.hexdigest())
