hashlib.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #. Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
  2. # Licensed to PSF under a Contributor Agreement.
  3. #
  4. __doc__ = """hashlib module - A common interface to many hash functions.
  5. new(name, data=b'') - returns a new hash object implementing the
  6. given hash function; initializing the hash
  7. using the given binary data.
  8. Named constructor functions are also available, these are faster
  9. than using new(name):
  10. md5(), sha1(), sha224(), sha256(), sha384(), and sha512()
  11. More algorithms may be available on your platform but the above are guaranteed
  12. to exist. See the algorithms_guaranteed and algorithms_available attributes
  13. to find out what algorithm names can be passed to new().
  14. NOTE: If you want the adler32 or crc32 hash functions they are available in
  15. the zlib module.
  16. Choose your hash function wisely. Some have known collision weaknesses.
  17. sha384 and sha512 will be slow on 32 bit platforms.
  18. Hash objects have these methods:
  19. - update(arg): Update the hash object with the bytes in arg. Repeated calls
  20. are equivalent to a single call with the concatenation of all
  21. the arguments.
  22. - digest(): Return the digest of the bytes passed to the update() method
  23. so far.
  24. - hexdigest(): Like digest() except the digest is returned as a unicode
  25. object of double length, containing only hexadecimal digits.
  26. - copy(): Return a copy (clone) of the hash object. This can be used to
  27. efficiently compute the digests of strings that share a common
  28. initial substring.
  29. For example, to obtain the digest of the string 'Nobody inspects the
  30. spammish repetition':
  31. >>> import hashlib
  32. >>> m = hashlib.md5()
  33. >>> m.update(b"Nobody inspects")
  34. >>> m.update(b" the spammish repetition")
  35. >>> m.digest()
  36. b'\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9'
  37. More condensed:
  38. >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
  39. 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
  40. """
  41. # This tuple and __get_builtin_constructor() must be modified if a new
  42. # always available algorithm is added.
  43. __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
  44. algorithms_guaranteed = set(__always_supported)
  45. algorithms_available = set(__always_supported)
  46. __all__ = __always_supported + ('new', 'algorithms_guaranteed',
  47. 'algorithms_available', 'pbkdf2_hmac')
  48. __builtin_constructor_cache = {}
  49. def __get_builtin_constructor(name):
  50. cache = __builtin_constructor_cache
  51. constructor = cache.get(name)
  52. if constructor is not None:
  53. return constructor
  54. try:
  55. if name in ('SHA1', 'sha1'):
  56. import _sha1
  57. cache['SHA1'] = cache['sha1'] = _sha1.sha1
  58. elif name in ('MD5', 'md5'):
  59. import _md5
  60. cache['MD5'] = cache['md5'] = _md5.md5
  61. elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
  62. import _sha256
  63. cache['SHA224'] = cache['sha224'] = _sha256.sha224
  64. cache['SHA256'] = cache['sha256'] = _sha256.sha256
  65. elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
  66. import _sha512
  67. cache['SHA384'] = cache['sha384'] = _sha512.sha384
  68. cache['SHA512'] = cache['sha512'] = _sha512.sha512
  69. except ImportError:
  70. pass # no extension module, this hash is unsupported.
  71. constructor = cache.get(name)
  72. if constructor is not None:
  73. return constructor
  74. raise ValueError('unsupported hash type ' + name)
  75. def __get_openssl_constructor(name):
  76. try:
  77. f = getattr(_hashlib, 'openssl_' + name)
  78. # Allow the C module to raise ValueError. The function will be
  79. # defined but the hash not actually available thanks to OpenSSL.
  80. f()
  81. # Use the C function directly (very fast)
  82. return f
  83. except (AttributeError, ValueError):
  84. return __get_builtin_constructor(name)
  85. def __py_new(name, data=b''):
  86. """new(name, data=b'') - Return a new hashing object using the named algorithm;
  87. optionally initialized with data (which must be bytes).
  88. """
  89. return __get_builtin_constructor(name)(data)
  90. def __hash_new(name, data=b''):
  91. """new(name, data=b'') - Return a new hashing object using the named algorithm;
  92. optionally initialized with data (which must be bytes).
  93. """
  94. try:
  95. return _hashlib.new(name, data)
  96. except ValueError:
  97. # If the _hashlib module (OpenSSL) doesn't support the named
  98. # hash, try using our builtin implementations.
  99. # This allows for SHA224/256 and SHA384/512 support even though
  100. # the OpenSSL library prior to 0.9.8 doesn't provide them.
  101. return __get_builtin_constructor(name)(data)
  102. try:
  103. import _hashlib
  104. new = __hash_new
  105. __get_hash = __get_openssl_constructor
  106. algorithms_available = algorithms_available.union(
  107. _hashlib.openssl_md_meth_names)
  108. except ImportError:
  109. new = __py_new
  110. __get_hash = __get_builtin_constructor
  111. try:
  112. # OpenSSL's PKCS5_PBKDF2_HMAC requires OpenSSL 1.0+ with HMAC and SHA
  113. from _hashlib import pbkdf2_hmac
  114. except ImportError:
  115. _trans_5C = bytes((x ^ 0x5C) for x in range(256))
  116. _trans_36 = bytes((x ^ 0x36) for x in range(256))
  117. def pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None):
  118. """Password based key derivation function 2 (PKCS #5 v2.0)
  119. This Python implementations based on the hmac module about as fast
  120. as OpenSSL's PKCS5_PBKDF2_HMAC for short passwords and much faster
  121. for long passwords.
  122. """
  123. if not isinstance(hash_name, str):
  124. raise TypeError(hash_name)
  125. if not isinstance(password, (bytes, bytearray)):
  126. password = bytes(memoryview(password))
  127. if not isinstance(salt, (bytes, bytearray)):
  128. salt = bytes(memoryview(salt))
  129. # Fast inline HMAC implementation
  130. inner = new(hash_name)
  131. outer = new(hash_name)
  132. blocksize = getattr(inner, 'block_size', 64)
  133. if len(password) > blocksize:
  134. password = new(hash_name, password).digest()
  135. password = password + b'\x00' * (blocksize - len(password))
  136. inner.update(password.translate(_trans_36))
  137. outer.update(password.translate(_trans_5C))
  138. def prf(msg, inner=inner, outer=outer):
  139. # PBKDF2_HMAC uses the password as key. We can re-use the same
  140. # digest objects and just update copies to skip initialization.
  141. icpy = inner.copy()
  142. ocpy = outer.copy()
  143. icpy.update(msg)
  144. ocpy.update(icpy.digest())
  145. return ocpy.digest()
  146. if iterations < 1:
  147. raise ValueError(iterations)
  148. if dklen is None:
  149. dklen = outer.digest_size
  150. if dklen < 1:
  151. raise ValueError(dklen)
  152. dkey = b''
  153. loop = 1
  154. from_bytes = int.from_bytes
  155. while len(dkey) < dklen:
  156. prev = prf(salt + loop.to_bytes(4, 'big'))
  157. # endianess doesn't matter here as long to / from use the same
  158. rkey = int.from_bytes(prev, 'big')
  159. for i in range(iterations - 1):
  160. prev = prf(prev)
  161. # rkey = rkey ^ prev
  162. rkey ^= from_bytes(prev, 'big')
  163. loop += 1
  164. dkey += rkey.to_bytes(inner.digest_size, 'big')
  165. return dkey[:dklen]
  166. for __func_name in __always_supported:
  167. # try them all, some may not work due to the OpenSSL
  168. # version not supporting that algorithm.
  169. try:
  170. globals()[__func_name] = __get_hash(__func_name)
  171. except ValueError:
  172. import logging
  173. logging.exception('code for hash %s was not found.', __func_name)
  174. # Cleanup locals()
  175. del __always_supported, __func_name, __get_hash
  176. del __py_new, __hash_new, __get_openssl_constructor