第17题:通常,登陆某个网站或者 APP,需要使用用户名和密码。密码是如何加密后存储起来的呢?请使用 Python 对密码加密。
#!/usr/bin/env python3
# -*- coding : utf-8 -*-
from hashlib import sha256
from hmac import HMAC
import os
def encrypt_password(password,salt = None):
if salt is None:
salt = os.urandom(8)
if isinstance(salt,str):
salt = salt.encode('utf-8')
new_password = password.encode('utf-8')
encrypt_password = HMAC(salt,new_password,sha256).hexdigest()
print("Encrypt passwrod is %s."% encrypt_password )
if __name__ == '__main__':
raw_password = input("Please input your password:")
encrypt_password(raw_password)