Skip to content

Commit

Permalink
Merge pull request ansible#12780 from mgedmin/py3k
Browse files Browse the repository at this point in the history
Python 3: avoid %-formatting of byte strings
  • Loading branch information
abadger committed Oct 16, 2015
2 parents 89b0c3f + 98958ec commit 38b99a7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions lib/ansible/parsing/vault/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,12 @@ def _format_output(self, b_data):
if not self.cipher_name:
raise AnsibleError("the cipher must be set before adding a header")

tmpdata = [b'%s\n' % b_data[i:i+80] for i in range(0, len(b_data), 80)]
tmpdata.insert(0, b'%s;%s;%s\n' % (b_HEADER, self.b_version,
to_bytes(self.cipher_name, errors='strict', encoding='utf-8')))
tmpdata = b''.join(tmpdata)
header = b';'.join([b_HEADER, self.b_version,
to_bytes(self.cipher_name, errors='strict', encoding='utf-8')])
tmpdata = [header]
tmpdata += [b_data[i:i+80] for i in range(0, len(b_data), 80)]
tmpdata += [b'']
tmpdata = b'\n'.join(tmpdata)

return tmpdata

Expand Down Expand Up @@ -422,7 +424,7 @@ def aes_derive_key_and_iv(self, password, salt, key_length, iv_length):

d = d_i = b''
while len(d) < key_length + iv_length:
text = b"%s%s%s" % (d_i, password, salt)
text = b''.join([d_i, password, salt])
d_i = to_bytes(md5(text).digest(), errors='strict')
d += d_i

Expand Down Expand Up @@ -568,7 +570,7 @@ def encrypt(self, data, password):

# COMBINE SALT, DIGEST AND DATA
hmac = HMAC.new(key2, cryptedData, SHA256)
message = b'%s\n%s\n%s' % (hexlify(salt), to_bytes(hmac.hexdigest()), hexlify(cryptedData))
message = b'\n'.join([hexlify(salt), to_bytes(hmac.hexdigest()), hexlify(cryptedData)])
message = hexlify(message)
return message

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ commands =
py27: python -m compileall -fq -x 'test|samples' lib test contrib
py{34,35}: python -m compileall -fq -x 'lib/ansible/module_utils|lib/ansible/modules' lib test contrib
# Unittests need lots of work to make code python3 compatible
py{26,27,35}: make tests
py{26,27,34,35}: make tests

0 comments on commit 38b99a7

Please sign in to comment.