1 # Copyright (C) 2011 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"""
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")
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
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.
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
)
57 _hashes
[name
] = HashlibWrapper(_hashes
[name
])
67 return _hashes
[algorithm
.upper()]