hashlib.sha256:
import hashlib
x = hashlib.sha256()
x.update(b"asd")
print("x_1 = " + x.hexdigest())
x = hashlib.sha256()
x.update("asd".encode())
print("x_2 = " + x.hexdigest())
x = hashlib.sha256()
x.update(b"a")
x.update(b"s")
x.update(b"d")
print("x_3 = " + x.hexdigest())
y = hashlib.sha256(b"asd").hexdigest()
print("y_1 = " + y)
z = hashlib.new("sha256")
z.update(b"asd")
print("z_1 = " + z.hexdigest())
运行结果:
x_1 = 688787d8ff144c502c7f5cffaafe2cc588d86079f9de88304c26b0cb99ce91c6
x_2 = 688787d8ff144c502c7f5cffaafe2cc588d86079f9de88304c26b0cb99ce91c6
x_3 = 688787d8ff144c502c7f5cffaafe2cc588d86079f9de88304c26b0cb99ce91c6
y_1 = 688787d8ff144c502c7f5cffaafe2cc588d86079f9de88304c26b0cb99ce91c6
z_1 = 688787d8ff144c502c7f5cffaafe2cc588d86079f9de88304c26b0cb99ce91c6
hashlib.pbkdf2_hmac加盐:
import hashlib
import binascii
import os
x = hashlib.pbkdf2_hmac("sha256", b"asd", b"", 1)
print("x_1 = " + binascii.hexlify(x).decode())
x = hashlib.pbkdf2_hmac("sha256", b"asd", b"", 1) # 相同盐值
print("x_2 = " + binascii.hexlify(x).decode())
x = hashlib.pbkdf2_hmac("sha256", b"asd", b"", 10) # 相同盐值,不同迭代次数
print("x_3 = " + binascii.hexlify(x).decode())
x = hashlib.pbkdf2_hmac("sha256", b"asd", b"dsa", 1) # 不同盐值,相同迭代次数
print("x_4 = " + binascii.hexlify(x).decode())
y = hashlib.pbkdf2_hmac("sha256", b"asd", os.urandom(16), 1) # 随机生成盐值
print("y_1 = " + binascii.hexlify(y).decode())
运行结果:
x_1 = b930d78b395bde9a074f5f762bc7c64aa85a05cd74dbf22d25c2357c6a59f883
x_2 = b930d78b395bde9a074f5f762bc7c64aa85a05cd74dbf22d25c2357c6a59f883
x_3 = 96c27cfb86a1b4a9e9c9e8ae1f8c3c3188c1c23ff268e410a9a6f7bca3a35f67
x_4 = f9653d10a4cc5b956fb030b1b420f8fdf145adfdd28650e007dd912b8e2a6c5a
y_1 = 338208350ccb7c6c99c582f83fda351801f79cd62def6799456a06da6598f567
hashlib.blake2b:
import hashlib
import hmac
import cryptography.fernet
x = hashlib.blake2b()
x.update(b"asd")
print("x = " + x.hexdigest())
print("true" if hmac.compare_digest(x.hexdigest(), hashlib.blake2b(b"asd").hexdigest()) else "false")
y = hashlib.blake2b(digest_size=5, key=b"", salt=b"", person=b"") # 键控哈希、加盐、个性化
y.update(b"asd")
print("y = " + y.hexdigest())
print("true" if hmac.compare_digest(y.hexdigest(), hashlib.blake2b(b"asd", digest_size=5, key=b"", salt=b"", person=b"").hexdigest()) else "false")
a = cryptography.fernet.Fernet.generate_key() # 自动生成密钥
b = cryptography.fernet.Fernet(a)
c = b.encrypt("asd".encode()) # 使用生成的密钥加密
print(c)
d = b.decrypt(c) # 使用生成的密钥解密
print(d.decode())
z = hmac.new(a, digestmod=hashlib.blake2b) # 使用本地密钥进行加密
z.update(b"asd")
print("z = " + z.hexdigest())
print("true" if hmac.compare_digest(z.hexdigest(), hmac.new(key=a, msg=b"asd", digestmod=hashlib.blake2b).hexdigest()) else "false")
运行结果:
x = e4139471a1800a05b5e02b56669f025d01e33c5655153a4faa29f694f7849c63f97577fcc3e397d1138cf4640e0c2053e74a7af646598310d076dc6f15d1af50
true
y = 940ba28546
true
b'gAAAAABbM9urQSo8QilnHGDUEVL-CCFOkkW4yDOc10yoAp4B-tUabQZw6y8BNbATty8oC-Om555xF8Xr4rQxRDgYkIZ67u9lsQ=='
asd
z = cc18cfb36af92c64bf9a55114e22d178a05a6f10bfe9d1bf7067acb108146edd1ea595cd46d8fbdda58a334d6444a22d404774162f7d675a8ef59f70b983af4e
true