Python中各种加密模块的应用是怎样的
这篇文章主要介绍了Python中各种加密模块的应用是怎样的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Python中各种加密模块的应用是怎样的文章都会有所收获,下面我们一起来看看吧。
在本章中,您将详细了解Python中各种加密模块.
以上就是关于“Python中各种加密模块的应用是怎样的”的介绍了,感谢各位的阅读,希望这篇文章能帮助大家解决问题。如果想要了解更多知识,欢迎关注博信,小编每天都会为大家更新不同的知识。
在本章中,您将详细了解Python中各种加密模块.
加密模块
它包含所有配方和基元,并在Python中提供高级编码接口.您可以使用以下命令安装加密模块 :
pip install cryptography
代码
您可以使用以下代码实现加密模块 :
fromcryptography.fernetimportFernet key=Fernet.generate_key() cipher_suite=Fernet(key) cipher_text=cipher_suite.encrypt("Thisexampleisusedtodemonstratecryptographymodule") plain_text=cipher_suite.decrypt(cipher_text)
输出
上面给出的代码产生以下输出 :
此处给出的代码用于验证密码并创建其哈希值.
它还包括用于验证密码以进行身份验证的逻辑.
importuuid importhashlib defhash_password(password): #uuidisusedtogeneratearandomnumberofthespecifiedpassword salt=uuid.uuid4().hex returnhashlib.sha256(salt.encode()+password.encode()).hexdigest()+':'+salt defcheck_password(hashed_password,user_password): password,salt=hashed_password.split(':') returnpassword==hashlib.sha256(salt.encode()+user_password.encode()).hexdigest() new_pass=input('Pleaseenterapassword:') hashed_password=hash_password(new_pass) print('Thestringtostoreinthedbis:'+hashed_password) old_pass=input('Nowpleaseenterthepasswordagaintocheck:') ifcheck_password(hashed_password,old_pass): print('Youenteredtherightpassword') else: print('Passwordsdonotmatch')
输出
场景1: 如果您输入了正确的密码,您可以找到以下输出 :
情景2: 如果我们输入错误的密码,您可以找到以下输出 :
说明
Hashlib包用于在数据库中存储密码.在此程序中,使用salt,在实现哈希函数之前,将随机序列添加到密码字符串中.
以上就是关于“Python中各种加密模块的应用是怎样的”的介绍了,感谢各位的阅读,希望这篇文章能帮助大家解决问题。如果想要了解更多知识,欢迎关注博信,小编每天都会为大家更新不同的知识。
版权声明
本文仅代表作者观点,不代表博信信息网立场。