From a50744adbc29baaff481eb9f51cb63b46152bd53 Mon Sep 17 00:00:00 2001 From: Ken Roberts Date: Tue, 3 Feb 2015 14:54:41 -0800 Subject: [PATCH] Added sha1_filecheck --- openlp/core/common/__init__.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/openlp/core/common/__init__.py b/openlp/core/common/__init__.py index e98ac7fe7..4f603c096 100644 --- a/openlp/core/common/__init__.py +++ b/openlp/core/common/__init__.py @@ -190,6 +190,32 @@ def verify_ip_address(addr): return True if verify_ipv4(addr) else verify_ipv6(addr) +def sha1_filecheck(input_file, check): + """ + Validate a downloaded file has the same SHA1 as recorded. + + :param input_file: File to test + :param check: MD5Sum to validate + :return: File MD5 == check + """ + log.debug('sha1_filecheck(file="{}", check="{}"'.format(input_file, check)) + try: + file_ = open(input_file, 'rb') + except OSError as err: + log.error('Unable to check "{}"'.format(input_file)) + log.error('Error code: {}'.format(err.errno)) + log.error('Error msg: {}'.format(err.strerror)) + return False + hash_obj = hashlib.new('sha1') + while True: + data = file_.read(1024) + if not data: + break + hash_obj.update(data) + log.debug('sha1_filecheck validating "{}"'.format(hash_obj.hexdigest())) + return hash_obj.hexdigest() == check + + def md5_filecheck(input_file, check): """ Validate a downloaded file has the same MD5 as recorded.