]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | # $Id$\r |
2 | #\r | |
3 | # Copyright (C) 2005 Gregory P. Smith (greg@krypto.org)\r | |
4 | # Licensed to PSF under a Contributor Agreement.\r | |
5 | #\r | |
6 | \r | |
7 | __doc__ = """hashlib module - A common interface to many hash functions.\r | |
8 | \r | |
9 | new(name, string='') - returns a new hash object implementing the\r | |
10 | given hash function; initializing the hash\r | |
11 | using the given string data.\r | |
12 | \r | |
13 | Named constructor functions are also available, these are much faster\r | |
14 | than using new():\r | |
15 | \r | |
16 | md5(), sha1(), sha224(), sha256(), sha384(), and sha512()\r | |
17 | \r | |
18 | More algorithms may be available on your platform but the above are\r | |
19 | guaranteed to exist.\r | |
20 | \r | |
21 | NOTE: If you want the adler32 or crc32 hash functions they are available in\r | |
22 | the zlib module.\r | |
23 | \r | |
24 | Choose your hash function wisely. Some have known collision weaknesses.\r | |
25 | sha384 and sha512 will be slow on 32 bit platforms.\r | |
26 | \r | |
27 | Hash objects have these methods:\r | |
28 | - update(arg): Update the hash object with the string arg. Repeated calls\r | |
29 | are equivalent to a single call with the concatenation of all\r | |
30 | the arguments.\r | |
31 | - digest(): Return the digest of the strings passed to the update() method\r | |
32 | so far. This may contain non-ASCII characters, including\r | |
33 | NUL bytes.\r | |
34 | - hexdigest(): Like digest() except the digest is returned as a string of\r | |
35 | double length, containing only hexadecimal digits.\r | |
36 | - copy(): Return a copy (clone) of the hash object. This can be used to\r | |
37 | efficiently compute the digests of strings that share a common\r | |
38 | initial substring.\r | |
39 | \r | |
40 | For example, to obtain the digest of the string 'Nobody inspects the\r | |
41 | spammish repetition':\r | |
42 | \r | |
43 | >>> import hashlib\r | |
44 | >>> m = hashlib.md5()\r | |
45 | >>> m.update("Nobody inspects")\r | |
46 | >>> m.update(" the spammish repetition")\r | |
47 | >>> m.digest()\r | |
48 | '\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9'\r | |
49 | \r | |
50 | More condensed:\r | |
51 | \r | |
52 | >>> hashlib.sha224("Nobody inspects the spammish repetition").hexdigest()\r | |
53 | 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'\r | |
54 | \r | |
55 | """\r | |
56 | \r | |
57 | # This tuple and __get_builtin_constructor() must be modified if a new\r | |
58 | # always available algorithm is added.\r | |
59 | __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')\r | |
60 | \r | |
61 | algorithms = __always_supported\r | |
62 | \r | |
63 | __all__ = __always_supported + ('new', 'algorithms')\r | |
64 | \r | |
65 | \r | |
66 | def __get_builtin_constructor(name):\r | |
67 | try:\r | |
68 | if name in ('SHA1', 'sha1'):\r | |
69 | import _sha\r | |
70 | return _sha.new\r | |
71 | elif name in ('MD5', 'md5'):\r | |
72 | import _md5\r | |
73 | return _md5.new\r | |
74 | elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):\r | |
75 | import _sha256\r | |
76 | bs = name[3:]\r | |
77 | if bs == '256':\r | |
78 | return _sha256.sha256\r | |
79 | elif bs == '224':\r | |
80 | return _sha256.sha224\r | |
81 | elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):\r | |
82 | import _sha512\r | |
83 | bs = name[3:]\r | |
84 | if bs == '512':\r | |
85 | return _sha512.sha512\r | |
86 | elif bs == '384':\r | |
87 | return _sha512.sha384\r | |
88 | except ImportError:\r | |
89 | pass # no extension module, this hash is unsupported.\r | |
90 | \r | |
91 | raise ValueError('unsupported hash type %s' % name)\r | |
92 | \r | |
93 | \r | |
94 | def __get_openssl_constructor(name):\r | |
95 | try:\r | |
96 | f = getattr(_hashlib, 'openssl_' + name)\r | |
97 | # Allow the C module to raise ValueError. The function will be\r | |
98 | # defined but the hash not actually available thanks to OpenSSL.\r | |
99 | f()\r | |
100 | # Use the C function directly (very fast)\r | |
101 | return f\r | |
102 | except (AttributeError, ValueError):\r | |
103 | return __get_builtin_constructor(name)\r | |
104 | \r | |
105 | \r | |
106 | def __py_new(name, string=''):\r | |
107 | """new(name, string='') - Return a new hashing object using the named algorithm;\r | |
108 | optionally initialized with a string.\r | |
109 | """\r | |
110 | return __get_builtin_constructor(name)(string)\r | |
111 | \r | |
112 | \r | |
113 | def __hash_new(name, string=''):\r | |
114 | """new(name, string='') - Return a new hashing object using the named algorithm;\r | |
115 | optionally initialized with a string.\r | |
116 | """\r | |
117 | try:\r | |
118 | return _hashlib.new(name, string)\r | |
119 | except ValueError:\r | |
120 | # If the _hashlib module (OpenSSL) doesn't support the named\r | |
121 | # hash, try using our builtin implementations.\r | |
122 | # This allows for SHA224/256 and SHA384/512 support even though\r | |
123 | # the OpenSSL library prior to 0.9.8 doesn't provide them.\r | |
124 | return __get_builtin_constructor(name)(string)\r | |
125 | \r | |
126 | \r | |
127 | try:\r | |
128 | import _hashlib\r | |
129 | new = __hash_new\r | |
130 | __get_hash = __get_openssl_constructor\r | |
131 | except ImportError:\r | |
132 | new = __py_new\r | |
133 | __get_hash = __get_builtin_constructor\r | |
134 | \r | |
135 | for __func_name in __always_supported:\r | |
136 | # try them all, some may not work due to the OpenSSL\r | |
137 | # version not supporting that algorithm.\r | |
138 | try:\r | |
139 | globals()[__func_name] = __get_hash(__func_name)\r | |
140 | except ValueError:\r | |
141 | import logging\r | |
142 | logging.exception('code for hash %s was not found.', __func_name)\r | |
143 | \r | |
144 | # Cleanup locals()\r | |
145 | del __always_supported, __func_name, __get_hash\r | |
146 | del __py_new, __hash_new, __get_openssl_constructor\r |