I. Overview of HASHLIB
Cryptographic Services involved: 14. Cryptographic Services
Where Hashlib is involved in secure hashing and message digests, provides several different cryptographic algorithm excuses, such as SHA1, SHA224, SHA256, SHA384, SHA512, MD5, and so on.
Ii. Quick Start
Import HASHLIBM = Hashlib.md5 () #创建hash对象, MD5: (message-digest algorithm 5) message digest algorithm, draw a 128-bit ciphertext print m #<md5 HASH Object @ 000000000254adf0>m.update (' Beginman ') #更新哈希对象以字符串参数print m.digest () #返回摘要, as binary data string value print M.hexdigest () #返回十六进制数字字符串 0b28251e684dfbd9102f8b6f0281c0c5print m.digest_size #16print m.block_size #64
Use new () to create a hash object that specifies the encryption mode
New (name, string= ")" "" " Return a new hashing object using the named algorithm; Optionally initialized with a string. "" "
h = hashlib.new (' MD5 ') print H #<md5 HASH Object @ 000000000260bdb0>h2 = hashlib.new (' ripemd160 ', ' What ') print H2 #<ripemd160 HASH Object @ 000000000271b9f0>h.update (' Beginman ') print h.hexdigest () # 666fc5baa93a7fb207c5bfff03b67732# equivalent s = hashlib.md5 () s.update (' Beginman ') print s.hexdigest () # 666fc5baa93a7fb207c5bfff03b67732print h2.hexdigest () #9c1185a5c5e9fc54612808977ee8f548b2258d31
Three, Common properties
#属性print hashlib.algorithms # (' MD5 ', ' SHA1 ', ' sha224 ', ' sha256 ', ' sha384 ', ' sha512 ') list all cryptographic algorithms print H.digest_ size #16 The amount of bytes produced by the hash. Print H.block_size #64 the internal block size of the hash algorithm in bytes.
Iv. Common methods
Hash.update (ARG)
Update the hash object with a string argument, m.update (a) if the same hash object repeats the method call; M.update (b) is equivalent to m.update (a+b).
Hash.digest ()
Returns a digest, as a binary data string value,
Hash.hexdigest ()
Returns a summary, as a hexadecimal data string value,
Hash.copy ()
Copy
V. Examples of applications
Registration, login, file upload, album encryption ....
Import datetimekey_value = '/beginman/' now = Datetime.datetime.now () m = hashlib.md5 () str = '%s%s '% (Key_value, Now.strftime ("%y%m%d")) m.update (str) value = M.hexdigest () print value #8db42d3e90b41105ed061b8347a7c850
[Go] Python module learning hashlib