[Python-checkins] python/dist/src/Lib hashlib.py, NONE, 1.1 md5.py, NONE, 1.1 sha.py, NONE, 1.1 hmac.py, 1.8, 1.9
greg@users.sourceforge.net
greg at users.sourceforge.net
Sun Aug 21 20:46:11 CEST 2005
- Previous message: [Python-checkins] python/dist/src/Doc/whatsnew whatsnew25.tex, 1.16, 1.17
- Next message: [Python-checkins] python/dist/src/Lib/test test_hashlib.py, NONE, 1.1 test_hashlib_speed.py, NONE, 1.1 regrtest.py, 1.170, 1.171 test_hmac.py, 1.8, 1.9
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32064/Lib
Modified Files:
hmac.py
Added Files:
hashlib.py md5.py sha.py
Log Message:
[ sf.net patch # 1121611 ]
A new hashlib module to replace the md5 and sha modules. It adds
support for additional secure hashes such as SHA-256 and SHA-512. The
hashlib module uses OpenSSL for fast platform optimized
implementations of algorithms when available. The old md5 and sha
modules still exist as wrappers around hashlib to preserve backwards
compatibility.
--- NEW FILE: hashlib.py ---
# $Id: hashlib.py,v 1.1 2005/08/21 18:46:00 greg Exp $
#
# Copyright (C) 2005 Gregory P. Smith (greg at electricrain.com)
# Licensed to PSF under a Contributor Agreement.
#
__doc__ = """hashlib module - A common interface to many hash functions.
new(name, string='') - returns a new hash object implementing the
given hash function; initializing the hash
using the given string data.
Named constructor functions are also available, these are much faster
than using new():
md5(), sha1(), sha224(), sha256(), sha384(), and sha512()
More algorithms may be available on your platform but the above are
guaranteed to exist.
Choose your hash function wisely. Some have known weaknesses.
sha384 and sha512 will be slow on 32 bit platforms.
"""
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')
--- NEW FILE: md5.py ---
# $Id: md5.py,v 1.1 2005/08/21 18:46:00 greg Exp $
#
# Copyright (C) 2005 Gregory P. Smith (greg at electricrain.com)
# Licensed to PSF under a Contributor Agreement.
from hashlib import md5
new = md5
blocksize = 1 # legacy value (wrong in any useful sense)
digest_size = 16
--- NEW FILE: sha.py ---
# $Id: sha.py,v 1.1 2005/08/21 18:46:00 greg Exp $
#
# Copyright (C) 2005 Gregory P. Smith (greg at electricrain.com)
# Licensed to PSF under a Contributor Agreement.
from hashlib import sha1 as sha
new = sha
blocksize = 1 # legacy value (wrong in any useful sense)
digest_size = 20
digestsize = 20
Index: hmac.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/hmac.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- hmac.py 20 Mar 2004 20:11:29 -0000 1.8
+++ hmac.py 21 Aug 2005 18:46:00 -0000 1.9
@@ -28,27 +28,33 @@
key: key for the keyed hash object.
msg: Initial input for the hash, if provided.
- digestmod: A module supporting PEP 247. Defaults to the md5 module.
+ digestmod: A module supporting PEP 247. *OR*
+ A hashlib constructor returning a new hash object.
+ Defaults to hashlib.md5.
"""
if key is _secret_backdoor_key: # cheap
return
if digestmod is None:
- import md5
- digestmod = md5
+ import hashlib
+ digestmod = hashlib.md5
- self.digestmod = digestmod
- self.outer = digestmod.new()
- self.inner = digestmod.new()
- self.digest_size = digestmod.digest_size
+ if callable(digestmod):
+ self.digest_cons = digestmod
+ else:
+ self.digest_cons = lambda d='': digestmod.new(d)
+
+ self.outer = self.digest_cons()
+ self.inner = self.digest_cons()
+ self.digest_size = self.inner.digest_size
blocksize = 64
ipad = "\x36" * blocksize
opad = "\x5C" * blocksize
if len(key) > blocksize:
- key = digestmod.new(key).digest()
+ key = self.digest_cons(key).digest()
key = key + chr(0) * (blocksize - len(key))
self.outer.update(_strxor(key, opad))
@@ -70,7 +76,7 @@
An update to this copy won't affect the original object.
"""
other = HMAC(_secret_backdoor_key)
- other.digestmod = self.digestmod
+ other.digest_cons = self.digest_cons
other.digest_size = self.digest_size
other.inner = self.inner.copy()
other.outer = self.outer.copy()
- Previous message: [Python-checkins] python/dist/src/Doc/whatsnew whatsnew25.tex, 1.16, 1.17
- Next message: [Python-checkins] python/dist/src/Lib/test test_hashlib.py, NONE, 1.1 test_hashlib_speed.py, NONE, 1.1 regrtest.py, 1.170, 1.171 test_hmac.py, 1.8, 1.9
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
More information about the Python-checkins
mailing list