pyhthon常用模块hashlib
python hashlib模块
一,hashlib模块主要用于加密,其中提供sha1,sha224,sha256,sha384,sha512,md5算法。常用的使用md5即可完成需求。
一,使用md5普通加密
import hashlib m = hashlib.md5() m.update(b'cnblog.com') print(m.hexdigest())
二,2次加密,其实就是将第一次的要加密的字符串和第二次要加密的字符串拼接起来做一次加密
import hashlib m2 = hashlib.md5('www.'.encode('utf-8')) m2.update('cnblogs.com'.encode('utf-8')) print(m2.hexdigest())
或者
import hashlib m3 = hashlib.md5() m3.update('www.'.encode('utf-8')) m3.update('cnblogs.com'.encode('utf-8')) print(m3.hexdigest())
或者直接将2次加密的字符串直接拼接
import hashlib m4 = hashlib.md5() m4.update('www.cnblogs.com'.encode('utf-8')) print(m4.hexdigest())
三,使用其他加密算法,和md5类似,比如sha512
import hashlib m5 = hashlib.sha512() m5.update(b'www') print(m5.hexdigest())
小例子:
import hashlib import sys class UserInfo(object):
def __init__(self):
print(
"""
1,注册
2,登陆
3,退出 """
)
#此方法用于加密用户的密码
def md5(self,password):
md5_pwd = hashlib.md5()
# md5_pwd.update(bytes(password,encoding='utf-8'))
md5_pwd.update(password.encode('utf-8')) return md5_pwd.hexdigest() #此方法用于后面注册和登陆公共输入接口
def input(self):
username = input('请输入账号:')
password = input('请输入密码:')
return (username,password) #注册方法
def register(self):
username,password= self.input()
with open('userinfo.txt','r') as f:
while True:
line = f.readline()
if line:
if username == line.split()[0]:
print('user alreay register')
break
else:
password = self.md5(password)
with open('userinfo.txt','a') as f:
f.write('{0}\t{1}\n'.format(username,password))
print('rgister sucessful,Please login ')
break #登陆方法
def login(self):
username,password= self.input()
with open('userinfo.txt','r') as f:
for line in f:
if line.split()[0] == username and line.split()[1] == self.md5(password):
print('login sucessful!')
break
elif line.split()[0] == username and line.split()[1] != self.md5(password):
print('passwd error!')
break
else:
print('user not register!!!')
print('login fail!!!') #主程序
def main():
while True:
user = UserInfo()
n = int(input('please input number:>>>'))
if n == 1:
user.register()
elif n == 2:
user.login()
else:
sys.exit(0) if __name__ == '__main__':
main()
pyhthon常用模块hashlib的更多相关文章
- 常用模块(hashlib,configparser,logging)
常用模块(hashlib,configparser,logging) hashlib hashlib 摘要算法的模块md5 sha1 sha256 sha512摘要的过程 不可逆能做的事:文件的一致性 ...
- 20 常用模块 hashlib hmac:加密 xml xlrd xlwt:excel读|写 configparser subprocess
hashlib模块:加密 加密: 1.有解密的加密方式 2.无解密的加密方式:碰撞检查 hashlib -- 1)不同数据加密后的结果一定不一致 -- 2)相同数据的加密结果一定是一致的 import ...
- Python全栈之路----常用模块----hashlib加密模块
加密算法介绍 HASH Python全栈之路----hash函数 Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列 ...
- python常用模块——hashlib模块
Python的hashlib提供了常见的摘要算法,如md5.sha1等 什么是摘要算法了?摘要算法又称哈希算法.散列算法. 它通过一个函数,把任意长度的数据转化魏一个长度固定的数据串(通常用十六进制的 ...
- 常用模块 - hashlib模块
一.简介 Python的hashlib提供了常见的摘要算法,如MD5.SHA1.SHA224.SHA256.SHA384.SHA512等算法. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过 ...
- python常用模块集合
python常用模块集合 Python自定义模块 python collections模块/系列 Python 常用模块-json/pickle序列化/反序列化 python 常用模块os系统接口 p ...
- Python常用模块-摘要算法(hashlib)
Python常用模块-摘要算法(hashlib) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MD5算法参数详解 1.十六进制md5算法摘要 #!/usr/bin/env p ...
- python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则
python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess ...
- 常用模块二(hashlib、configparser、logging)
阅读目录 常用模块二 hashlib模块 configparse模块 logging模块 常用模块二 返回顶部 hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SH ...
随机推荐
- HoloLens开发手记 - 语音输入 Voice input
语音是HoloLens三大重要输入形式之一.它允许你直接通过语言控制全息图像,而不用借助手势.你只要凝视全息图像然后说出语音命令即可.语音输入是自然的交互方式,它能够很好的改善复杂的交互,因为通过一条 ...
- Java language
1.Java开发环境: java编译运行过程: 1. 编译期:.java源文件,经过编译,生成.class字节码文件 2. 运行期:JVM加载.class并运行.class - 特点:跨平台.一次编程 ...
- curl: (7) Failed connect to 172.16.100.199:9200; 没有到主机的路由
没有到主机的路由这种问题很常见,多数是由机器的防火墙没有关闭. Ubuntu 查看防火墙状态 ufw status 关闭防火墙 ufw disable centos6 查看防火墙状态 service ...
- UFLDL 教程学习笔记(三)自编码与稀疏性
UFLDL(Unsupervised Feature Learning and Deep Learning)Tutorial 是由 Stanford 大学的 Andrew Ng 教授及其团队编写的一套 ...
- Hadoop社区版搭建
1.环境准备 1.1 硬件配置 设备名 参数 数量 6台 系统 centos6.5 内存 64G 硬盘 32T/台 CPU 16核/台 1.2 软件版本 Hadoop-2.x 下载地址 JDK1.7 ...
- Eureka自我保护模式——难点重点
一.开启Eureka自我保护模式访问Eureka主页时,如果看到这样一段大红色的句子: EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ...
- 方便操作的命名范围scope
<?php namespace Goods\Model; use Think\Model; class GoodsModel extends Model { protected $_scope ...
- vue里碰到 $refs 的问题
记录困惑自己一个简单的问题...(瞬间感觉官方文档的强大) 在自己做的一个项目中,遇到一个列表页,根据id能进入详情页(动态匹配路由),详情页是单独的一个组件,在这个详情的组件里,我想获取内容给你区域 ...
- 关于fasterxml-jackson发生Can not deserialize instance of异常原因验证
关于fasterxml-jackson发生Can not deserialize instance of异常原因验证 这两天线上有大量的java.lang.IllegalArgumentExcepti ...
- 第一篇 Spring boot 配置文件笔记
spring boot 不需要配置太多文件程序便可正常运行,特殊情况需要我们自己配置文件. 项目以IDEA写实例,系统会默认在src/main/java/resources目录下创建applicati ...