Python3 hashlib密码散列算法原理详解


Posted in Python onMarch 30, 2020

1.hashlib密码散列

hashlib模块定义了一个API来访问不同的密码散列算法。要使用一个特定的散列算法,可以用适当的构造器函数或new()来创建一个散列对象。不论使用哪个具体的算法,这些对象都使用相同的API。

1.1 散列算法

由于hashlib有OpenSSL提供“底层支持”,所以OpenSSL库提供的所有算法都可用,包括:

  • md5
  • sha1
  • sha224
  • sha256
  • sha384
  • sha512

有些算法在所有平台上都可用,而有些则依赖于底层库。这两种算法分别由algorithms_guaranteed和algorithms_available提供。

import hashlib
print('Guaranteed:\n{}\n'.format(
  ', '.join(sorted(hashlib.algorithms_guaranteed))))
print('Available:\n{}'.format(
  ', '.join(sorted(hashlib.algorithms_available))))

Guaranteed:
blake2b, blake2s, md5, sha1, sha224, sha256, sha384, sha3_224, sha3_256, sha3_384, sha3_512, sha512, shake_128, shake_256
Available:
DSA, DSA-SHA, MD4, MD5, RIPEMD160, SHA, SHA1, SHA224, SHA256, SHA384, SHA512, blake2b, blake2s, dsaEncryption, dsaWithSHA, ecdsa-with-SHA1, md4, md5, ripemd160, sha, sha1, sha224, sha256, sha384, sha3_224, sha3_256, sha3_384, sha3_512, sha512, shake_128, shake_256, whirlpool

1.2 MD5示例

要为一个数据块(在这里就是转换为一个字节串的Unicode串)计算MD5散列或摘要,首先要创建散列对象,然后增加数据,最后调用digest()或hexdigest()。

import hashlib
lorem = '''Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.'''
h = hashlib.md5()
h.update(lorem.encode('utf-8'))
print(h.hexdigest())

这个例子使用了hexdigest()方法而不是digest(),因为要格式化输出以便清楚的打印。如果可以接受二进制摘要值,那么可以使用digest()。

Python3 hashlib密码散列算法原理详解

1.3 SHA1示例

SHA1摘要也用同样的方式计算。

import hashlib
lorem = '''Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.'''
h = hashlib.sha1()
h.update(lorem.encode('utf-8'))
print(h.hexdigest())

这个例子中的摘要值有所不同,因为MD5和SHA1算法不同。

Python3 hashlib密码散列算法原理详解

1.4 增量更新

散列计算器的update()方法可以反复调用。每次调用时,都会根据提供的附加文本更新摘要。增量更新比将整个文件读入内存更高效,而且能生成相同的结果。

import hashlib
lorem = '''Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.'''
h = hashlib.md5()
h.update(lorem.encode('utf-8'))
all_at_once = h.hexdigest()
def chunkize(size, text):
  "Return parts of the text in size-based increments."
  start = 0
  while start < len(text):
    chunk = text[start:start + size]
    yield chunk
    start += size
  return
h = hashlib.md5()
for chunk in chunkize(64, lorem.encode('utf-8')):
  h.update(chunk)
line_by_line = h.hexdigest()
print('All at once :', all_at_once)
print('Line by line:', line_by_line)
print('Same    :', (all_at_once == line_by_line))

这个例子展示了读取或生成数据时如何以增量方式更新一个摘要。

Python3 hashlib密码散列算法原理详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python使用PyGreSQL操作PostgreSQL数据库教程
Jul 30 Python
Windows下实现Python2和Python3两个版共存的方法
Jun 12 Python
浅谈Python生成器generator之next和send的运行流程(详解)
May 08 Python
Python爬虫通过替换http request header来欺骗浏览器实现登录功能
Jan 07 Python
python去掉空白行的多种实现代码
Mar 19 Python
python验证码识别教程之灰度处理、二值化、降噪与tesserocr识别
Jun 04 Python
Python 字符串换行的多种方式
Sep 06 Python
Python实现最常见加密方式详解
Jul 13 Python
django 连接数据库 sqlite的例子
Aug 14 Python
python用requests实现http请求代码实例
Oct 31 Python
Django与pyecharts结合的实例代码
May 13 Python
python--shutil移动文件到另一个路径的操作
Jul 13 Python
django xadmin action兼容自定义model权限教程
Mar 30 #Python
使用Django xadmin 实现修改时间选择器为不可输入状态
Mar 30 #Python
Django admin 实现search_fields精确查询实例
Mar 30 #Python
Django模型中字段属性choice使用说明
Mar 30 #Python
Django+python服务器部署与环境部署教程详解
Mar 30 #Python
Python GUI编程学习笔记之tkinter中messagebox、filedialog控件用法详解
Mar 30 #Python
django自定义非主键自增字段类型详解(auto increment field)
Mar 30 #Python
You might like
PHP array_multisort()函数的使用札记
2011/07/03 PHP
PHP 图片上传代码
2011/09/13 PHP
PHP中判断文件存在使用is_file还是file_exists?
2015/04/03 PHP
php获取远程文件大小
2015/10/20 PHP
thinkPHP5.1框架使用SemanticUI实现分页功能示例
2019/08/03 PHP
关于laravel-admin ueditor 集成并解决刷新的问题
2019/10/21 PHP
jquery中获取select选中值的代码
2011/06/27 Javascript
BootStrap实用代码片段之一
2016/03/22 Javascript
js实现文字超出部分用省略号代替实例代码
2016/09/01 Javascript
Vue 2.X的状态管理vuex记录详解
2017/03/23 Javascript
使用Math.max,Math.min获取数组中的最值实例
2017/04/25 Javascript
通过学习bootstrop导航条学会修改bootstrop颜色基调
2017/06/11 Javascript
Vue 解决父组件跳转子路由后当前导航active样式消失问题
2020/07/21 Javascript
Vue实现返回顶部按钮实例代码
2020/10/21 Javascript
Python的函数嵌套的使用方法
2014/01/24 Python
python 捕获 shell/bash 脚本的输出结果实例
2017/01/04 Python
python字符串常用方法
2018/06/14 Python
Python socket套接字实现C/S模式远程命令执行功能案例
2018/07/06 Python
Django实战之用户认证(用户登录与注销)
2018/07/16 Python
python调用摄像头显示图像的实例
2018/08/03 Python
Django使用AJAX调用自己写的API接口的方法
2019/03/06 Python
python爬虫解决验证码的思路及示例
2019/08/01 Python
Django框架 查询Extra功能实现解析
2019/09/04 Python
keras-siamese用自己的数据集实现详解
2020/06/10 Python
浅析Python 简单工厂模式和工厂方法模式的优缺点
2020/07/13 Python
Python中常用的os操作汇总
2020/11/05 Python
Python页面加载的等待方式总结
2021/02/28 Python
程序员机试试题汇总
2012/03/07 面试题
编程实现去掉XML的重复结点
2014/05/28 面试题
应聘编辑职位自荐信范文
2014/01/05 职场文书
便利店的创业计划书
2014/01/15 职场文书
旅游管理毕业生自荐信范文
2014/03/19 职场文书
房屋买卖协议书范本
2014/04/10 职场文书
五一活动标语
2014/06/30 职场文书
当幸福来敲门英文观后感
2015/06/01 职场文书
Python中生成随机数据安全性、多功能性、用途和速度方面进行比较
2022/04/14 Python