Statistics
| Branch: | Tag: | Revision:

root / lib / utils / hash.py @ 0aee8ee9

History | View | Annotate | Download (2.4 kB)

1 f21bb4b7 Michael Hanselmann
#
2 f21bb4b7 Michael Hanselmann
#
3 f21bb4b7 Michael Hanselmann
4 f21bb4b7 Michael Hanselmann
# Copyright (C) 2006, 2007, 2010, 2011 Google Inc.
5 f21bb4b7 Michael Hanselmann
#
6 f21bb4b7 Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 f21bb4b7 Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 f21bb4b7 Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 f21bb4b7 Michael Hanselmann
# (at your option) any later version.
10 f21bb4b7 Michael Hanselmann
#
11 f21bb4b7 Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 f21bb4b7 Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 f21bb4b7 Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 f21bb4b7 Michael Hanselmann
# General Public License for more details.
15 f21bb4b7 Michael Hanselmann
#
16 f21bb4b7 Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 f21bb4b7 Michael Hanselmann
# along with this program; if not, write to the Free Software
18 f21bb4b7 Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 f21bb4b7 Michael Hanselmann
# 02110-1301, USA.
20 f21bb4b7 Michael Hanselmann
21 f21bb4b7 Michael Hanselmann
"""Utility functions for hashing.
22 f21bb4b7 Michael Hanselmann

23 f21bb4b7 Michael Hanselmann
"""
24 f21bb4b7 Michael Hanselmann
25 f21bb4b7 Michael Hanselmann
import os
26 f21bb4b7 Michael Hanselmann
import hmac
27 f21bb4b7 Michael Hanselmann
28 f21bb4b7 Michael Hanselmann
from ganeti import compat
29 f21bb4b7 Michael Hanselmann
30 f21bb4b7 Michael Hanselmann
31 f21bb4b7 Michael Hanselmann
def Sha1Hmac(key, text, salt=None):
32 f21bb4b7 Michael Hanselmann
  """Calculates the HMAC-SHA1 digest of a text.
33 f21bb4b7 Michael Hanselmann

34 f21bb4b7 Michael Hanselmann
  HMAC is defined in RFC2104.
35 f21bb4b7 Michael Hanselmann

36 f21bb4b7 Michael Hanselmann
  @type key: string
37 f21bb4b7 Michael Hanselmann
  @param key: Secret key
38 f21bb4b7 Michael Hanselmann
  @type text: string
39 f21bb4b7 Michael Hanselmann

40 f21bb4b7 Michael Hanselmann
  """
41 f21bb4b7 Michael Hanselmann
  if salt:
42 f21bb4b7 Michael Hanselmann
    salted_text = salt + text
43 f21bb4b7 Michael Hanselmann
  else:
44 f21bb4b7 Michael Hanselmann
    salted_text = text
45 f21bb4b7 Michael Hanselmann
46 f21bb4b7 Michael Hanselmann
  return hmac.new(key, salted_text, compat.sha1).hexdigest()
47 f21bb4b7 Michael Hanselmann
48 f21bb4b7 Michael Hanselmann
49 f21bb4b7 Michael Hanselmann
def VerifySha1Hmac(key, text, digest, salt=None):
50 f21bb4b7 Michael Hanselmann
  """Verifies the HMAC-SHA1 digest of a text.
51 f21bb4b7 Michael Hanselmann

52 f21bb4b7 Michael Hanselmann
  HMAC is defined in RFC2104.
53 f21bb4b7 Michael Hanselmann

54 f21bb4b7 Michael Hanselmann
  @type key: string
55 f21bb4b7 Michael Hanselmann
  @param key: Secret key
56 f21bb4b7 Michael Hanselmann
  @type text: string
57 f21bb4b7 Michael Hanselmann
  @type digest: string
58 f21bb4b7 Michael Hanselmann
  @param digest: Expected digest
59 f21bb4b7 Michael Hanselmann
  @rtype: bool
60 f21bb4b7 Michael Hanselmann
  @return: Whether HMAC-SHA1 digest matches
61 f21bb4b7 Michael Hanselmann

62 f21bb4b7 Michael Hanselmann
  """
63 f21bb4b7 Michael Hanselmann
  return digest.lower() == Sha1Hmac(key, text, salt=salt).lower()
64 f21bb4b7 Michael Hanselmann
65 f21bb4b7 Michael Hanselmann
66 f21bb4b7 Michael Hanselmann
def _FingerprintFile(filename):
67 f21bb4b7 Michael Hanselmann
  """Compute the fingerprint of a file.
68 f21bb4b7 Michael Hanselmann

69 f21bb4b7 Michael Hanselmann
  If the file does not exist, a None will be returned
70 f21bb4b7 Michael Hanselmann
  instead.
71 f21bb4b7 Michael Hanselmann

72 f21bb4b7 Michael Hanselmann
  @type filename: str
73 f21bb4b7 Michael Hanselmann
  @param filename: the filename to checksum
74 f21bb4b7 Michael Hanselmann
  @rtype: str
75 f21bb4b7 Michael Hanselmann
  @return: the hex digest of the sha checksum of the contents
76 f21bb4b7 Michael Hanselmann
      of the file
77 f21bb4b7 Michael Hanselmann

78 f21bb4b7 Michael Hanselmann
  """
79 f21bb4b7 Michael Hanselmann
  if not (os.path.exists(filename) and os.path.isfile(filename)):
80 f21bb4b7 Michael Hanselmann
    return None
81 f21bb4b7 Michael Hanselmann
82 f21bb4b7 Michael Hanselmann
  f = open(filename)
83 f21bb4b7 Michael Hanselmann
84 f21bb4b7 Michael Hanselmann
  fp = compat.sha1_hash()
85 f21bb4b7 Michael Hanselmann
  while True:
86 f21bb4b7 Michael Hanselmann
    data = f.read(4096)
87 f21bb4b7 Michael Hanselmann
    if not data:
88 f21bb4b7 Michael Hanselmann
      break
89 f21bb4b7 Michael Hanselmann
90 f21bb4b7 Michael Hanselmann
    fp.update(data)
91 f21bb4b7 Michael Hanselmann
92 f21bb4b7 Michael Hanselmann
  return fp.hexdigest()
93 f21bb4b7 Michael Hanselmann
94 f21bb4b7 Michael Hanselmann
95 f21bb4b7 Michael Hanselmann
def FingerprintFiles(files):
96 f21bb4b7 Michael Hanselmann
  """Compute fingerprints for a list of files.
97 f21bb4b7 Michael Hanselmann

98 f21bb4b7 Michael Hanselmann
  @type files: list
99 f21bb4b7 Michael Hanselmann
  @param files: the list of filename to fingerprint
100 f21bb4b7 Michael Hanselmann
  @rtype: dict
101 f21bb4b7 Michael Hanselmann
  @return: a dictionary filename: fingerprint, holding only
102 f21bb4b7 Michael Hanselmann
      existing files
103 f21bb4b7 Michael Hanselmann

104 f21bb4b7 Michael Hanselmann
  """
105 f21bb4b7 Michael Hanselmann
  ret = {}
106 f21bb4b7 Michael Hanselmann
107 f21bb4b7 Michael Hanselmann
  for filename in files:
108 f21bb4b7 Michael Hanselmann
    cksum = _FingerprintFile(filename)
109 f21bb4b7 Michael Hanselmann
    if cksum:
110 f21bb4b7 Michael Hanselmann
      ret[filename] = cksum
111 f21bb4b7 Michael Hanselmann
112 f21bb4b7 Michael Hanselmann
  return ret