| 1 | # $Id: hashlib.py 39416 2005-08-26 15:20:46Z tim_one $
|
|---|
| 2 | #
|
|---|
| 3 | # Copyright (C) 2005 Gregory P. Smith ([email protected])
|
|---|
| 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 |
|
|---|
|
|---|