python小程序-0017

来源:互联网 发布:mysql的存储过程 编辑:程序博客网 时间:2024/06/01 09:42

第17题:通常,登陆某个网站或者 APP,需要使用用户名和密码。密码是如何加密后存储起来的呢?请使用 Python 对密码加密。

  • 阅读资料 用户密码的存储与 Python 示例
  • 阅读资料 Hashing Strings with Python
  • 阅读资料 Python’s safest method to store and retrieve passwords from a database
#!/usr/bin/env python3# -*- coding : utf-8 -*-from hashlib import sha256from hmac import HMACimport osdef 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)
原创粉丝点击