diff options
author | cinap_lenrek <cinap_lenrek@felloff.net> | 2016-04-11 20:31:14 +0200 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@felloff.net> | 2016-04-11 20:31:14 +0200 |
commit | 4e04698ab6fddc1efe41b97be54fcf120810bf5d (patch) | |
tree | 38da146ade3ab5dfb26fa6ce1e986952a5b4a18d /sys/lib | |
parent | 8f4db30e7865232f9179f5850fe0e1ca759302cc (diff) |
python: remove openssl support, use ape/libsec for cryptographics hash functions
Diffstat (limited to 'sys/lib')
-rw-r--r-- | sys/lib/python/hashlib.py | 106 |
1 files changed, 21 insertions, 85 deletions
diff --git a/sys/lib/python/hashlib.py b/sys/lib/python/hashlib.py index 71f63c72a..06cc67c21 100644 --- a/sys/lib/python/hashlib.py +++ b/sys/lib/python/hashlib.py @@ -51,88 +51,24 @@ More condensed: """ - -def __get_builtin_constructor(name): - if name in ('SHA1', 'sha1'): - import _sha - return _sha.new - elif name in ('MD5', 'md5'): - import _md5 - return _md5.new - elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'): - import _sha256 - bs = name[3:] - if bs == '256': - return _sha256.sha256 - elif bs == '224': - return _sha256.sha224 -# elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'): -# import _sha512 -# bs = name[3:] -# if bs == '512': -# return _sha512.sha512 -# elif bs == '384': -# return _sha512.sha384 - - raise ValueError, "unsupported hash type" - - -def __py_new(name, string=''): - """new(name, string='') - Return a new hashing object using the named algorithm; - optionally initialized with a string. - """ - return __get_builtin_constructor(name)(string) - - -def __hash_new(name, string=''): - """new(name, string='') - Return a new hashing object using the named algorithm; - optionally initialized with a string. - """ - try: - return _hashlib.new(name, string) - except ValueError: - # If the _hashlib module (OpenSSL) doesn't support the named - # hash, try using our builtin implementations. - # This allows for SHA224/256 and SHA384/512 support even though - # the OpenSSL library prior to 0.9.8 doesn't provide them. - return __get_builtin_constructor(name)(string) - - -try: - import _hashlib - # use the wrapper of the C implementation - new = __hash_new - - for opensslFuncName in filter(lambda n: n.startswith('openssl_'), dir(_hashlib)): - funcName = opensslFuncName[len('openssl_'):] - try: - # try them all, some may not work due to the OpenSSL - # version not supporting that algorithm. - f = getattr(_hashlib, opensslFuncName) - f() - # Use the C function directly (very fast) - exec funcName + ' = f' - except ValueError: - try: - # Use the builtin implementation directly (fast) - exec funcName + ' = __get_builtin_constructor(funcName)' - except ValueError: - # this one has no builtin implementation, don't define it - pass - # clean up our locals - del f - del opensslFuncName - del funcName - -except ImportError: - # We don't have the _hashlib OpenSSL module? - # use the built in legacy interfaces via a wrapper function - new = __py_new - - # lookup the C function to use directly for the named constructors - md5 = __get_builtin_constructor('md5') - sha1 = __get_builtin_constructor('sha1') - sha224 = __get_builtin_constructor('sha224') - sha256 = __get_builtin_constructor('sha256') - sha384 = __get_builtin_constructor('sha384') - sha512 = __get_builtin_constructor('sha512') +import _sechash + +md5 = _sechash.md5 +sha1 = _sechash.sha1 +sha224 = _sechash.sha224 +sha256 = _sechash.sha256 +sha384 = _sechash.sha384 +sha512 = _sechash.sha512 + +algs = dict() +for a in [md5, sha1, sha224, sha256, sha384, sha512]: + algs[a().name.lower()] = a + +def new(name, string=''): + """new(name, string='') - Return a new hashing object using the named algorithm; + optionally initialized with a string. + """ + a = algs[name.lower()] + if a != None: + return a(string) + raise ValueError, "unsupported hash type" |