Revision 716a32cb

b/lib/compat.py
31 31
  functools = None
32 32

  
33 33

  
34
# compat.md5_hash and compat.sha1_hash can be called to generate and md5 and a
35
# sha1 hashing modules, under python 2.4, 2.5 and 2.6, even though some changes
36
# went on. compat.sha1 is python-version specific and is used for python
37
# modules (hmac, for example) which have changed their behavior as well from
38
# one version to the other.
39
try:
40
  # Yes, we're not using the imports in this module.
41
  # pylint: disable-msg=W0611
42
  from hashlib import md5 as md5_hash
43
  from hashlib import sha1 as sha1_hash
44
  # this additional version is needed for compatibility with the hmac module
45
  sha1 = sha1_hash
46
except ImportError:
47
  from md5 import new as md5_hash
48
  import sha
49
  sha1 = sha
50
  sha1_hash = sha.new
51

  
52

  
34 53
def all(seq, pred=bool): # pylint: disable-msg=W0622
35 54
  """Returns True if pred(x) is True for every element in the iterable.
36 55

  
b/lib/http/auth.py
28 28
import binascii
29 29

  
30 30
from ganeti import utils
31
from ganeti import compat
31 32
from ganeti import http
32 33

  
33 34
from cStringIO import StringIO
34 35

  
35
try:
36
  from hashlib import md5
37
except ImportError:
38
  from md5 import new as md5
39

  
40

  
41 36
# Digest types from RFC2617
42 37
HTTP_BASIC_AUTH = "Basic"
43 38
HTTP_DIGEST_AUTH = "Digest"
......
271 266
        # There can not be a valid password for this case
272 267
        raise AssertionError("No authentication realm")
273 268

  
274
      expha1 = md5()
269
      expha1 = compat.md5_hash()
275 270
      expha1.update("%s:%s:%s" % (username, realm, password))
276 271

  
277 272
      return (expected_password.lower() == expha1.hexdigest().lower())
b/lib/utils.py
55 55
from cStringIO import StringIO
56 56

  
57 57
try:
58
  from hashlib import sha1
59
except ImportError:
60
  import sha as sha1
61

  
62
try:
63 58
  import ctypes
64 59
except ImportError:
65 60
  ctypes = None
66 61

  
67 62
from ganeti import errors
68 63
from ganeti import constants
64
from ganeti import compat
69 65

  
70 66

  
71 67
_locksheld = []
......
748 744

  
749 745
  f = open(filename)
750 746

  
751
  if callable(sha1):
752
    fp = sha1()
753
  else:
754
    fp = sha1.new()
747
  fp = compat.sha1_hash()
755 748
  while True:
756 749
    data = f.read(4096)
757 750
    if not data:
......
2799 2792
  else:
2800 2793
    salted_text = text
2801 2794

  
2802
  return hmac.new(key, salted_text, sha1).hexdigest()
2795
  return hmac.new(key, salted_text, compat.sha1).hexdigest()
2803 2796

  
2804 2797

  
2805 2798
def VerifySha1Hmac(key, text, digest, salt=None):
b/test/ganeti.utils_unittest.py
39 39
import warnings
40 40
import distutils.version
41 41
import glob
42
import md5
43 42
import errno
44 43

  
45 44
import ganeti
46 45
import testutils
47 46
from ganeti import constants
47
from ganeti import compat
48 48
from ganeti import utils
49 49
from ganeti import errors
50 50
from ganeti import serializer
......
670 670
    data = utils.ReadFile(self._TestDataFilename("cert1.pem"))
671 671
    self.assertEqual(len(data), 814)
672 672

  
673
    h = md5.new()
673
    h = compat.md5_hash()
674 674
    h.update(data)
675 675
    self.assertEqual(h.hexdigest(), "a491efb3efe56a0535f924d5f8680fd4")
676 676

  
......
679 679
                          size=100)
680 680
    self.assertEqual(len(data), 100)
681 681

  
682
    h = md5.new()
682
    h = compat.md5_hash()
683 683
    h.update(data)
684 684
    self.assertEqual(h.hexdigest(), "893772354e4e690b9efd073eed433ce7")
685 685

  

Also available in: Unified diff