diff options
author | Aaron LI <aaronly.me@outlook.com> | 2016-11-21 12:56:53 +0800 |
---|---|---|
committer | Aaron LI <aaronly.me@outlook.com> | 2016-11-21 12:56:53 +0800 |
commit | b23df04a1afdeab26b62ce1ac0ce85ef8547de28 (patch) | |
tree | 2e50c7d9c66eda3abed68a65e37b90fb5d8a4437 | |
parent | d5ca15269ebfa6da1c4b8a7651eb06e403b6abdd (diff) | |
download | fg21sim-b23df04a1afdeab26b62ce1ac0ce85ef8547de28.tar.bz2 |
utils: Add "hashutil.py" with function "md5()"
md5(): Calculate the MD5 checksum of the file.
Credit: https://stackoverflow.com/a/3431838/4856091
-rw-r--r-- | fg21sim/utils/hashutil.py | 40 |
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() |