python小程序-0017

第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 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)

你可能感兴趣的:(python)