Python中各种加密模块的应用是怎样的
Admin 2022-08-18 群英技术资讯 521 次浏览
它包含所有配方和基元,并在Python中提供高级编码接口.您可以使用以下命令安装加密模块 :
pip install cryptography
您可以使用以下代码实现加密模块 :
from cryptography.fernet import Fernet key = Fernet.generate_key() cipher_suite = Fernet(key) cipher_text = cipher_suite.encrypt("This example is used to demonstrate cryptography module") plain_text = cipher_suite.decrypt(cipher_text)
上面给出的代码产生以下输出 :
此处给出的代码用于验证密码并创建其哈希值.
它还包括用于验证密码以进行身份验证的逻辑.
import uuid import hashlib def hash_password(password): # uuid is used to generate a random number of the specified password salt = uuid.uuid4().hex return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt def check_password(hashed_password, user_password): password, salt = hashed_password.split(':') return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest() new_pass = input('Please enter a password: ') hashed_password = hash_password(new_pass) print('The string to store in the db is: ' + hashed_password) old_pass = input('Now please enter the password again to check: ') if check_password(hashed_password, old_pass): print('You entered the right password') else: print('Passwords do not match')
输出
场景1 : 如果您输入了正确的密码,您可以找到以下输出 :
情景2 : 如果我们输入错误的密码,您可以找到以下输出 :
Hashlib 包用于在数据库中存储密码.在此程序中,使用 salt ,在实现哈希函数之前,将随机序列添加到密码字符串中.
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
word、excel、PPT,虽然说是特殊文件,其实也是实际工作中我们经常会用到的文件类型。本文将为大家详解Python读取Word文件和文件内容的方法,感兴趣的可以了解一下
这篇文章主要介绍了python使用redis模块来跟redis实现交互,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
数据类型,即 dtype ,也是一个特殊的对象, 它包含了ndarray需要为某一种类型数据所申明的内存块信息(也成为了元数据,即表示数据的数据)
Python 有一个叫做argparse的内置库,可以用它来创建一个命令行界面。本文将详解如何利用argparse实现创建一个命令行应用程序,需要的可以参考一下
Python中字符串反转常用的五种方法:使用字符串切片、使用递归、使用列表reverse()方法、使用栈和使用for循环。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008