source: vendor/python/2.5/Lib/hashlib.py

Last change on this file was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 3.6 KB
Line 
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
9new(name, string='') - returns a new hash object implementing the
10 given hash function; initializing the hash
11 using the given string data.
12
13Named constructor functions are also available, these are much faster
14than using new():
15
16md5(), sha1(), sha224(), sha256(), sha384(), and sha512()
17
18More algorithms may be available on your platform but the above are
19guaranteed to exist.
20
21Choose your hash function wisely. Some have known weaknesses.
22sha384 and sha512 will be slow on 32 bit platforms.
23"""
24
25
26def __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
51def __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