1 package Crypt::Monkeysphere::Keytrans;
7 use Exporter qw(import);
8 our @EXPORT_OK=qw(openssh_rsa_pubkey_pack);
11 # takes a Math::BigInt and returns it properly packed for openssh output.
13 sub openssh_mpi_pack {
16 my $val = $num->as_hex();
18 # ensure we've got an even multiple of 2 nybbles here.
20 if (length($val) % 2);
21 $val = pack('H*', $val);
22 # packed binary ones-complement representation of the value.
24 my $mpilen = length($val);
26 my $ret = pack('N', $mpilen);
28 # if the first bit of the leading byte is high, we should include a
30 if (ord($val) & 0x80) {
31 $ret = pack('NC', $mpilen+1, 0);
37 # this output is not base64-encoded yet. Pass it through
38 # encode_base64($output, '') if you want to make a file.
40 sub openssh_rsa_pubkey_pack {
41 my ($modulus, $exponent) = @_;
43 return openssh_mpi_pack(Math::BigInt->new('0x'.unpack('H*', "ssh-rsa"))).
44 openssh_mpi_pack($exponent).
45 openssh_mpi_pack($modulus);