#! /usr/bin/python from sys import argv import os as OS import catacomb as C MAGIC = 'SSH PRIVATE KEY FILE FORMAT 1.1\n\0' keyring, tag, outfile = argv[1:] kf = C.KeyFile(keyring) k = kf[tag] kd = k.data n, e = [kd[label].mp for label in ['n', 'e']] priv = kd['private'] d, p, q, dp, dq, q_inv = \ [priv[label].mp for label in ['d', 'p', 'q', 'd-mod-p', 'd-mod-q', 'q-inv']] buf = C.WriteBuffer() buf.put(MAGIC) buf.putu8(0) # symmetric cipher type buf.putu32(0) # `reserved data' buf.putu32(n.nbits) # `not sure what this is' buf.putu16(n.nbits).put(n.storeb()) buf.putu16(e.nbits).put(e.storeb()) buf.putblk32(k.fulltag) # comment buf.putu16(1234).putu16(1234)# `next two pairs of characters are identical' buf.putu16(d.nbits).put(d.storeb()) buf.putu16(q_inv.nbits).put(q_inv.storeb()) buf.putu16(q.nbits).put(q.storeb()) buf.putu16(p.nbits).put(p.storeb()) outtmp = outfile + '.new' with open(outtmp, 'wb') as f: f.write(buf) OS.rename(outtmp, outfile)