From b23df04a1afdeab26b62ce1ac0ce85ef8547de28 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Mon, 21 Nov 2016 12:56:53 +0800 Subject: utils: Add "hashutil.py" with function "md5()" md5(): Calculate the MD5 checksum of the file. Credit: https://stackoverflow.com/a/3431838/4856091 --- fg21sim/utils/hashutil.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 fg21sim/utils/hashutil.py 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 +# 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() -- cgit v1.2.2