From: Artem Bityutskiy Date: Thu, 18 Oct 2012 15:17:06 +0000 (+0300) Subject: RawImageCreator: introduce a function for hash calculation X-Git-Tag: 0.16~95 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=22b4e7075d2107e2a5138c7722ab8f101d3ba10d;p=tools%2Fmic.git RawImageCreator: introduce a function for hash calculation Introduce a separate function for hash calculations. I will need hash in bmap code, so it is nice to have a common helper for this. Change-Id: I859450803a0c6cfa9bb6db738a6931508572374f Signed-off-by: Artem Bityutskiy --- diff --git a/mic/imager/raw.py b/mic/imager/raw.py index 6503f39..620af26 100644 --- a/mic/imager/raw.py +++ b/mic/imager/raw.py @@ -409,6 +409,38 @@ class RawImageCreator(BaseImageCreator): shutil.move(src,dst) self._write_image_xml() + def _calc_hashes(self, file_name, hashes, start = 0, end = None): + """ Calculate hashes for a file. The 'file_name' argument is the file + to calculate hash functions for, 'start' and 'end' are the starting and + ending file offset to calculate the has functions for. The 'hashes' + argument is a list of haslib hash functions to calculate. Returns the + the list of calculated hash values in the hexadecimal form in the same + order as 'hashes'. """ + + if end == None: + end = os.path.getsize(file_name) + + chunk_size = 65536 + to_read = end - start; + read = 0 + + with open(file_name, "rb") as f: + f.seek(start) + + while read < to_read: + if read + chunk_size > to_read: + chunk_size = to_read - read + chunk = f.read(chunk_size) + for hash_obj in hashes: + hash_obj.update(chunk) + read += chunk_size + + result = [] + for hash_obj in hashes: + result.append(hash_obj.hexdigest()) + + return result + def _write_image_xml(self): imgarch = "i686" if self.target_arch and self.target_arch.startswith("arm"): @@ -454,7 +486,6 @@ class RawImageCreator(BaseImageCreator): for name in self.__disks.keys(): diskpath = self._full_path(self._outdir, name, \ self.__disk_format) - disk_size = os.path.getsize(diskpath) full_name = self._full_name(name, self.__disk_format) msger.debug("Generating disk signature for %s" % full_name) @@ -462,26 +493,13 @@ class RawImageCreator(BaseImageCreator): xml += " \n" \ % (full_name, self.__disk_format) - m1 = hashlib.sha1() - m2 = hashlib.sha256() - f = open(diskpath,"r") - while 1: - chunk = f.read(65536) - if not chunk: - break - m1.update(chunk) - if m2: - m2.update(chunk) - - sha1checksum = m1.hexdigest() - xml += " %s\n" \ - % sha1checksum - - if m2: - sha256checksum = m2.hexdigest() - xml += " %s\n" \ - % sha256checksum + hashes = self._calc_hashes(diskpath, + (hashlib.sha1(), hashlib.sha256())) + xml += " %s\n" \ + % hashes[0] + xml += " %s\n" \ + % hashes[1] xml += " \n" else: for name in self.__disks.keys():