gpg: move armor to proto

This commit is contained in:
Roman Zeyde
2016-05-21 17:15:42 +03:00
parent 0e72e3b7ff
commit 2cf081420f
3 changed files with 20 additions and 20 deletions

View File

@@ -1,5 +1,4 @@
"""Create GPG ECDSA signatures and public keys using TREZOR device.""" """Create GPG ECDSA signatures and public keys using TREZOR device."""
import base64
import hashlib import hashlib
import logging import logging
import struct import struct
@@ -229,19 +228,3 @@ def _make_signature(signer_func, data_to_sign, public_algo,
return bytes(header + hashed + unhashed + return bytes(header + hashed + unhashed +
digest[:2] + # used for decoder's sanity check digest[:2] + # used for decoder's sanity check
sig) # actual ECDSA signature sig) # actual ECDSA signature
def _split_lines(body, size):
lines = []
for i in range(0, len(body), size):
lines.append(body[i:i+size] + '\n')
return ''.join(lines)
def armor(blob, type_str):
"""See https://tools.ietf.org/html/rfc4880#section-6 for details."""
head = '-----BEGIN PGP {}-----\nVersion: GnuPG v2\n\n'.format(type_str)
body = base64.b64encode(blob)
checksum = base64.b64encode(util.crc24(blob))
tail = '-----END PGP {}-----\n'.format(type_str)
return head + _split_lines(body, 64) + '=' + checksum + '\n' + tail

View File

@@ -1,5 +1,6 @@
"""GPG protocol utilities.""" """GPG protocol utilities."""
import base64
import hashlib import hashlib
import logging import logging
import struct import struct
@@ -137,3 +138,19 @@ class PublicKey(object):
return '<{}>'.format(util.hexlify(self.key_id())) return '<{}>'.format(util.hexlify(self.key_id()))
__str__ = __repr__ __str__ = __repr__
def _split_lines(body, size):
lines = []
for i in range(0, len(body), size):
lines.append(body[i:i+size] + '\n')
return ''.join(lines)
def armor(blob, type_str):
"""See https://tools.ietf.org/html/rfc4880#section-6 for details."""
head = '-----BEGIN PGP {}-----\nVersion: GnuPG v2\n\n'.format(type_str)
body = base64.b64encode(blob)
checksum = base64.b64encode(util.crc24(blob))
tail = '-----END PGP {}-----\n'.format(type_str)
return head + _split_lines(body, 64) + '=' + checksum + '\n' + tail

View File

@@ -7,7 +7,7 @@ import sys
import time import time
import os import os
from . import decode, encode from . import decode, encode, proto
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -25,7 +25,7 @@ def run_create(args):
result = s.export() result = s.export()
s.close() s.close()
sys.stdout.write(encode.armor(result, 'PUBLIC KEY BLOCK')) sys.stdout.write(proto.armor(result, 'PUBLIC KEY BLOCK'))
def run_sign(args): def run_sign(args):
@@ -39,7 +39,7 @@ def run_sign(args):
sig = s.sign(data) sig = s.sign(data)
s.close() s.close()
sig = encode.armor(sig, 'SIGNATURE') sig = proto.armor(sig, 'SIGNATURE')
decode.verify(pubkey=pubkey, signature=sig, original_data=data) decode.verify(pubkey=pubkey, signature=sig, original_data=data)
filename = '-' # write to stdout filename = '-' # write to stdout