blob: e24d5a11cfbb91209fdf623fbeff85829dc14ac6 [file] [log] [blame]
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001# $Id$
2#
Gregory P. Smith9406f5c2007-08-26 02:58:36 +00003# Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00004# Licensed to PSF under a Contributor Agreement.
5#
6
7__doc__ = """hashlib module - A common interface to many hash functions.
8
Guido van Rossume22905a2007-08-27 23:09:25 +00009new(name, data=b'') - returns a new hash object implementing the
10 given hash function; initializing the hash
11 using the given binary data.
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000012
13Named constructor functions are also available, these are much faster
14than using new():
15
16md5(), sha1(), sha224(), sha256(), sha384(), and sha512()
17
18More algorithms may be available on your platform but the above are
19guaranteed to exist.
20
Thomas Wouters89f507f2006-12-13 04:49:30 +000021Choose your hash function wisely. Some have known collision weaknesses.
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000022sha384 and sha512 will be slow on 32 bit platforms.
Thomas Wouters89f507f2006-12-13 04:49:30 +000023
24Hash objects have these methods:
25 - update(arg): Update the hash object with the string arg. Repeated calls
26 are equivalent to a single call with the concatenation of all
27 the arguments.
28 - digest(): Return the digest of the strings passed to the update() method
29 so far. This may contain non-ASCII characters, including
30 NUL bytes.
31 - hexdigest(): Like digest() except the digest is returned as a string of
32 double length, containing only hexadecimal digits.
33 - copy(): Return a copy (clone) of the hash object. This can be used to
34 efficiently compute the digests of strings that share a common
35 initial substring.
36
37For example, to obtain the digest of the string 'Nobody inspects the
38spammish repetition':
39
40 >>> import hashlib
41 >>> m = hashlib.md5()
Guido van Rossume22905a2007-08-27 23:09:25 +000042 >>> m.update(b"Nobody inspects")
43 >>> m.update(b" the spammish repetition")
Thomas Wouters89f507f2006-12-13 04:49:30 +000044 >>> m.digest()
Guido van Rossume22905a2007-08-27 23:09:25 +000045 b'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
Thomas Wouters89f507f2006-12-13 04:49:30 +000046
47More condensed:
48
Guido van Rossume22905a2007-08-27 23:09:25 +000049 >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
Thomas Wouters89f507f2006-12-13 04:49:30 +000050 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
51
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000052"""
53
54
55def __get_builtin_constructor(name):
56 if name in ('SHA1', 'sha1'):
57 import _sha
58 return _sha.new
59 elif name in ('MD5', 'md5'):
60 import _md5
61 return _md5.new
62 elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
63 import _sha256
64 bs = name[3:]
65 if bs == '256':
66 return _sha256.sha256
67 elif bs == '224':
68 return _sha256.sha224
69 elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
70 import _sha512
71 bs = name[3:]
72 if bs == '512':
73 return _sha512.sha512
74 elif bs == '384':
75 return _sha512.sha384
76
Collin Winterce36ad82007-08-30 01:19:48 +000077 raise ValueError("unsupported hash type")
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000078
79
Guido van Rossume22905a2007-08-27 23:09:25 +000080def __py_new(name, data=b''):
81 """new(name, data='') - Return a new hashing object using the named algorithm;
82 optionally initialized with data (which must be bytes).
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000083 """
Guido van Rossume22905a2007-08-27 23:09:25 +000084 return __get_builtin_constructor(name)(data)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000085
86
Guido van Rossume22905a2007-08-27 23:09:25 +000087def __hash_new(name, data=b''):
88 """new(name, data=b'') - Return a new hashing object using the named algorithm;
89 optionally initialized with data (which must be bytes).
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000090 """
91 try:
Guido van Rossume22905a2007-08-27 23:09:25 +000092 return _hashlib.new(name, data)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000093 except ValueError:
94 # If the _hashlib module (OpenSSL) doesn't support the named
95 # hash, try using our builtin implementations.
96 # This allows for SHA224/256 and SHA384/512 support even though
97 # the OpenSSL library prior to 0.9.8 doesn't provide them.
Guido van Rossume22905a2007-08-27 23:09:25 +000098 return __get_builtin_constructor(name)(data)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000099
100
101try:
102 import _hashlib
103 # use the wrapper of the C implementation
104 new = __hash_new
105
106 for opensslFuncName in filter(lambda n: n.startswith('openssl_'), dir(_hashlib)):
107 funcName = opensslFuncName[len('openssl_'):]
108 try:
109 # try them all, some may not work due to the OpenSSL
110 # version not supporting that algorithm.
111 f = getattr(_hashlib, opensslFuncName)
112 f()
113 # Use the C function directly (very fast)
Georg Brandl7cae87c2006-09-06 06:51:57 +0000114 exec(funcName + ' = f')
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000115 except ValueError:
116 try:
117 # Use the builtin implementation directly (fast)
Georg Brandl7cae87c2006-09-06 06:51:57 +0000118 exec(funcName + ' = __get_builtin_constructor(funcName)')
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000119 except ValueError:
120 # this one has no builtin implementation, don't define it
121 pass
122 # clean up our locals
123 del f
124 del opensslFuncName
125 del funcName
126
127except ImportError:
128 # We don't have the _hashlib OpenSSL module?
129 # use the built in legacy interfaces via a wrapper function
130 new = __py_new
131
132 # lookup the C function to use directly for the named constructors
133 md5 = __get_builtin_constructor('md5')
134 sha1 = __get_builtin_constructor('sha1')
135 sha224 = __get_builtin_constructor('sha224')
136 sha256 = __get_builtin_constructor('sha256')
137 sha384 = __get_builtin_constructor('sha384')
138 sha512 = __get_builtin_constructor('sha512')