hashlib.py 7.7 KB

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