python-hashlib用法

hashlib module - A common interface to many hash functions.


new(name, data=b'', **kwargs) - returns a new hash object implementing the  given hash function; initializing the hash using the given binary data.


Named constructor functions are also available, these are faster than using new(name):

md5(), sha1(), sha224(), sha256(), sha384(), sha512(), blake2b(), blake2s(),

sha3_224, sha3_256, sha3_384, sha3_512, shake_128, and shake_256.


More algorithms may be available on your platform but the above are guaranteed to exist.  See the algorithms_guaranteed and algorithms_available attributes to find out what algorithm names can be passed to new().


NOTE: If you want the adler32 or crc32 hash functions they are available in

the zlib module.


Choose your hash function wisely.  Some have known collision weaknesses.

sha384 and sha512 will be slow on 32 bit platforms.


Hash objects have these methods:

 - update(arg): Update the hash object with the bytes in arg. Repeated calls

                are equivalent to a single call with the concatenation of all

                the arguments.

 - digest():    Return the digest of the bytes passed to the update() method

                so far.

 - hexdigest(): Like digest() except the digest is returned as a unicode

                object of double length, containing only hexadecimal digits.

 - copy():      Return a copy (clone) of the hash object. This can be used to

                efficiently compute the digests of strings that share a common

                initial substring.

 


md5:

>>> import hashlib,codecs

>>> mmd5=hashlib.md5()

>>> imei='abcd'

>>> mmd5.update(imei.encode('utf8'))

>>> print mmd5.hexdigest()

e2fc714c4727ee9395f324cd2e7f331f

>>> mm2=hashlib.new('md5')

>>> mm2.update(imei.encode('utf8'))

>>> print mm2.hexdigest()

e2fc714c4727ee9395f324cd2e7f331f

>>> print hashlib.md5(imei.encode('utf8')).hexdigest()

e2fc714c4727ee9395f324cd2e7f331f

>>> print codecs.encode(hashlib.md5(msg.encode('utf8')).digest(),'hex')

e2fc714c4727ee9395f324cd2e7f331f


sha256:

>>> msha256 = hashlib.sha256()

>>> msha256.update(imei.encode('utf8'))

>>> print msha256.hexdigest()

88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589

>>> m2sha256 = hashlib.new('sha256')

>>> m2sha256.update(imei.encode('utf8'))

>>> print m2sha256.hexdigest()

88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589

>>> print hashlib.sha256(m.encode('utf8')).hexdigest()

88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589

>>> print codecs.encode(hashlib.sha256(msg.encode('utf8')).digest(),'hex')

88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589


hmac:(以下在python3下使用的)

>>> import hmac,hashlib

>>> m='Hello, world!'

>>> k='secret'

>>> h=hmac.new(k.encode('utf8'),m.encode('utf8'),digestmod='md5')

>>> h.hexdigest()

'fa4ee7d173f2d97ee79022d1a7355bcf'

>>> print (hmac.new(k.encode('utf8'),m.encode('utf8'),digestmod='md5').hexdigest

())

fa4ee7d173f2d97ee79022d1a7355bcf

>>> import codecs

>>> print (codecs.encode(hmac.new(k.encode('utf8'),m.encode('utf8'),digestmod='m

d5').digest(),'hex'))

b'fa4ee7d173f2d97ee79022d1a7355bcf'

>>> print (str(codecs.encode(hmac.new(k.encode('utf8'),m.encode('utf8'),digestmo

d='md5').digest(),'hex').decode()))

fa4ee7d173f2d97ee79022d1a7355bcf