+++ /dev/null
-# $Id$\r
-#\r
-# Copyright (C) 2005 Gregory P. Smith (greg@krypto.org)\r
-# Licensed to PSF under a Contributor Agreement.\r
-#\r
-\r
-__doc__ = """hashlib module - A common interface to many hash functions.\r
-\r
-new(name, string='') - returns a new hash object implementing the\r
- given hash function; initializing the hash\r
- using the given string data.\r
-\r
-Named constructor functions are also available, these are much faster\r
-than using new():\r
-\r
-md5(), sha1(), sha224(), sha256(), sha384(), and sha512()\r
-\r
-More algorithms may be available on your platform but the above are\r
-guaranteed to exist.\r
-\r
-NOTE: If you want the adler32 or crc32 hash functions they are available in\r
-the zlib module.\r
-\r
-Choose your hash function wisely. Some have known collision weaknesses.\r
-sha384 and sha512 will be slow on 32 bit platforms.\r
-\r
-Hash objects have these methods:\r
- - update(arg): Update the hash object with the string arg. Repeated calls\r
- are equivalent to a single call with the concatenation of all\r
- the arguments.\r
- - digest(): Return the digest of the strings passed to the update() method\r
- so far. This may contain non-ASCII characters, including\r
- NUL bytes.\r
- - hexdigest(): Like digest() except the digest is returned as a string of\r
- double length, containing only hexadecimal digits.\r
- - copy(): Return a copy (clone) of the hash object. This can be used to\r
- efficiently compute the digests of strings that share a common\r
- initial substring.\r
-\r
-For example, to obtain the digest of the string 'Nobody inspects the\r
-spammish repetition':\r
-\r
- >>> import hashlib\r
- >>> m = hashlib.md5()\r
- >>> m.update("Nobody inspects")\r
- >>> m.update(" the spammish repetition")\r
- >>> m.digest()\r
- '\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9'\r
-\r
-More condensed:\r
-\r
- >>> hashlib.sha224("Nobody inspects the spammish repetition").hexdigest()\r
- 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'\r
-\r
-"""\r
-\r
-# This tuple and __get_builtin_constructor() must be modified if a new\r
-# always available algorithm is added.\r
-__always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')\r
-\r
-algorithms = __always_supported\r
-\r
-__all__ = __always_supported + ('new', 'algorithms')\r
-\r
-\r
-def __get_builtin_constructor(name):\r
- try:\r
- if name in ('SHA1', 'sha1'):\r
- import _sha\r
- return _sha.new\r
- elif name in ('MD5', 'md5'):\r
- import _md5\r
- return _md5.new\r
- elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):\r
- import _sha256\r
- bs = name[3:]\r
- if bs == '256':\r
- return _sha256.sha256\r
- elif bs == '224':\r
- return _sha256.sha224\r
- elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):\r
- import _sha512\r
- bs = name[3:]\r
- if bs == '512':\r
- return _sha512.sha512\r
- elif bs == '384':\r
- return _sha512.sha384\r
- except ImportError:\r
- pass # no extension module, this hash is unsupported.\r
-\r
- raise ValueError('unsupported hash type %s' % name)\r
-\r
-\r
-def __get_openssl_constructor(name):\r
- try:\r
- f = getattr(_hashlib, 'openssl_' + name)\r
- # Allow the C module to raise ValueError. The function will be\r
- # defined but the hash not actually available thanks to OpenSSL.\r
- f()\r
- # Use the C function directly (very fast)\r
- return f\r
- except (AttributeError, ValueError):\r
- return __get_builtin_constructor(name)\r
-\r
-\r
-def __py_new(name, string=''):\r
- """new(name, string='') - Return a new hashing object using the named algorithm;\r
- optionally initialized with a string.\r
- """\r
- return __get_builtin_constructor(name)(string)\r
-\r
-\r
-def __hash_new(name, string=''):\r
- """new(name, string='') - Return a new hashing object using the named algorithm;\r
- optionally initialized with a string.\r
- """\r
- try:\r
- return _hashlib.new(name, string)\r
- except ValueError:\r
- # If the _hashlib module (OpenSSL) doesn't support the named\r
- # hash, try using our builtin implementations.\r
- # This allows for SHA224/256 and SHA384/512 support even though\r
- # the OpenSSL library prior to 0.9.8 doesn't provide them.\r
- return __get_builtin_constructor(name)(string)\r
-\r
-\r
-try:\r
- import _hashlib\r
- new = __hash_new\r
- __get_hash = __get_openssl_constructor\r
-except ImportError:\r
- new = __py_new\r
- __get_hash = __get_builtin_constructor\r
-\r
-for __func_name in __always_supported:\r
- # try them all, some may not work due to the OpenSSL\r
- # version not supporting that algorithm.\r
- try:\r
- globals()[__func_name] = __get_hash(__func_name)\r
- except ValueError:\r
- import logging\r
- logging.exception('code for hash %s was not found.', __func_name)\r
-\r
-# Cleanup locals()\r
-del __always_supported, __func_name, __get_hash\r
-del __py_new, __hash_new, __get_openssl_constructor\r