dnspython: Update to newer upstream snapshot.
[Samba/gebeck_regimport.git] / lib / dnspython / dns / hash.py
blob7bd5ae598008d3affdbda15edf7eb2a9c32ebad6
1 # Copyright (C) 2010 Nominum, Inc.
3 # Permission to use, copy, modify, and distribute this software and its
4 # documentation for any purpose with or without fee is hereby granted,
5 # provided that the above copyright notice and this permission notice
6 # appear in all copies.
8 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 """Hashing backwards compatibility wrapper"""
18 import sys
20 _hashes = None
22 def _need_later_python(alg):
23 def func(*args, **kwargs):
24 raise NotImplementedError("TSIG algorithm " + alg +
25 " requires Python 2.5.2 or later")
26 return func
28 def _setup():
29 global _hashes
30 _hashes = {}
31 try:
32 import hashlib
33 _hashes['MD5'] = hashlib.md5
34 _hashes['SHA1'] = hashlib.sha1
35 _hashes['SHA224'] = hashlib.sha224
36 _hashes['SHA256'] = hashlib.sha256
37 if sys.hexversion >= 0x02050200:
38 _hashes['SHA384'] = hashlib.sha384
39 _hashes['SHA512'] = hashlib.sha512
40 else:
41 _hashes['SHA384'] = _need_later_python('SHA384')
42 _hashes['SHA512'] = _need_later_python('SHA512')
44 if sys.hexversion < 0x02050000:
45 # hashlib doesn't conform to PEP 247: API for
46 # Cryptographic Hash Functions, which hmac before python
47 # 2.5 requires, so add the necessary items.
48 class HashlibWrapper:
49 def __init__(self, basehash):
50 self.basehash = basehash
51 self.digest_size = self.basehash().digest_size
53 def new(self, *args, **kwargs):
54 return self.basehash(*args, **kwargs)
56 for name in _hashes:
57 _hashes[name] = HashlibWrapper(_hashes[name])
59 except ImportError:
60 import md5, sha
61 _hashes['MD5'] = md5
62 _hashes['SHA1'] = sha
64 def get(algorithm):
65 if _hashes is None:
66 _setup()
67 return _hashes[algorithm.upper()]