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 map和reduce函数用法示例
Feb 26 Python
Django rest framework基本介绍与代码示例
Jan 26 Python
Python操作MySQL模拟银行转账
Mar 12 Python
Python实现线程状态监测简单示例
Mar 28 Python
Numpy截取指定范围内的数据方法
Nov 14 Python
在python中实现将一张图片剪切成四份的方法
Dec 05 Python
如何利用Python开发一个简单的猜数字游戏
Sep 22 Python
导入tensorflow时报错:cannot import name 'abs'的解决
Oct 10 Python
Matplotlib绘制雷达图和三维图的示例代码
Jan 07 Python
Pytest如何使用skip跳过执行测试
Aug 13 Python
关于python3.9安装wordcloud出错的问题及解决办法
Nov 02 Python
python tkinter实现连连看游戏
Nov 16 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_flip() 删除重复数组元素专用函数
2010/05/16 PHP
PHP获取搜索引擎关键字来源的函数(支持百度和谷歌等搜索引擎)
2012/10/03 PHP
探讨file_get_contents与curl效率及稳定性的分析
2013/06/06 PHP
php+html5使用FormData对象提交表单及上传图片的方法
2015/02/11 PHP
自己写的php curl库实现整站克隆功能
2015/02/12 PHP
详解php比较操作符的安全问题
2015/12/03 PHP
PHP的PDO操作简单示例
2016/03/30 PHP
php读取XML的常见方法实例总结
2017/04/25 PHP
php获取微信共享收货地址的方法
2017/12/21 PHP
对php 判断http还是https,以及获得当前url的方法详解
2019/01/15 PHP
PHP操作XML中XPath的应用示例
2019/07/04 PHP
cocos2dx骨骼动画Armature源码剖析(一)
2015/09/08 Javascript
JavaScript编程中布尔对象的基本使用
2015/10/25 Javascript
JavaScript实现in-place思想的快速排序方法
2016/08/07 Javascript
angular2倒计时组件使用详解
2017/01/12 Javascript
解决iview打包时UglifyJs报错的问题
2018/03/07 Javascript
vue中实现先请求数据再渲染dom分享
2018/03/17 Javascript
浅谈在node.js进入文件目录的问题
2018/05/13 Javascript
vue-router懒加载的3种方式汇总
2021/02/28 Vue.js
[42:22]DOTA2上海特级锦标赛C组小组赛#1 OG VS Archon第一局
2016/02/27 DOTA
深入讲解Python函数中参数的使用及默认参数的陷阱
2016/03/13 Python
Python Requests库基本用法示例
2018/08/20 Python
python3 mmh3安装及使用方法
2019/10/09 Python
浅谈python print(xx, flush = True) 全网最清晰的解释
2020/02/21 Python
HTML5 Video/Audio播放本地文件示例介绍
2013/11/18 HTML / CSS
FILA德国官方网站:来自意大利的体育和街头服饰品牌
2019/07/19 全球购物
学校后勤岗位职责
2014/02/19 职场文书
《大江保卫战》教学反思
2014/04/11 职场文书
关于爱国的演讲稿
2014/05/07 职场文书
淘宝店策划方案
2014/06/07 职场文书
毕业生应聘求职信
2014/07/10 职场文书
总经理聘用协议书
2015/09/21 职场文书
详细总结Python常见的安全问题
2021/05/21 Python
浅谈Python数学建模之固定费用问题
2021/06/23 Python
Redis 持久化 RDB 与 AOF的执行过程
2021/11/07 Redis
在Oracle表中进行关键词搜索的过程
2022/06/10 Oracle