From: W. Trevor King Date: Mon, 23 Dec 2013 00:27:11 +0000 (-0800) Subject: Factor byte-to-string conversion out into byte_string X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=5e93ad382effb8ff852b187af30fe75b6b898085;p=gpg-migrate.git Factor byte-to-string conversion out into byte_string No need to repeat this code everywhere we want to stringify some bytes. --- diff --git a/gpg-migrate.py b/gpg-migrate.py index 8d475f7..e376f8d 100755 --- a/gpg-migrate.py +++ b/gpg-migrate.py @@ -25,6 +25,26 @@ def _get_stdout(args, stdin=None): return stdout +def byte_string(data, sep=' '): + r"""Convert a byte-string to human readable form + + >>> byte_string(b'\x12\x34\x56') + '12 34 56' + """ + return sep.join('{:02x}'.format(byte) for byte in data) + + +def string_bytes(data, sep=' '): + r"""Reverse byte_string() + + >>> string_bytes('12 fa fb') + b'\x12\xfa\xfb' + """ + return bytes( + int(c1+c2, base=16) for c1,c2 in + zip(data[::2 + len(sep)], data[1::2 + len(sep)])) + + class PGPPacket (dict): # http://tools.ietf.org/search/rfc4880 _old_format_packet_length_type = { # type: (bytes, struct type) @@ -325,7 +345,7 @@ class PGPPacket (dict): if key in self: value = self[key] if isinstance(value, bytes): - value = ' '.join('{:02x}'.format(byte) for byte in value) + value = byte_string(data=value) lines.append(' {}: {}'.format(label, value)) return '\n'.join(lines) @@ -711,7 +731,7 @@ class PGPPacket (dict): subpacket['signature-creation-time'] = _struct.unpack('>I', data)[0] def _parse_issuer_signature_subpacket(self, data, subpacket): - subpacket['issuer'] = ''.join('{:02x}'.format(byte) for byte in data) + subpacket['issuer'] = byte_string(data=data, sep='') def _parse_key_expiration_time_signature_subpacket( self, data, subpacket): @@ -996,9 +1016,8 @@ class PGPPacket (dict): raise ValueError( ('serialized {} differs from input packet: ' 'at byte {}, {} != {}').format( - self['type'], i, - ' '.join('{:02x}'.format(byte) for byte in out_chunk), - ' '.join('{:02x}'.format(byte) for byte in in_chunk))) + self['type'], i, byte_string(data=out_chunk), + byte_string(data=in_chunk))) class PGPKey (object):