From 0fc0e3da9e3ba602ce9722ae9243c7218c2568c1 Mon Sep 17 00:00:00 2001 From: Simeon Warner Date: Fri, 6 May 2016 20:45:38 -0400 Subject: [PATCH] Fix type check and pep8 --- resync/utils.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/resync/utils.py b/resync/utils.py index 028e18d..17d737a 100644 --- a/resync/utils.py +++ b/resync/utils.py @@ -1,11 +1,13 @@ -"""util.py: A collection of common util functions used in source and/or client.""" +"""util.py: A collection of utility functions for source and/or client.""" +import base64 +import hashlib """Compute digests for ResourceSync. -These are all base64 encoded according to the rules of +These are all base64 encoded according to the rules of http://www.ietf.org/rfc/rfc4648.txt -MD5 +MD5 ResourceSync defined to be the same as for Content-MD5 in HTTP, http://www.ietf.org/rfc/rfc2616.txt which, in turn, defined the @@ -15,31 +17,31 @@ http://www.ietf.org/rfc/rfc1864.txt Unfortunately, RFC1864 is rather vague and contains only and example which doesn't use encoding characters for 62 or 63. It points to RFC1521 to describe base64 which is explicit that the encoding alphabet -is [A-Za-z0-9+/] with = to pad. +is [A-Za-z0-9+/] with = to pad. The above corresponds with the alphabet of "3. Base 64 Encoding" in RFC3548 http://www.ietf.org/rfc/rfc3548.txt -and not the url safe version, "Base 64 Encoding with URL and Filename Safe +and not the url safe version, "Base 64 Encoding with URL and Filename Safe Alphabet" which replaces + and / with - and _ respectively. This is the same as the alphabet of "4. Base 64 Encoding" in RFC4648 http://www.ietf.org/rfc/rfc4648.txt. -This algorithm is implemented by base64.standard_b64encode() or +This algorithm is implemented by base64.standard_b64encode() or base64.b64encode() with no altchars specified. Available in python2.4 and up [http://docs.python.org/library/base64.html] """ -import base64 -import hashlib -def compute_md5_for_string(string): + +def compute_md5_for_string(str): """Compute MD5 digest over some string or byte payload. Returns a string containing the digest. """ - if (type(string) != 'bytes'): - string = string.encode('utf-8') #make bytes - return base64.b64encode(hashlib.md5(string).digest()).decode('utf-8') + if (not isinstance(str, bytes)): + str = str.encode('utf-8') # make bytes + return base64.b64encode(hashlib.md5(str).digest()).decode('utf-8') + def compute_md5_for_file(file, block_size=2**14): """Compute MD5 digest for a file. @@ -55,4 +57,5 @@ def compute_md5_for_file(file, block_size=2**14): if not data: break md5.update(data) + f.close() return base64.b64encode(md5.digest()).decode('utf-8')