[bmap-tools] [PATCH 03/20] bmaptools: preparations to switch to sha256
Artem Bityutskiy
dedekind1 at gmail.com
Wed Sep 18 03:38:15 EDT 2013
From: Artem Bityutskiy <artem.bityutskiy at intel.com>
In all places where it does not matter which exactly checksum we calculate,
substitute 'sha1' with 'chksum'. This is a preparation for the further change
where we will switch from using SHA1 checksums to using SHA256 checksums.
Change-Id: I51000ad7669d1459c5d6d5e8141e0a6d74f93a56
Signed-off-by: Artem Bityutskiy <artem.bityutskiy at intel.com>
---
bmaptools/BmapCopy.py | 56 ++++++++++++++++++++++++-------------------------
bmaptools/BmapCreate.py | 28 ++++++++++++-------------
tests/test_api_base.py | 34 +++++++++++++++---------------
3 files changed, 59 insertions(+), 59 deletions(-)
diff --git a/bmaptools/BmapCopy.py b/bmaptools/BmapCopy.py
index 4a9c8cc..0eb5c40 100644
--- a/bmaptools/BmapCopy.py
+++ b/bmaptools/BmapCopy.py
@@ -99,9 +99,9 @@ class BmapCopy:
Instead, they are initialized only by the 'copy()' method.
The 'copy()' method implements image copying. You may choose whether to
- verify the SHA1 checksum while copying or not. Note, this is done only in
- case of bmap-based copying and only if bmap contains the SHA1 checksums
- (e.g., bmap version 1.0 did not have SHA1 checksums).
+ verify the checksum while copying or not. Note, this is done only in case
+ of bmap-based copying and only if bmap contains checksums (e.g., bmap
+ version 1.0 did not have checksums support).
You may choose whether to synchronize the destination file after writing or
not. To explicitly synchronize it, use the 'sync()' method.
@@ -237,31 +237,31 @@ class BmapCopy:
def _verify_bmap_checksum(self):
"""
- This is a helper function which verifies SHA1 checksum of the bmap file.
+ This is a helper function which verifies the bmap file checksum.
"""
import mmap
- correct_sha1 = self._xml.find("BmapFileSHA1").text.strip()
+ correct_chksum = self._xml.find("BmapFileSHA1").text.strip()
- # Before verifying the shecksum, we have to substitute the SHA1 value
- # stored in the file with all zeroes. For these purposes we create
- # private memory mapping of the bmap file.
+ # Before verifying the shecksum, we have to substitute the checksum
+ # value stored in the file with all zeroes. For these purposes we
+ # create private memory mapping of the bmap file.
mapped_bmap = mmap.mmap(self._f_bmap.fileno(), 0,
access = mmap.ACCESS_COPY)
- sha1_pos = mapped_bmap.find(correct_sha1)
- assert sha1_pos != -1
+ chksum_pos = mapped_bmap.find(correct_chksum)
+ assert chksum_pos != -1
- mapped_bmap[sha1_pos:sha1_pos + 40] = '0' * 40
- calculated_sha1 = hashlib.sha1(mapped_bmap).hexdigest()
+ mapped_bmap[chksum_pos:chksum_pos + 40] = '0' * 40
+ calculated_chksum = hashlib.sha1(mapped_bmap).hexdigest()
mapped_bmap.close()
- if calculated_sha1 != correct_sha1:
+ if calculated_chksum != correct_chksum:
raise Error("checksum mismatch for bmap file '%s': calculated "
"'%s', should be '%s'"
- % (self._bmap_path, calculated_sha1, correct_sha1))
+ % (self._bmap_path, calculated_chksum, correct_chksum))
def _parse_bmap(self):
"""
@@ -348,12 +348,12 @@ class BmapCopy:
def _get_block_ranges(self):
"""
This is a helper generator that parses the bmap XML file and for each
- block range in the XML file it yields ('first', 'last', 'sha1') tuples,
- where:
+ block range in the XML file it yields ('first', 'last', 'chksum')
+ tuples, where:
* 'first' is the first block of the range;
* 'last' is the last block of the range;
- * 'sha1' is the SHA1 checksum of the range ('None' is used if it is
- missing.
+ * 'chksum' is the checksum of the range ('None' is used if it is
+ missing).
If there is no bmap file, the generator just yields a single range
for entire image file. If the image size is unknown, the generator
@@ -393,11 +393,11 @@ class BmapCopy:
last = first
if 'sha1' in xml_element.attrib:
- sha1 = xml_element.attrib['sha1']
+ chksum = xml_element.attrib['sha1']
else:
- sha1 = None
+ chksum = None
- yield (first, last, sha1)
+ yield (first, last, chksum)
def _get_batches(self, first, last):
"""
@@ -433,8 +433,8 @@ class BmapCopy:
"""
try:
- for (first, last, sha1) in self._get_block_ranges():
- if verify and sha1:
+ for (first, last, chksum) in self._get_block_ranges():
+ if verify and chksum:
hash_obj = hashlib.new('sha1')
self._f_image.seek(first * self.block_size)
@@ -452,18 +452,18 @@ class BmapCopy:
self._batch_queue.put(None)
return
- if verify and sha1:
+ if verify and chksum:
hash_obj.update(buf)
blocks = (len(buf) + self.block_size - 1) / self.block_size
self._batch_queue.put(("range", start, start + blocks - 1,
buf))
- if verify and sha1 and hash_obj.hexdigest() != sha1:
+ if verify and chksum and hash_obj.hexdigest() != chksum:
raise Error("checksum mismatch for blocks range %d-%d: "
"calculated %s, should be %s (image file %s)"
% (first, last, hash_obj.hexdigest(),
- sha1, self._image_path))
+ chksum, self._image_path))
# Silence pylint warning about catching too general exception
# pylint: disable=W0703
except Exception:
@@ -478,8 +478,8 @@ class BmapCopy:
"""
Copy the image to the destination file using bmap. The 'sync' argument
defines whether the destination file has to be synchronized upon
- return. The 'verify' argument defines whether the SHA1 checksum has to
- be verified while copying.
+ return. The 'verify' argument defines whether the checksum has to be
+ verified while copying.
"""
# Create the queue for block batches and start the reader thread, which
diff --git a/bmaptools/BmapCreate.py b/bmaptools/BmapCreate.py
index b96224a..2017a39 100644
--- a/bmaptools/BmapCreate.py
+++ b/bmaptools/BmapCreate.py
@@ -126,7 +126,7 @@ class BmapCreate:
self._mapped_count_pos1 = None
self._mapped_count_pos2 = None
- self._sha1_pos = None
+ self._chksum_pos = None
self._f_image_needs_close = False
self._f_bmap_needs_close = False
@@ -218,7 +218,7 @@ class BmapCreate:
xml += " <BmapFileSHA1> "
self._f_bmap.write(xml)
- self._sha1_pos = self._f_bmap.tell()
+ self._chksum_pos = self._f_bmap.tell()
xml = "0" * 40 + " </BmapFileSHA1>\n\n"
xml += " <!-- The block map which consists of elements which may either be a\n"
@@ -249,14 +249,14 @@ class BmapCreate:
self._f_bmap.write("%u" % self.mapped_cnt)
self._f_bmap.seek(0)
- sha1 = hashlib.sha1(self._f_bmap.read()).hexdigest()
- self._f_bmap.seek(self._sha1_pos)
- self._f_bmap.write("%s" % sha1)
+ chksum = hashlib.sha1(self._f_bmap.read()).hexdigest()
+ self._f_bmap.seek(self._chksum_pos)
+ self._f_bmap.write("%s" % chksum)
- def _calculate_sha1(self, first, last):
+ def _calculate_chksum(self, first, last):
"""
- A helper function which calculates SHA1 checksum for the range of
- blocks of the image file: from block 'first' to block 'last'.
+ A helper function which calculates checksum for the range of blocks of
+ the image file: from block 'first' to block 'last'.
"""
start = first * self.block_size
@@ -281,7 +281,7 @@ class BmapCreate:
def generate(self, include_checksums=True):
"""
Generate bmap for the image file. If 'include_checksums' is 'True',
- also generate SHA1 checksums for block ranges.
+ also generate checksums for block ranges.
"""
# Save image file position in order to restore it at the end
@@ -295,17 +295,17 @@ class BmapCreate:
for first, last in self.fiemap.get_mapped_ranges(0, self.blocks_cnt):
self.mapped_cnt += last - first + 1
if include_checksums:
- sha1 = self._calculate_sha1(first, last)
- sha1 = " sha1=\"%s\"" % sha1
+ chksum = self._calculate_chksum(first, last)
+ chksum = " sha1=\"%s\"" % chksum
else:
- sha1 = ""
+ chksum = ""
if first != last:
self._f_bmap.write(" <Range%s> %s-%s </Range>\n"
- % (sha1, first, last))
+ % (chksum, first, last))
else:
self._f_bmap.write(" <Range%s> %s </Range>\n"
- % (sha1, first))
+ % (chksum, first))
self.mapped_size = self.mapped_cnt * self.block_size
self.mapped_size_human = human_size(self.mapped_size)
diff --git a/tests/test_api_base.py b/tests/test_api_base.py
index e814fa0..a98b9b2 100644
--- a/tests/test_api_base.py
+++ b/tests/test_api_base.py
@@ -136,9 +136,9 @@ def _generate_compressed_files(file_obj, delete=True):
yield tbz2_file_obj.name
tmp_file_obj.close()
-def _calculate_sha1(file_obj):
+def _calculate_chksum(file_obj):
"""
- Calculates SHA1 checksum for the contents of file object 'file_obj'.
+ Calculates checksum for the contents of file object 'file_obj'.
"""
file_obj.seek(0)
@@ -154,7 +154,7 @@ def _calculate_sha1(file_obj):
return hash_obj.hexdigest()
-def _copy_image(image, f_dest, f_bmap, image_sha1, image_size):
+def _copy_image(image, f_dest, f_bmap, image_chksum, image_size):
"""
Copy image 'image' using bmap 'f_bmap' to the destination file 'f_dest'.
"""
@@ -177,7 +177,7 @@ def _copy_image(image, f_dest, f_bmap, image_sha1, image_size):
# Compare the original file and the copy are identical
f_dest.seek(0)
- assert _calculate_sha1(f_dest) == image_sha1
+ assert _calculate_chksum(f_dest) == image_chksum
if not hasattr(image, "read"):
f_image.close()
@@ -213,7 +213,7 @@ def _do_test(f_image, image_size, delete=True):
delete=delete, dir=directory,
suffix=".bmap2")
- image_sha1 = _calculate_sha1(f_image)
+ image_chksum = _calculate_chksum(f_image)
#
# Pass 1: generate the bmap, copy and compare
@@ -223,7 +223,7 @@ def _do_test(f_image, image_size, delete=True):
creator = BmapCreate.BmapCreate(f_image.name, f_bmap1.name)
creator.generate()
- _copy_image(f_image, f_copy, f_bmap1, image_sha1, image_size)
+ _copy_image(f_image, f_copy, f_bmap1, image_chksum, image_size)
# Make sure that holes in the copy are identical to holes in the random
# sparse file.
@@ -235,7 +235,7 @@ def _do_test(f_image, image_size, delete=True):
creator = BmapCreate.BmapCreate(f_image, f_bmap2)
creator.generate()
- _copy_image(f_image, f_copy, f_bmap2, image_sha1, image_size)
+ _copy_image(f_image, f_copy, f_bmap2, image_chksum, image_size)
_compare_holes(f_image, f_copy)
# Make sure the bmap files generated at pass 1 and pass 2 are identical
@@ -246,36 +246,36 @@ def _do_test(f_image, image_size, delete=True):
#
for compressed in _generate_compressed_files(f_image, delete=delete):
- _copy_image(compressed, f_copy, f_bmap1, image_sha1, image_size)
+ _copy_image(compressed, f_copy, f_bmap1, image_chksum, image_size)
# Test without setting the size
- _copy_image(compressed, f_copy, f_bmap1, image_sha1, None)
+ _copy_image(compressed, f_copy, f_bmap1, image_chksum, None)
# Append a "file:" prefixe to make BmapCopy use urllib
compressed = "file:" + compressed
- _copy_image(compressed, f_copy, f_bmap1, image_sha1, image_size)
- _copy_image(compressed, f_copy, f_bmap1, image_sha1, None)
+ _copy_image(compressed, f_copy, f_bmap1, image_chksum, image_size)
+ _copy_image(compressed, f_copy, f_bmap1, image_chksum, None)
#
# Pass 5: copy without bmap and make sure it is identical to the original
# file.
- _copy_image(f_image, f_copy, None, image_sha1, image_size)
- _copy_image(f_image, f_copy, None, image_sha1, None)
+ _copy_image(f_image, f_copy, None, image_chksum, image_size)
+ _copy_image(f_image, f_copy, None, image_chksum, None)
#
# Pass 6: test compressed files copying without bmap
#
for compressed in _generate_compressed_files(f_image, delete=delete):
- _copy_image(compressed, f_copy, f_bmap1, image_sha1, image_size)
+ _copy_image(compressed, f_copy, f_bmap1, image_chksum, image_size)
# Test without setting the size
- _copy_image(compressed, f_copy, f_bmap1, image_sha1, None)
+ _copy_image(compressed, f_copy, f_bmap1, image_chksum, None)
# Append a "file:" prefixe to make BmapCopy use urllib
- _copy_image(compressed, f_copy, f_bmap1, image_sha1, image_size)
- _copy_image(compressed, f_copy, f_bmap1, image_sha1, None)
+ _copy_image(compressed, f_copy, f_bmap1, image_chksum, image_size)
+ _copy_image(compressed, f_copy, f_bmap1, image_chksum, None)
# Close temporary files, which will also remove them
f_copy.close()
--
1.8.1.4
More information about the Bmap-tools
mailing list