diff options
| author | Guillaume Horel <guillaume.horel@gmail.com> | 2010-11-16 00:29:26 -0500 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@gmail.com> | 2010-11-16 00:29:26 -0500 |
| commit | 50ffed97de46dd5d3367703bd5e14c36ee3fb583 (patch) | |
| tree | c78263296e3e3e2ea68f84d9c1b6069904c92e92 /ssh_rsa_key_util.py | |
| parent | 46930d831628c3ccd4f015d1ff6fc4cc3ecf26cd (diff) | |
| download | alias-50ffed97de46dd5d3367703bd5e14c36ee3fb583.tar.gz | |
Clean up ssh_rsa_key_util.py.
reused some functions from keyczar.util
Diffstat (limited to 'ssh_rsa_key_util.py')
| -rw-r--r-- | ssh_rsa_key_util.py | 52 |
1 files changed, 12 insertions, 40 deletions
diff --git a/ssh_rsa_key_util.py b/ssh_rsa_key_util.py index 11d322e..e4cd5ba 100644 --- a/ssh_rsa_key_util.py +++ b/ssh_rsa_key_util.py @@ -1,6 +1,7 @@ import base64 import struct import filecmp +from keyczar import util # need pyasn for DER parsing and generating from pyasn1.type import univ @@ -13,7 +14,6 @@ def read_int(buffer, i): i += 4 return (l, i) - def read_chunk(buffer, i): "Read chunk from buffer." @@ -29,34 +29,6 @@ def read_chunk(buffer, i): i += l return (s, i) - -def unpack_bigint(buffer): - "Turn binary chunk into integer." - - v = 0 - for c in buffer: - v *= 256 - v += ord(c) - - return v - - -def pack_bigint(v): - "Pack integer into binary chunk." - - chunk = '' - rest = v - while rest: - chunk = chr(rest % 256) + chunk - rest //= 256 - - # add a zero byte if the highest bit is 1, so it won't be negative - if ord(chunk[0]) & 128: - chunk = chr(0) + chunk - - return chunk - - def read_rsa_pub(filename): """Read RSA public key file. Structure: @@ -76,15 +48,14 @@ def read_rsa_pub(filename): # grab e (s, i) = read_chunk(raw, i) - e = unpack_bigint(s) - + #e = unpack_bigint(s) + e = util.BytesToLong(s) # grab n (s, i) = read_chunk(raw, i) - n = unpack_bigint(s) - + #n = unpack_bigint(s) + n = util.BytesToLong(s) return (n, e, host) - def write_rsa_pub(filename, n, e, host): """Write RSA public key file. Structure: @@ -93,8 +64,11 @@ def write_rsa_pub(filename, n, e, host): base64data: [7]ssh-rsa[len][e-data][len][n-data] """ - e_str = pack_bigint(e) - n_str = pack_bigint(n) + #e_str = pack_bigint(e) + #n_str = pack_bigint(n) + e_str = util.BigIntToBytes(e) + n_str = util.BigIntToBytes(n) + # pack e and n properly into the raw data raw = struct.pack('!I7sI{0:d}sI{1:d}s'.format(len(e_str), len(n_str)), 7, 'ssh-rsa', len(e_str), e_str, len(n_str), n_str) @@ -102,7 +76,6 @@ def write_rsa_pub(filename, n, e, host): content = "ssh-rsa {0!s} {1!s}\n".format(base64.b64encode(raw), host) file(filename, 'w').write(content) - def read_rsa_pri(filename): """Read RSA private key file. Structure: @@ -136,7 +109,6 @@ def read_rsa_pri(filename): return (n, e, d, p, q, e1, e2, c) - def write_rsa_pri(filename, n, e, d, p, q, e1, e2, c): """Write RSA private key file. Structure: @@ -184,9 +156,9 @@ def write_rsa_pri(filename, n, e, d, p, q, e1, e2, c): chopped = [data[i:i + width] for i in xrange(0, len(data), width)] # assemble file content content = """-----BEGIN RSA PRIVATE KEY----- -%s +{0} -----END RSA PRIVATE KEY----- -""" % '\n'.join(chopped) +""".format('\n'.join(chopped)) file(filename, 'w').write(content) if __name__ == '__main__' : |
