專欄地址:每週一個 Python 模塊html
hashlib
模塊定義了用於訪問不一樣加密散列算法的 API。要使用特定的哈希算法,須要先用適當的構造函數或new()
建立哈希對象。而後,不管使用何種算法,對象都使用相同的 API。python
因爲hashlib
受 OpenSSL 「支持」,所以該庫提供的全部算法均可用,包括:git
- MD5
- SHA1
- SHA224
- SHA256
- SHA384
- SHA512
有些算法可用於全部平臺,有些算法依賴於底層庫。對於每一個列表,分別查看 algorithms_guaranteed
和algorithms_available
函數。github
import hashlib
print('Guaranteed:\n{}\n'.format(', '.join(sorted(hashlib.algorithms_guaranteed))))
print('Available:\n{}'.format(', '.join(sorted(hashlib.algorithms_available))))
# output
# Guaranteed:
# blake2b, blake2s, md5, sha1, sha224, sha256, sha384, sha3_224,
# sha3_256, sha3_384, sha3_512, sha512, shake_128, shake_256
#
# Available:
# BLAKE2b512, BLAKE2s256, MD4, MD5, MD5 - SHA1, RIPEMD160, SHA1,
# SHA224, SHA256, SHA384, SHA512, blake2b, blake2b512, blake2s,
# blake2s256, md4, md5, md5 - sha1, ripemd160, sha1, sha224, sha256,
# sha384, sha3_224, sha3_256, sha3_384, sha3_512, sha512,
# shake_128, shake_256, whirlpool
複製代碼
本節中的全部示例都使用相同的示例數據:算法
# hashlib_data.py
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.'''
複製代碼
要計算數據塊(此處爲轉換爲字節字符串的 unicode 字符串)的 MD5 哈希或摘要,首先建立哈希對象,而後添加數據並調用 digest()
或 hexdigest()
。ide
import hashlib
from hashlib_data import lorem
h = hashlib.md5()
h.update(lorem.encode('utf-8'))
print(h.hexdigest()) # 3f2fd2c9e25d60fb0fa5d593b802b7a8
複製代碼
此例使用 hexdigest()
方法而不是 digest()
,由於輸出已格式化,所以能夠清晰地打印。若是二進制摘要值能夠接受,請使用digest()
。函數
SHA1 摘要以相同的方式計算。ui
import hashlib
from hashlib_data import lorem
h = hashlib.sha1()
h.update(lorem.encode('utf-8'))
print(h.hexdigest()) # ea360b288b3dd178fe2625f55b2959bf1dba6eef
複製代碼
摘要值在此示例中是不一樣的,由於算法從 MD5 更改成 SHA1。加密
有時,在字符串中按名稱引用算法比經過直接使用構造函數更方便。例如,將哈希類型存儲在配置文件中。在這種狀況下,用 new()
建立哈希對象。spa
# hashlib_new.py
import argparse
import hashlib
import sys
from hashlib_data import lorem
parser = argparse.ArgumentParser('hashlib demo')
parser.add_argument(
'hash_name',
choices=hashlib.algorithms_available,
help='the name of the hash algorithm to use',
)
parser.add_argument(
'data',
nargs='?',
default=lorem,
help='the input data to hash, defaults to lorem ipsum',
)
args = parser.parse_args()
h = hashlib.new(args.hash_name)
h.update(args.data.encode('utf-8'))
print(h.hexdigest())
# output
# $ python3 hashlib_new.py sha1
# ea360b288b3dd178fe2625f55b2959bf1dba6eef
#
# $ python3 hashlib_new.py sha256
#
# 3c887cc71c67949df29568119cc646f46b9cd2c2b39d456065646bc2fc09ffd8
#
# $ python3 hashlib_new.py sha512
#
# a7e53384eb9bb4251a19571450465d51809e0b7046101b87c4faef96b9bc904cf7f90
# 035f444952dfd9f6084eeee2457433f3ade614712f42f80960b2fca43ff
#
# $ python3 hashlib_new.py md5
#
# 3f2fd2c9e25d60fb0fa5d593b802b7a8
複製代碼
update()
能夠重複調用哈希計算器的方法。每次,摘要都會根據輸入的附加文本進行更新。逐步更新比將整個文件讀入內存更有效,併產生相同的結果。
import hashlib
from hashlib_data import lorem
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) # All at once : 3f2fd2c9e25d60fb0fa5d593b802b7a8
print('Line by line:', line_by_line) # Line by line: 3f2fd2c9e25d60fb0fa5d593b802b7a8
print('Same :', (all_at_once == line_by_line)) # Same : True
複製代碼
相關文檔: