mirror of
https://github.com/romanz/amodem.git
synced 2026-04-14 01:45:24 +08:00
gpg: move armor to proto
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
"""Create GPG ECDSA signatures and public keys using TREZOR device."""
|
||||
import base64
|
||||
import hashlib
|
||||
import logging
|
||||
import struct
|
||||
@@ -229,19 +228,3 @@ def _make_signature(signer_func, data_to_sign, public_algo,
|
||||
return bytes(header + hashed + unhashed +
|
||||
digest[:2] + # used for decoder's sanity check
|
||||
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
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""GPG protocol utilities."""
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import logging
|
||||
import struct
|
||||
@@ -137,3 +138,19 @@ class PublicKey(object):
|
||||
return '<{}>'.format(util.hexlify(self.key_id()))
|
||||
|
||||
__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
|
||||
|
||||
@@ -7,7 +7,7 @@ import sys
|
||||
import time
|
||||
import os
|
||||
|
||||
from . import decode, encode
|
||||
from . import decode, encode, proto
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -25,7 +25,7 @@ def run_create(args):
|
||||
result = s.export()
|
||||
s.close()
|
||||
|
||||
sys.stdout.write(encode.armor(result, 'PUBLIC KEY BLOCK'))
|
||||
sys.stdout.write(proto.armor(result, 'PUBLIC KEY BLOCK'))
|
||||
|
||||
|
||||
def run_sign(args):
|
||||
@@ -39,7 +39,7 @@ def run_sign(args):
|
||||
sig = s.sign(data)
|
||||
s.close()
|
||||
|
||||
sig = encode.armor(sig, 'SIGNATURE')
|
||||
sig = proto.armor(sig, 'SIGNATURE')
|
||||
decode.verify(pubkey=pubkey, signature=sig, original_data=data)
|
||||
|
||||
filename = '-' # write to stdout
|
||||
|
||||
Reference in New Issue
Block a user