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

 更新时间:2020年03月30日 15:51:06   作者:爱编程的小灰灰  
这篇文章主要介绍了Python3 hashlib密码散列算法原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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()。

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算法不同。

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))

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

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

相关文章

  • 浅谈pandas中shift和diff函数关系

    浅谈pandas中shift和diff函数关系

    下面小编就为大家分享一篇浅谈pandas中shift和diff函数关系,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • 在python里从协程返回一个值的示例

    在python里从协程返回一个值的示例

    今天小编就为大家分享一篇在python里从协程返回一个值的示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-02-02
  • 用Python输出一个杨辉三角的例子

    用Python输出一个杨辉三角的例子

    这篇文章主要介绍了用Python和erlang输出一个杨辉三角的例子,同时还提供了一个erlang版杨辉三角,需要的朋友可以参考下
    2014-06-06
  • Python发送邮件测试报告操作实例详解

    Python发送邮件测试报告操作实例详解

    这篇文章主要介绍了Python发送邮件测试报告操作,结合实例形式较为详细的分析了Python邮件发送相关模块使用及操作注意事项,需要的朋友可以参考下
    2018-12-12
  • tensorflow实现加载mnist数据集

    tensorflow实现加载mnist数据集

    这篇文章主要为大家详细介绍了tensorflow实现加载mnist数据集,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-09-09
  • Pyspider进行API接口抓取和数据采集的实现

    Pyspider进行API接口抓取和数据采集的实现

    Pyspider是一个基于Python的强大的网络爬虫框架,它提供了丰富的功能和灵活的扩展性,使我们可以轻松地进行数据的抓取和处理,本文主要介绍了Pyspider进行API接口抓取和数据采集的实现,感兴趣的可以了解一下
    2023-09-09
  • Python selenium页面加载慢超时的解决方案

    Python selenium页面加载慢超时的解决方案

    这篇文章主要介绍了Python selenium页面加载慢超时的解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • PHP基于phpqrcode类库生成二维码过程解析

    PHP基于phpqrcode类库生成二维码过程解析

    这篇文章主要介绍了PHP基于phpqrcode类库生成二维码过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • python输出带颜色字体实例方法

    python输出带颜色字体实例方法

    在本篇文章里小编给大家整理了关于python输出带颜色字体实例以及相关代码,有需要的朋友们可以学习参考下。
    2019-09-09
  • python中torch.nn.identity()方法详解

    python中torch.nn.identity()方法详解

    今天看源码时遇到的这个恒等函数,就如同名字那样占位符,并没有实际操作,下面这篇文章主要给大家介绍了关于python中torch.nn.identity()方法的相关资料,需要的朋友可以参考下
    2022-03-03

最新评论