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的更多相关文章

  1. 常用模块(hashlib,configparser,logging)

    常用模块(hashlib,configparser,logging) hashlib hashlib 摘要算法的模块md5 sha1 sha256 sha512摘要的过程 不可逆能做的事:文件的一致性 ...

  2. 20 常用模块 hashlib hmac:加密 xml xlrd xlwt:excel读|写 configparser subprocess

    hashlib模块:加密 加密: 1.有解密的加密方式 2.无解密的加密方式:碰撞检查 hashlib -- 1)不同数据加密后的结果一定不一致 -- 2)相同数据的加密结果一定是一致的 import ...

  3. Python全栈之路----常用模块----hashlib加密模块

    加密算法介绍 HASH       Python全栈之路----hash函数 Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列 ...

  4. python常用模块——hashlib模块

    Python的hashlib提供了常见的摘要算法,如md5.sha1等 什么是摘要算法了?摘要算法又称哈希算法.散列算法. 它通过一个函数,把任意长度的数据转化魏一个长度固定的数据串(通常用十六进制的 ...

  5. 常用模块 - hashlib模块

    一.简介 Python的hashlib提供了常见的摘要算法,如MD5.SHA1.SHA224.SHA256.SHA384.SHA512等算法. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过 ...

  6. python常用模块集合

    python常用模块集合 Python自定义模块 python collections模块/系列 Python 常用模块-json/pickle序列化/反序列化 python 常用模块os系统接口 p ...

  7. Python常用模块-摘要算法(hashlib)

    Python常用模块-摘要算法(hashlib) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MD5算法参数详解 1.十六进制md5算法摘要 #!/usr/bin/env p ...

  8. 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 ...

  9. 常用模块二(hashlib、configparser、logging)

    阅读目录 常用模块二 hashlib模块 configparse模块 logging模块   常用模块二 返回顶部 hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SH ...

随机推荐

  1. Keras 资源

    Keras中文文档 github Keras example 官方博客 A ten-minute introduction to sequence-to-sequence learning in Ke ...

  2. Hexo的next主题安装

    通过Git+Hexo搭建的个人博客地址:https://liangh.top/ 1.使用git克隆最新版本 2.先在themes目录创建一个next文件夹,然后在hexo站点目录下右键打开Git Ba ...

  3. spring cloud+.net core搭建微服务架构:配置中心续(五)

    前言 上一章最后讲了,更新配置以后需要重启客户端才能生效,这在实际的场景中是不可取的.由于目前Steeltoe配置的重载只能由客户端发起,没有实现处理程序侦听服务器更改事件,所以还没办法实现彻底实现这 ...

  4. MyBatis源码解析【8】简单demo理解接口式编程

    前言 这个分类比较连续,如果这里看不懂,或者第一次看,请回顾之前的博客 http://www.cnblogs.com/linkstar/category/1027239.html 上一次我们经过源码的 ...

  5. Eureka多机高可用

    线上Eureka高可用集群,至少三个节点组成一个集群,推荐部署在不同的服务器上,IP用域名绑定,端口保持一致. 10.1.22.26:876210.1.22.27:876210.1.22.28:876 ...

  6. rtsp信令交互流程

  7. javascript中 Function.prototype.apply()与Function.prototype.call() 对比详解

    Function.prototype.apply()|Function.prototype.call() apply()方法可以在使用一个指定的 this 值和一个参数数组(或类数组对象)的前提下调用 ...

  8. Hyperledger Fabric密码模块系列之BCCSP(一)

    Fabric作为IBM主导的区块链平台,可谓是联盟链中的一枝独秀,现如今已经有100多个大型国际银行.金融以及科技公司的加盟.与其说Fabric是区块链的一种平台,倒不如说是一个区块链框架更加精确,因 ...

  9. FTP 150 Opening BINARY mode data connection for MLSD 读取目录列表失败

    这完全是因为防火墙的问题,把服务器的防火墙关闭之后,在连接,一切ok

  10. c#执行sql语句的问题

    var sql = "UPDATE dbo.Purchase_Order SET StatusID = 14,StatusText='已合并', Remark=isnull(Remark, ...