annotate Lib/hashlib.py @ 39042:c02fa187e0f4 legacy-trunk

Part of SF patch #1484695. This removes dead code. The chksum was already verified in .frombuf() on the lines above. If there was a problem an exception is raised, so there was no way this condition could have been true.
author Neal Norwitz <nnorwitz@gmail.com>
date Mon, 10 Jul 2006 00:23:17 +0000
parents 3e5c07faab11
children da79c4e9a5bb c94f37054f19
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1 # $Id$
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
2 #
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
3 # Copyright (C) 2005 Gregory P. Smith (greg@electricrain.com)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
4 # Licensed to PSF under a Contributor Agreement.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
5 #
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
6
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
7 __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
8
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
9 new(name, string='') - returns a new hash object implementing the
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
10 given hash function; initializing the hash
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
11 using the given string data.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
12
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
13 Named constructor functions are also available, these are much faster
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
14 than using new():
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
15
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
16 md5(), sha1(), sha224(), sha256(), sha384(), and sha512()
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
17
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
18 More algorithms may be available on your platform but the above are
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
19 guaranteed to exist.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
20
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
21 Choose your hash function wisely. Some have known weaknesses.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
22 sha384 and sha512 will be slow on 32 bit platforms.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
23 """
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
24
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
25
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
26 def __get_builtin_constructor(name):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
27 if name in ('SHA1', 'sha1'):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
28 import _sha
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
29 return _sha.new
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
30 elif name in ('MD5', 'md5'):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
31 import _md5
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
32 return _md5.new
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
33 elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
34 import _sha256
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
35 bs = name[3:]
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
36 if bs == '256':
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
37 return _sha256.sha256
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
38 elif bs == '224':
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
39 return _sha256.sha224
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
40 elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
41 import _sha512
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
42 bs = name[3:]
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
43 if bs == '512':
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
44 return _sha512.sha512
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
45 elif bs == '384':
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
46 return _sha512.sha384
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
47
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
48 raise ValueError, "unsupported hash type"
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
49
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
50
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
51 def __py_new(name, string=''):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
52 """new(name, string='') - Return a new hashing object using the named algorithm;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
53 optionally initialized with a string.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
54 """
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
55 return __get_builtin_constructor(name)(string)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
56
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
57
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
58 def __hash_new(name, string=''):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
59 """new(name, string='') - Return a new hashing object using the named algorithm;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
60 optionally initialized with a string.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
61 """
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
62 try:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
63 return _hashlib.new(name, string)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
64 except ValueError:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
65 # 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
66 # hash, try using our builtin implementations.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
67 # 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
68 # the OpenSSL library prior to 0.9.8 doesn't provide them.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
69 return __get_builtin_constructor(name)(string)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
70
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
71
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
72 try:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
73 import _hashlib
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
74 # use the wrapper of the C implementation
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
75 new = __hash_new
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
76
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
77 for opensslFuncName in filter(lambda n: n.startswith('openssl_'), dir(_hashlib)):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
78 funcName = opensslFuncName[len('openssl_'):]
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
79 try:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
80 # try them all, some may not work due to the OpenSSL
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
81 # version not supporting that algorithm.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
82 f = getattr(_hashlib, opensslFuncName)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
83 f()
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
84 # Use the C function directly (very fast)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
85 exec funcName + ' = f'
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
86 except ValueError:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
87 try:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
88 # Use the builtin implementation directly (fast)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
89 exec funcName + ' = __get_builtin_constructor(funcName)'
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
90 except ValueError:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
91 # this one has no builtin implementation, don't define it
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
92 pass
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
93 # clean up our locals
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
94 del f
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
95 del opensslFuncName
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
96 del funcName
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
97
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
98 except ImportError:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
99 # We don't have the _hashlib OpenSSL module?
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
100 # use the built in legacy interfaces via a wrapper function
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
101 new = __py_new
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
102
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
103 # lookup the C function to use directly for the named constructors
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
104 md5 = __get_builtin_constructor('md5')
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
105 sha1 = __get_builtin_constructor('sha1')
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
106 sha224 = __get_builtin_constructor('sha224')
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
107 sha256 = __get_builtin_constructor('sha256')
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
108 sha384 = __get_builtin_constructor('sha384')
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
109 sha512 = __get_builtin_constructor('sha512')