source: trunk/server/lib/dnspython/dns/hash.py

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 2.4 KB
Line 
1# Copyright (C) 2010 Nominum, Inc.
2#
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.
7#
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.
15
16"""Hashing backwards compatibility wrapper"""
17
18import sys
19
20_hashes = None
21
22def _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
27
28def _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')
43
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
52
53 def new(self, *args, **kwargs):
54 return self.basehash(*args, **kwargs)
55
56 for name in _hashes:
57 _hashes[name] = HashlibWrapper(_hashes[name])
58
59 except ImportError:
60 import md5, sha
61 _hashes['MD5'] = md5
62 _hashes['SHA1'] = sha
63
64def get(algorithm):
65 if _hashes is None:
66 _setup()
67 return _hashes[algorithm.upper()]
Note: See TracBrowser for help on using the repository browser.