aboutsummaryrefslogtreecommitdiffstats
path: root/fg21sim/utils
diff options
context:
space:
mode:
Diffstat (limited to 'fg21sim/utils')
-rw-r--r--fg21sim/utils/hashutil.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/fg21sim/utils/hashutil.py b/fg21sim/utils/hashutil.py
new file mode 100644
index 0000000..394ef04
--- /dev/null
+++ b/fg21sim/utils/hashutil.py
@@ -0,0 +1,40 @@
+# Copyright (c) 2016 Weitian LI <liweitianux@live.com>
+# MIT license
+
+"""
+Hash utilities.
+
+md5 :
+ Calculate the MD5 checksum of the file.
+"""
+
+import hashlib
+
+
+def md5(filepath, blocksize=65536):
+ """
+ Calculate the MD5 checksum/digest of the file data.
+
+ Parameters
+ ----------
+ filepath : str
+ The path to the file
+ blocksize : int, optional
+ The block size (bytes) of chunks when read the file.
+
+ Returns
+ -------
+ digest : str
+ The checksum/digest of the file data, containing only hexadecimal
+ digits.
+
+ Credits
+ -------
+ - Stackoverflow: Generating an MD5 checksum of a file
+ https://stackoverflow.com/a/3431838/4856091
+ """
+ hasher = hashlib.md5()
+ with open(filepath, "rb") as f:
+ for chunk in iter(lambda: f.read(blocksize), b""):
+ hasher.update(chunk)
+ return hasher.hexdigest()