From da5f75098cd0bdf76535d371b5ea0da786d230cf Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Tue, 7 Apr 2009 16:36:06 +0200 Subject: [PATCH] entropy.tools: add sha1 and sha256 functions --- libraries/entropy/const.py | 2 ++ libraries/entropy/tools.py | 56 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/libraries/entropy/const.py b/libraries/entropy/const.py index 10edc4540..44dfa1242 100644 --- a/libraries/entropy/const.py +++ b/libraries/entropy/const.py @@ -329,6 +329,8 @@ def const_default_settings(rootdir): # of its releated package file 'packagesmd5fileext': ".md5", 'packagessha512fileext': ".sha512", + 'packagessha256fileext': ".sha256", + 'packagessha1fileext': ".sha1", # Extension of the file that "contains" expiration mtime 'packagesexpirationfileext': ".expired", # number of days after a package will be removed from mirrors diff --git a/libraries/entropy/tools.py b/libraries/entropy/tools.py index c5d222a25..637d6f09c 100644 --- a/libraries/entropy/tools.py +++ b/libraries/entropy/tools.py @@ -485,6 +485,26 @@ def sha512(filepath): readfile.close() return m.hexdigest() +def sha256(filepath): + m = hashlib.sha256() + readfile = open(filepath) + block = readfile.read(1024) + while block: + m.update(block) + block = readfile.read(1024) + readfile.close() + return m.hexdigest() + +def sha1(filepath): + m = hashlib.sha1() + readfile = open(filepath) + block = readfile.read(1024) + while block: + m.update(block) + block = readfile.read(1024) + readfile.close() + return m.hexdigest() + def md5sum_directory(directory, get_obj = False): if not os.path.isdir(directory): raise DirectoryNotFound("DirectoryNotFound: directory just does not exist.") @@ -888,6 +908,26 @@ def create_sha512_file(filepath): f.close() return hashfile +def create_sha256_file(filepath): + sha256hash = sha256(filepath) + hashfile = filepath+etpConst['packagessha256fileext'] + f = open(hashfile,"w") + tbz2name = os.path.basename(filepath) + f.write(sha256hash+" "+filepath+"\n") + f.flush() + f.close() + return hashfile + +def create_sha1_file(filepath): + sha1hash = sha1(filepath) + hashfile = filepath+etpConst['packagessha1fileext'] + f = open(hashfile,"w") + tbz2name = os.path.basename(filepath) + f.write(sha1hash+" "+filepath+"\n") + f.flush() + f.close() + return hashfile + def compare_md5(filepath,checksum): checksum = str(checksum) result = md5sum(filepath) @@ -904,6 +944,22 @@ def compare_sha512(filepath, checksum): return True return False +def compare_sha256(filepath, checksum): + checksum = str(checksum) + result = sha256(filepath) + result = str(result) + if checksum == result: + return True + return False + +def compare_sha1(filepath, checksum): + checksum = str(checksum) + result = sha1(filepath) + result = str(result) + if checksum == result: + return True + return False + def md5string(string): m = hashlib.md5() m.update(string)