3 # Copyright (C) 2005 Gregory P. Smith (greg@electricrain.com)
4 # Licensed to PSF under a Contributor Agreement.
7 __doc__
= """hashlib module - A common interface to many hash functions.
9 new(name, string='') - returns a new hash object implementing the
10 given hash function; initializing the hash
11 using the given string data.
13 Named constructor functions are also available, these are much faster
16 md5(), sha1(), sha224(), sha256(), sha384(), and sha512()
18 More algorithms may be available on your platform but the above are
21 Choose your hash function wisely. Some have known collision weaknesses.
22 sha384 and sha512 will be slow on 32 bit platforms.
24 Hash 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
28 - digest(): Return the digest of the strings passed to the update() method
29 so far. This may contain non-ASCII characters, including
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
37 For example, to obtain the digest of the string 'Nobody inspects the
42 >>> m.update("Nobody inspects")
43 >>> m.update(" the spammish repetition")
45 '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
49 >>> hashlib.sha224("Nobody inspects the spammish repetition").hexdigest()
50 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
55 def __get_builtin_constructor(name
):
56 if name
in ('SHA1', 'sha1'):
59 elif name
in ('MD5', 'md5'):
62 elif name
in ('SHA256', 'sha256', 'SHA224', 'sha224'):
69 elif name
in ('SHA512', 'sha512', 'SHA384', 'sha384'):
77 raise ValueError, "unsupported hash type"
80 def __py_new(name
, string
=''):
81 """new(name, string='') - Return a new hashing object using the named algorithm;
82 optionally initialized with a string.
84 return __get_builtin_constructor(name
)(string
)
87 def __hash_new(name
, string
=''):
88 """new(name, string='') - Return a new hashing object using the named algorithm;
89 optionally initialized with a string.
92 return _hashlib
.new(name
, string
)
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.
98 return __get_builtin_constructor(name
)(string
)
103 # use the wrapper of the C implementation
106 for opensslFuncName
in filter(lambda n
: n
.startswith('openssl_'), dir(_hashlib
)):
107 funcName
= opensslFuncName
[len('openssl_'):]
109 # try them all, some may not work due to the OpenSSL
110 # version not supporting that algorithm.
111 f
= getattr(_hashlib
, opensslFuncName
)
113 # Use the C function directly (very fast)
114 exec funcName
+ ' = f'
117 # Use the builtin implementation directly (fast)
118 exec funcName
+ ' = __get_builtin_constructor(funcName)'
120 # this one has no builtin implementation, don't define it
122 # clean up our locals
128 # We don't have the _hashlib OpenSSL module?
129 # use the built in legacy interfaces via a wrapper function
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')