fix Py3k issues
This commit is contained in:
@@ -9,8 +9,8 @@ import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
DER_OCTET_STRING = b'\x04'
|
||||
ECDSA_KEY_TYPE = 'ecdsa-sha2-nistp256'
|
||||
ECDSA_CURVE_NAME = 'nistp256'
|
||||
ECDSA_KEY_TYPE = b'ecdsa-sha2-nistp256'
|
||||
ECDSA_CURVE_NAME = b'nistp256'
|
||||
|
||||
curve = ecdsa.NIST256p
|
||||
hashfunc = hashlib.sha256
|
||||
@@ -57,21 +57,23 @@ def decompress_pubkey(pub):
|
||||
B = curve.curve.b()
|
||||
x = util.bytes2num(pub[1:33])
|
||||
beta = pow(int(x*x*x+A*x+B), int((P+1)//4), int(P))
|
||||
y = (P-beta) if ((beta + ord(pub[0])) % 2) else beta
|
||||
|
||||
p0 = util.bytes2num(pub[:1])
|
||||
y = (P-beta) if ((beta + p0) % 2) else beta
|
||||
|
||||
point = ecdsa.ellipticcurve.Point(curve.curve, x, y)
|
||||
vk = ecdsa.VerifyingKey.from_public_point(point, curve=curve,
|
||||
hashfunc=hashfunc)
|
||||
parts = [ECDSA_KEY_TYPE, ECDSA_CURVE_NAME,
|
||||
DER_OCTET_STRING + vk.to_string()]
|
||||
return ''.join([util.frame(p) for p in parts])
|
||||
return b''.join([util.frame(p) for p in parts])
|
||||
|
||||
|
||||
def export_public_key(pubkey, label):
|
||||
blob = decompress_pubkey(pubkey)
|
||||
log.debug('fingerprint: %s', fingerprint(blob))
|
||||
b64 = base64.b64encode(blob)
|
||||
return '{} {} {}\n'.format(ECDSA_KEY_TYPE, b64, label)
|
||||
b64 = base64.b64encode(blob).decode('ascii')
|
||||
return '{} {} {}\n'.format(ECDSA_KEY_TYPE.decode('ascii'), b64, label)
|
||||
|
||||
|
||||
def import_public_key(line):
|
||||
|
||||
@@ -5,12 +5,12 @@ from .. import formats
|
||||
|
||||
def test_fingerprint():
|
||||
fp = '5d:41:40:2a:bc:4b:2a:76:b9:71:9d:91:10:17:c5:92'
|
||||
assert formats.fingerprint('hello') == fp
|
||||
assert formats.fingerprint(b'hello') == fp
|
||||
|
||||
|
||||
_point = (
|
||||
44423495295951059636974944244307637263954375053872017334547086177777411863925L, # nopep8
|
||||
111713194882028655451852320740440245619792555065469028846314891587105736340201L # nopep8
|
||||
44423495295951059636974944244307637263954375053872017334547086177777411863925, # nopep8
|
||||
111713194882028655451852320740440245619792555065469028846314891587105736340201 # nopep8
|
||||
)
|
||||
|
||||
_public_key = (
|
||||
@@ -24,12 +24,12 @@ _public_key = (
|
||||
|
||||
def test_parse_public_key():
|
||||
key = formats.import_public_key(_public_key)
|
||||
assert key['name'] == 'home'
|
||||
assert key['name'] == b'home'
|
||||
assert key['point'] == _point
|
||||
|
||||
assert key['curve'] == 'nistp256'
|
||||
assert key['curve'] == b'nistp256'
|
||||
assert key['fingerprint'] == '4b:19:bc:0f:c8:7e:dc:fa:1a:e3:c2:ff:6f:e0:80:a2' # nopep8
|
||||
assert key['type'] == 'ecdsa-sha2-nistp256'
|
||||
assert key['type'] == b'ecdsa-sha2-nistp256'
|
||||
assert key['size'] == 32
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user