annotate Lib/hashlib.py @ 70817:dd775b1561ba

Fix markup.
author Georg Brandl <georg@python.org>
date Tue, 14 Jun 2011 21:09:55 +0200
parents c238bddf3596
children c97cfe04880f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
64488
49441e88ccc1 hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents: 59212
diff changeset
1 # Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
2 # Licensed to PSF under a Contributor Agreement.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
3 #
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
4
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
5 __doc__ = """hashlib module - A common interface to many hash functions.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
6
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42849
diff changeset
7 new(name, data=b'') - returns a new hash object implementing the
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42849
diff changeset
8 given hash function; initializing the hash
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42849
diff changeset
9 using the given binary data.
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
10
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents: 43020
diff changeset
11 Named constructor functions are also available, these are faster
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents: 43020
diff changeset
12 than using new(name):
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
13
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
14 md5(), sha1(), sha224(), sha256(), sha384(), and sha512()
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
15
64488
49441e88ccc1 hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents: 59212
diff changeset
16 More algorithms may be available on your platform but the above are guaranteed
49441e88ccc1 hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents: 59212
diff changeset
17 to exist. See the algorithms_guaranteed and algorithms_available attributes
49441e88ccc1 hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents: 59212
diff changeset
18 to find out what algorithm names can be passed to new().
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
19
45931
1657f0acca78 Merged revisions 61538-61540,61556,61559-61560,61563,61565,61571,61575-61576,61580-61582,61586,61591,61593,61595,61605-61606,61613-61616,61618,61621-61623,61625,61627,61631-61634 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43294
diff changeset
20 NOTE: If you want the adler32 or crc32 hash functions they are available in
1657f0acca78 Merged revisions 61538-61540,61556,61559-61560,61563,61565,61571,61575-61576,61580-61582,61586,61591,61593,61595,61605-61606,61613-61616,61618,61621-61623,61625,61627,61631-61634 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43294
diff changeset
21 the zlib module.
1657f0acca78 Merged revisions 61538-61540,61556,61559-61560,61563,61565,61571,61575-61576,61580-61582,61586,61591,61593,61595,61605-61606,61613-61616,61618,61621-61623,61625,61627,61631-61634 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43294
diff changeset
22
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
23 Choose your hash function wisely. Some have known collision weaknesses.
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
24 sha384 and sha512 will be slow on 32 bit platforms.
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
25
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
26 Hash objects have these methods:
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents: 43020
diff changeset
27 - update(arg): Update the hash object with the bytes in arg. Repeated calls
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
28 are equivalent to a single call with the concatenation of all
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
29 the arguments.
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents: 43020
diff changeset
30 - digest(): Return the digest of the bytes passed to the update() method
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents: 43020
diff changeset
31 so far.
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents: 43020
diff changeset
32 - hexdigest(): Like digest() except the digest is returned as a unicode
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents: 43020
diff changeset
33 object of double length, containing only hexadecimal digits.
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
34 - copy(): Return a copy (clone) of the hash object. This can be used to
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
35 efficiently compute the digests of strings that share a common
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
36 initial substring.
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
37
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
38 For example, to obtain the digest of the string 'Nobody inspects the
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
39 spammish repetition':
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
40
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
41 >>> import hashlib
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
42 >>> m = hashlib.md5()
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42849
diff changeset
43 >>> m.update(b"Nobody inspects")
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42849
diff changeset
44 >>> m.update(b" the spammish repetition")
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
45 >>> m.digest()
48916
7e194b9cc0a2 issue3715: docstring representation of hex escaped string needs to be double
Gregory P. Smith <greg@mad-scientist.com>
parents: 45931
diff changeset
46 b'\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9'
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
47
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
48 More condensed:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
49
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42849
diff changeset
50 >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
51 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
52
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
53 """
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
54
55669
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
55 # This tuple and __get_builtin_constructor() must be modified if a new
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
56 # always available algorithm is added.
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
57 __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
58
67733
6f40b4709e9a Make the type consistent for hashlib algorithm constants. (Reviewed by Benjamin).
Raymond Hettinger <python@rcn.com>
parents: 64488
diff changeset
59 algorithms_guaranteed = set(__always_supported)
6f40b4709e9a Make the type consistent for hashlib algorithm constants. (Reviewed by Benjamin).
Raymond Hettinger <python@rcn.com>
parents: 64488
diff changeset
60 algorithms_available = set(__always_supported)
59212
9b4e0cd7817a Merged revisions 78528 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55669
diff changeset
61
64488
49441e88ccc1 hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents: 59212
diff changeset
62 __all__ = __always_supported + ('new', 'algorithms_guaranteed',
49441e88ccc1 hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents: 59212
diff changeset
63 'algorithms_available')
55669
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
64
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
65
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
66 def __get_builtin_constructor(name):
70122
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
67 try:
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
68 if name in ('SHA1', 'sha1'):
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
69 import _sha1
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
70 return _sha1.sha1
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
71 elif name in ('MD5', 'md5'):
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
72 import _md5
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
73 return _md5.md5
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
74 elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
75 import _sha256
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
76 bs = name[3:]
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
77 if bs == '256':
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
78 return _sha256.sha256
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
79 elif bs == '224':
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
80 return _sha256.sha224
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
81 elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
82 import _sha512
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
83 bs = name[3:]
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
84 if bs == '512':
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
85 return _sha512.sha512
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
86 elif bs == '384':
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
87 return _sha512.sha384
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 67733
diff changeset
88 except ImportError:
70127
c238bddf3596 fix whitespace
Gregory P. Smith <greg@krypto.org>
parents: 70122
diff changeset
89 pass # no extension module, this hash is unsupported.
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
90
55669
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
91 raise ValueError('unsupported hash type %s' % name)
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
92
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
93
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
94 def __get_openssl_constructor(name):
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
95 try:
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
96 f = getattr(_hashlib, 'openssl_' + name)
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
97 # Allow the C module to raise ValueError. The function will be
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
98 # defined but the hash not actually available thanks to OpenSSL.
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
99 f()
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
100 # Use the C function directly (very fast)
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
101 return f
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
102 except (AttributeError, ValueError):
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
103 return __get_builtin_constructor(name)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
104
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
105
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42849
diff changeset
106 def __py_new(name, data=b''):
43294
78f1c329c983 Adds stand alone _md5 and _sha1 modules for use by hashlib on systems
Gregory P. Smith <greg@mad-scientist.com>
parents: 43020
diff changeset
107 """new(name, data=b'') - Return a new hashing object using the named algorithm;
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42849
diff changeset
108 optionally initialized with data (which must be bytes).
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
109 """
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42849
diff changeset
110 return __get_builtin_constructor(name)(data)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
111
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
112
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42849
diff changeset
113 def __hash_new(name, data=b''):
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42849
diff changeset
114 """new(name, data=b'') - Return a new hashing object using the named algorithm;
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42849
diff changeset
115 optionally initialized with data (which must be bytes).
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
116 """
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
117 try:
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42849
diff changeset
118 return _hashlib.new(name, data)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
119 except ValueError:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
120 # If the _hashlib module (OpenSSL) doesn't support the named
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
121 # hash, try using our builtin implementations.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
122 # This allows for SHA224/256 and SHA384/512 support even though
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
123 # the OpenSSL library prior to 0.9.8 doesn't provide them.
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42849
diff changeset
124 return __get_builtin_constructor(name)(data)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
125
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
126
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
127 try:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
128 import _hashlib
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
129 new = __hash_new
55669
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
130 __get_hash = __get_openssl_constructor
64488
49441e88ccc1 hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents: 59212
diff changeset
131 algorithms_available = algorithms_available.union(
49441e88ccc1 hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents: 59212
diff changeset
132 _hashlib.openssl_md_meth_names)
55669
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
133 except ImportError:
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
134 new = __py_new
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
135 __get_hash = __get_builtin_constructor
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
136
55669
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
137 for __func_name in __always_supported:
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
138 # try them all, some may not work due to the OpenSSL
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
139 # version not supporting that algorithm.
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
140 try:
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
141 globals()[__func_name] = __get_hash(__func_name)
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
142 except ValueError:
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
143 import logging
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
144 logging.exception('code for hash %s was not found.', __func_name)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
145
55669
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
146 # Cleanup locals()
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
147 del __always_supported, __func_name, __get_hash
da21b480ff05 Merged revisions 74479 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 48916
diff changeset
148 del __py_new, __hash_new, __get_openssl_constructor