home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / hashlib.py < prev    next >
Encoding:
Text File  |  2005-10-28  |  3.7 KB  |  110 lines

  1. # $Id: hashlib.py 39416 2005-08-26 15:20:46Z tim_one $
  2. #
  3. #  Copyright (C) 2005   Gregory P. Smith (greg@electricrain.com)
  4. #  Licensed to PSF under a Contributor Agreement.
  5. #
  6.  
  7. __doc__ = """hashlib module - A common interface to many hash functions.
  8.  
  9. new(name, string='') - returns a new hash object implementing the
  10.                        given hash function; initializing the hash
  11.                        using the given string data.
  12.  
  13. Named constructor functions are also available, these are much faster
  14. than using new():
  15.  
  16. md5(), sha1(), sha224(), sha256(), sha384(), and sha512()
  17.  
  18. More algorithms may be available on your platform but the above are
  19. guaranteed to exist.
  20.  
  21. Choose your hash function wisely.  Some have known weaknesses.
  22. sha384 and sha512 will be slow on 32 bit platforms.
  23. """
  24.  
  25.  
  26. def __get_builtin_constructor(name):
  27.     if name in ('SHA1', 'sha1'):
  28.         import _sha
  29.         return _sha.new
  30.     elif name in ('MD5', 'md5'):
  31.         import _md5
  32.         return _md5.new
  33.     elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'):
  34.         import _sha256
  35.         bs = name[3:]
  36.         if bs == '256':
  37.             return _sha256.sha256
  38.         elif bs == '224':
  39.             return _sha256.sha224
  40.     elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
  41.         import _sha512
  42.         bs = name[3:]
  43.         if bs == '512':
  44.             return _sha512.sha512
  45.         elif bs == '384':
  46.             return _sha512.sha384
  47.  
  48.     raise ValueError, "unsupported hash type"
  49.  
  50.  
  51. def __py_new(name, string=''):
  52.     """new(name, string='') - Return a new hashing object using the named algorithm;
  53.     optionally initialized with a string.
  54.     """
  55.     return __get_builtin_constructor(name)(string)
  56.  
  57.  
  58. def __hash_new(name, string=''):
  59.     """new(name, string='') - Return a new hashing object using the named algorithm;
  60.     optionally initialized with a string.
  61.     """
  62.     try:
  63.         return _hashlib.new(name, string)
  64.     except ValueError:
  65.         # If the _hashlib module (OpenSSL) doesn't support the named
  66.         # hash, try using our builtin implementations.
  67.         # This allows for SHA224/256 and SHA384/512 support even though
  68.         # the OpenSSL library prior to 0.9.8 doesn't provide them.
  69.         return __get_builtin_constructor(name)(string)
  70.  
  71.  
  72. try:
  73.     import _hashlib
  74.     # use the wrapper of the C implementation
  75.     new = __hash_new
  76.  
  77.     for opensslFuncName in filter(lambda n: n.startswith('openssl_'), dir(_hashlib)):
  78.         funcName = opensslFuncName[len('openssl_'):]
  79.         try:
  80.             # try them all, some may not work due to the OpenSSL
  81.             # version not supporting that algorithm.
  82.             f = getattr(_hashlib, opensslFuncName)
  83.             f()
  84.             # Use the C function directly (very fast)
  85.             exec funcName + ' = f'
  86.         except ValueError:
  87.             try:
  88.                 # Use the builtin implementation directly (fast)
  89.                 exec funcName + ' = __get_builtin_constructor(funcName)'
  90.             except ValueError:
  91.                 # this one has no builtin implementation, don't define it
  92.                 pass
  93.     # clean up our locals
  94.     del f
  95.     del opensslFuncName
  96.     del funcName
  97.  
  98. except ImportError:
  99.     # We don't have the _hashlib OpenSSL module?
  100.     # use the built in legacy interfaces via a wrapper function
  101.     new = __py_new
  102.  
  103.     # lookup the C function to use directly for the named constructors
  104.     md5 = __get_builtin_constructor('md5')
  105.     sha1 = __get_builtin_constructor('sha1')
  106.     sha224 = __get_builtin_constructor('sha224')
  107.     sha256 = __get_builtin_constructor('sha256')
  108.     sha384 = __get_builtin_constructor('sha384')
  109.     sha512 = __get_builtin_constructor('sha512')
  110.