Revision f21bb4b7 lib/utils/__init__.py
b/lib/utils/__init__.py | ||
---|---|---|
46 | 46 |
import OpenSSL |
47 | 47 |
import datetime |
48 | 48 |
import calendar |
49 |
import hmac |
|
50 | 49 |
|
51 | 50 |
from cStringIO import StringIO |
52 | 51 |
|
... | ... | |
59 | 58 |
from ganeti.utils.text import * # pylint: disable-msg=W0401 |
60 | 59 |
from ganeti.utils.mlock import * # pylint: disable-msg=W0401 |
61 | 60 |
from ganeti.utils.log import * # pylint: disable-msg=W0401 |
61 |
from ganeti.utils.hash import * # pylint: disable-msg=W0401 |
|
62 | 62 |
|
63 | 63 |
_locksheld = [] |
64 | 64 |
|
... | ... | |
850 | 850 |
" '_once_lock' and '_name_sequence' attributes") |
851 | 851 |
|
852 | 852 |
|
853 |
def _FingerprintFile(filename): |
|
854 |
"""Compute the fingerprint of a file. |
|
855 |
|
|
856 |
If the file does not exist, a None will be returned |
|
857 |
instead. |
|
858 |
|
|
859 |
@type filename: str |
|
860 |
@param filename: the filename to checksum |
|
861 |
@rtype: str |
|
862 |
@return: the hex digest of the sha checksum of the contents |
|
863 |
of the file |
|
864 |
|
|
865 |
""" |
|
866 |
if not (os.path.exists(filename) and os.path.isfile(filename)): |
|
867 |
return None |
|
868 |
|
|
869 |
f = open(filename) |
|
870 |
|
|
871 |
fp = compat.sha1_hash() |
|
872 |
while True: |
|
873 |
data = f.read(4096) |
|
874 |
if not data: |
|
875 |
break |
|
876 |
|
|
877 |
fp.update(data) |
|
878 |
|
|
879 |
return fp.hexdigest() |
|
880 |
|
|
881 |
|
|
882 |
def FingerprintFiles(files): |
|
883 |
"""Compute fingerprints for a list of files. |
|
884 |
|
|
885 |
@type files: list |
|
886 |
@param files: the list of filename to fingerprint |
|
887 |
@rtype: dict |
|
888 |
@return: a dictionary filename: fingerprint, holding only |
|
889 |
existing files |
|
890 |
|
|
891 |
""" |
|
892 |
ret = {} |
|
893 |
|
|
894 |
for filename in files: |
|
895 |
cksum = _FingerprintFile(filename) |
|
896 |
if cksum: |
|
897 |
ret[filename] = cksum |
|
898 |
|
|
899 |
return ret |
|
900 |
|
|
901 |
|
|
902 | 853 |
def ForceDictType(target, key_types, allowed_values=None): |
903 | 854 |
"""Force the values of a dict to have certain types. |
904 | 855 |
|
... | ... | |
2541 | 2492 |
return (cert, salt) |
2542 | 2493 |
|
2543 | 2494 |
|
2544 |
def Sha1Hmac(key, text, salt=None): |
|
2545 |
"""Calculates the HMAC-SHA1 digest of a text. |
|
2546 |
|
|
2547 |
HMAC is defined in RFC2104. |
|
2548 |
|
|
2549 |
@type key: string |
|
2550 |
@param key: Secret key |
|
2551 |
@type text: string |
|
2552 |
|
|
2553 |
""" |
|
2554 |
if salt: |
|
2555 |
salted_text = salt + text |
|
2556 |
else: |
|
2557 |
salted_text = text |
|
2558 |
|
|
2559 |
return hmac.new(key, salted_text, compat.sha1).hexdigest() |
|
2560 |
|
|
2561 |
|
|
2562 |
def VerifySha1Hmac(key, text, digest, salt=None): |
|
2563 |
"""Verifies the HMAC-SHA1 digest of a text. |
|
2564 |
|
|
2565 |
HMAC is defined in RFC2104. |
|
2566 |
|
|
2567 |
@type key: string |
|
2568 |
@param key: Secret key |
|
2569 |
@type text: string |
|
2570 |
@type digest: string |
|
2571 |
@param digest: Expected digest |
|
2572 |
@rtype: bool |
|
2573 |
@return: Whether HMAC-SHA1 digest matches |
|
2574 |
|
|
2575 |
""" |
|
2576 |
return digest.lower() == Sha1Hmac(key, text, salt=salt).lower() |
|
2577 |
|
|
2578 |
|
|
2579 | 2495 |
def FindMatch(data, name): |
2580 | 2496 |
"""Tries to find an item in a dictionary matching a name. |
2581 | 2497 |
|
Also available in: Unified diff