summaryrefslogtreecommitdiff
path: root/lib/ansible/utils/hashing.py
diff options
context:
space:
mode:
authorToshio Kuratomi <a.badger@gmail.com>2016-08-24 09:29:59 -0700
committerGitHub <noreply@github.com>2016-08-24 09:29:59 -0700
commit300d6482d1dd29ef8a2fb5743d38c8035770cb87 (patch)
treeb80054b8a5a1c7247e114e1012464af0da3893de /lib/ansible/utils/hashing.py
parente098c5ef82e77ac518cc4d6615dce4d341cb6c19 (diff)
downloadansible-300d6482d1dd29ef8a2fb5743d38c8035770cb87.tar.gz
Hashing needs byte strings in python3 (#17221)
First try at porting this passed in string-types as that worked on python2. Python3 is more strict so be explicit about converting from text to bytes
Diffstat (limited to 'lib/ansible/utils/hashing.py')
-rw-r--r--lib/ansible/utils/hashing.py16
1 files changed, 8 insertions, 8 deletions
diff --git a/lib/ansible/utils/hashing.py b/lib/ansible/utils/hashing.py
index 4ef19d6c4e..a486f3e7b0 100644
--- a/lib/ansible/utils/hashing.py
+++ b/lib/ansible/utils/hashing.py
@@ -38,22 +38,19 @@ except ImportError:
# Assume we're running in FIPS mode here
_md5 = None
-from ansible.compat.six import string_types
from ansible.errors import AnsibleError
from ansible.utils.unicode import to_bytes
+
def secure_hash_s(data, hash_func=sha1):
''' Return a secure hash hex digest of data. '''
digest = hash_func()
- try:
- if not isinstance(data, string_types):
- data = "%s" % data
- digest.update(data)
- except UnicodeEncodeError:
- digest.update(data.encode('utf-8'))
+ data = to_bytes(data, errors='strict')
+ digest.update(data)
return digest.hexdigest()
+
def secure_hash(filename, hash_func=sha1):
''' Return a secure hash hex digest of local file, None if file is not present or a directory. '''
@@ -76,6 +73,8 @@ def secure_hash(filename, hash_func=sha1):
checksum = secure_hash
checksum_s = secure_hash_s
+
+#
# Backwards compat functions. Some modules include md5s in their return values
# Continue to support that for now. As of ansible-1.8, all of those modules
# should also return "checksum" (sha1 for now)
@@ -84,14 +83,15 @@ checksum_s = secure_hash_s
# 2) Compliance with a third party protocol
#
# MD5 will not work on systems which are FIPS-140-2 compliant.
+#
def md5s(data):
if not _md5:
raise ValueError('MD5 not available. Possibly running in FIPS mode')
return secure_hash_s(data, _md5)
+
def md5(filename):
if not _md5:
raise ValueError('MD5 not available. Possibly running in FIPS mode')
return secure_hash(filename, _md5)
-