python小程序-0017

第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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值