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 ...
随机推荐
- 一站式SpringBoot for NoSQL Study Tutorial 开发教程学习手册
SpringBoot2.0 + NoSQL使用教程,项目名称:“SpringBoot2NoSQL” 项目地址: https://gitee.com/475660/SpringBoot2NoSQL 项目 ...
- Python进程-理论
进程定义 程序: 计算机程序是存储在磁盘上的可执行二进制(或其他类型)文件.只有把它们加载到内存中,并被操作系统调用,它们才会拥有其自己的生命周期. 进程: 进程则是表示的一个正在执行的程序.每个进程 ...
- for(var i=0;i<=3;i++){ setTimeout(function() { console.log(i) }, 10);}
for(var i=0;i<=3;i++){ setTimeout(function() { console.log(i) }, 10);} 答案:打印4次4 这道题涉及了异步.作用域.闭包 ...
- linux中sed命令的使用
sed命令是linux或者shell编程中常用的筛选.替换命令,如果能熟练使用sed则对经常使用的人来说在工作上是非常有帮助的 下面把sed主要的用法列出来(有错误的地方大家可以指正): p命令只打印 ...
- Spring Boot - 修改Tomcat默认的8080端口
前言 默认情况下,Spring Boot内置的Tomcat服务会使用8080端口启动,我们可以使用以下任何技巧去更改默认的Tomcat端口: 注:我们可以通过server.port=0配置,去自动配置 ...
- PetaPoco在ASP.NET Core 2.2中使用注入方式访问数据库
.Net Core中一个特别重要的特性就是依赖注入功能,那么我们在使用PetaPoco的时候是否也可以使用依赖注入特性呢? 回答当然是可以的啦.使用方法(两种注入方式)如下 services.AddS ...
- 数据库新秀 postgresql vs mongo 性能PK
前几天看了一篇文章<High Performance JSON PostgreSQL vs. MongoDB> 发布在Percona Live Europe 2017 作者是<Dom ...
- 使用centos 7安装conpot
使用CentOS的版本7.3和Conpot 0.5.1(也可能适用于其他CentOS的版本) 1.通过ssh登录系统,并需要具有足够的系统特权(e.g root) 2.系统升级 yum -y upda ...
- 百度umeditor富文本编辑器插件扩展
富文本编辑器在WEB开发中经常用到,个人比较喜欢用百度出的ueditor这款,ueditor这款本身支持插件扩展的,但是ueditor的mini版本 umeditor 就没有那么方便了,不过找了很多资 ...
- Scala函数与函数式编程
函数是scala的重要组成部分, 本文将探讨scala中函数的应用. scala作为支持函数式编程的语言, scala可以将函数作为对象即所谓"函数是一等公民". 函数定义 sca ...