hmac
这是一个使用with的工作示例sha256
import hashlib
import hmac
import string
from random import choice, randint
characters = string.ascii_letters + string.ascii_lowercase + string.ascii_uppercase + string.digits + string.hexdigits + string.punctuation + string.octdigits
password = "".join(choice(characters) for x in range(randint(25, 100)))
print(password)
SECRET = 'this is my secret'
def hash_password(pw):
hashed_password = hmac.new(
SECRET.encode(),
msg=pw.encode(),
digestmod=hashlib.sha256
).hexdigest().upper()
return hashed_password
password_file = 'test.password'
with open(password_file, 'w') as f:
f.write(hash_password(password))
user_supplied = input('Enter the password supplied: ')
with open(password_file, 'r') as f:
print(f"Does match? {f.read() == hash_password(user_supplied)}")
这是一个示例运行
bXFREVkOJ~PDUBEfSkUcK[W6s~yDcDzdCi*SjxOc6T79a5[7s\P0i|)yh#'rK6nzB@CEDX1T7Umc-OOEi
Enter the password supplied: bXFREVkOJ~PDUBEfSkUcK[W6s~yDcDzdCi*SjxOc6T79a5[7s\P0i|)yh#'rK6nzB@CEDX1T7Umc-OOEi
Does match? True