出自:http://blog.csdn.net/oychw/article/details/8921553
2013-05-13 磁針石
#承接軟件自動化實施與培訓等gtalk:ouyangchongwu#gmail.com qq 37391319 博客:http://blog.csdn.net/oychw
#版權所有,轉載刊登請來函聯系
# 深圳測試自動化python項目接單群113938272深圳會計軟件測試兼職 6089740
#深圳地攤群 66250781武岡洞口城步新寧鄉情群49494279
#自動化測試和python群組: http://groups.google.com/group/automation_testing_python
#參考資料:《ThePython Standard Library by Example 2011》
# http://docs.python.org/2/howto/sockets.html
9.1 hashlib
hashlib用來替換md5和sha模塊,並使他們的API一致。它由OpenSSL支持,支持如下算法:md5,sha1, sha224, sha256, sha384, sha512.
9.1.1 示例數據
importhashlib
lorem = ’’’Loremipsum dolor sit amet, consectetur adipisicing elit,
sed doeiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minimveniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip exea commodo consequat. Duis aute irure dolor in
reprehenderitin voluptate velit esse cillum dolore eu fugiat nulla
pariatur.Excepteur sint occaecat cupidatat non proident, sunt in
culpa quiofficia deserunt mollit anim id est laborum.’’’
9.1.2 MD5
fromhashlib_data import lorem
h =hashlib.md5()
h.update(lorem)
print h.hexdigest()
執行結果:
$ pythonhashlib_md5.py
1426f365574592350315090e295ac273
9.1.3 SHA1
importhashlib
fromhashlib_data import lorem
h =hashlib.sha1()
h.update(lorem)
printh.hexdigest()
執行結果:
$ pythonhashlib_sha1.py
8173396ba8a560b89a3f3e2fcc024b044bc83d0a
9.1.4 new
使用new可以指定加密的類型。
#end_pymotw_header
importhashlib
import sys
try:
hash_name = sys.argv[1]
exceptIndexError:
print 'Specify the hash name as the firstargument.'
else:
try:
data = sys.argv[2]
except IndexError:
from hashlib_data import lorem as data
h = hashlib.new(hash_name)
h.update(data)
printh.hexdigest()
執行結果:
$ pythonhashlib_new.py sha1
8173396ba8a560b89a3f3e2fcc024b044bc83d0a
$ pythonhashlib_new.py sha256
dca37495608c68ec23bbb54ab9675bf0152db63e5a51ab1061dc9982b843e767
$ pythonhashlib_new.py sha512
0e3d4bc1cbc117382fa077b147a7ff6363f6cbc7508877460f978a566a0adb6dbb4c8
b89f56514da98eb94d7135e1b7ad7fc4a2d747c02af67fcd4e571bd54de
$ pythonhashlib_new.py md5
1426f365574592350315090e295ac273
9.1.5 增量更新
文件太大的時候,可以分多次讀入:
#end_pymotw_header
importhashlib
fromhashlib_data import lorem
h =hashlib.md5()
h.update(lorem)
all_at_once =h.hexdigest()
defchunkize(size, text):
"Return parts of the text insize-based increments."
start = 0
while start < len(text):
chunk = text[start:start+size]
yield chunk
start += size
return
h =hashlib.md5()
for chunk inchunkize(64, lorem):
h.update(chunk)
line_by_line= h.hexdigest()
print 'All atonce :', all_at_once
print 'Lineby line:', line_by_line
print'Same :', (all_at_once ==line_by_line)
執行結果:
$ pythonhashlib_update.py
All at once :1426f365574592350315090e295ac273
Line by line:1426f365574592350315090e295ac273
Same : True
參考資料:
hashlib(http://docs.python.org/library/hashlib.html) The standard librarydocumentation
for thismodule.
Voidspace:IronPython and hashlib
(www.voidspace.org.uk/python/weblog/arch_d7_2006_10_07.shtml#e497)A
wrapper for hashlibthat works with IronPython.
hmac (page473) The hmac module.
OpenSSL(http://www.openssl.org/) An open source encryption toolkit.
zlib包含adler32 和 crc32哈希。
手冊中的示例:
>>> import hashlib
>>> m = hashlib.md5()
>>> m.update("Nobodyinspects")
>>> m.update("the spammish repetition")
>>> m.digest()
'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
>>> m.digest_size
16
>>> m.block_size
64
>>> hashlib.sha224("Nobodyinspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
屬性hashlib.algorithms包含支持的算法。
屬性hash.digest_size :結果hash的大小
屬性hash. block_size : hash內部塊的大小