NAME

Convert::SSH2 - Convert SSH2 RSA keys to other formats

VERSION

Version 0.01

SYNOPSIS

use 5.010;
use Convert::SSH2;

my $converter = Convert::SSH2->new('~/.ssh/id_rsa.pub');
# Automatically calls parse()

# Use default PKCS#1 format
say $converter->format_output();

$converter->write('/my/pub/key.pem');

PURPOSE

This library converts SSH2 style RSA public keys to other representations like PKCS#1. This is useful if you want to use these public keys with other Perl cryptography libraries like Crypt::RSA or Crypt::OpenSSL::RSA.

ATTRIBUTES

key

Required. Read-only. The key material. Attempts to be DWIMish. If this is a file path, it will be used to load the file contents into memory. If it's a buffer, it will use the buffer contents.

format

Read-only. The output format. Current supports:

  • pkcs1

    This format looks like

    -----BEGIN RSA PUBLIC KEY-----
    ...
    -----END RSA PUBLIC KEY-----
  • pkcs8

    This format looks like

    -----BEGIN PUBLIC KEY-----
    ...
    -----END PUBLIC KEY-----

You can add your own format by implementing a Convert::SSH2::Format::Base module.

METHODS

Generally, errors are fatal. Use Try::Tiny if you want more graceful error handling.

new()

Constructor. Takes any of the attributes as arguments. You may optionally call new with either a buffer or a path, and the class will assume that it is the key material.

The object automatically attempts to parse key data after instantiation.

parse()

This method takes the Base64 encoded portion of the SSH key, decodes it, and then converts the data inside of it into three components: the id string ('ssh-rsa'), the public exponent ('e'), and the modulus ('n'). By default it looks for the Base64 data inside the instantiated object, but you can optionally pass in a Base64 string.

It uses Math::BigInt to hold large integers such as 'n' or 'e'. If you don't have libgmp installed, it will fall back to pure perl automatically, but there will be a speed penalty.

Returns a true value on success.

format_output()

Using a subclass of Convert::SSH2::Format::Base, generate a representation of the SSH2 key.

Returns a formatted string.

write()

Convenience method to write a formatted key representation to a file.

Expects a pathname. Automatically calls format_output() if necessary. If the output format has been generated already, it uses a cached version.

Returns a true value on success.

AUTHOR

Mark Allen, <mrallen1 at yahoo.com>

BUGS

Please report any bugs or feature requests to bug-convert-ssh2 at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Convert-SSH2. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Convert::SSH2

You can also look for information at:

SEE ALSO

Convert::SSH2::Format::Base, Convert::SSH2::Format::PKCS1

Converting OpenSSH public keys

ACKNOWLEDGEMENTS

Mark Cavage

LICENSE AND COPYRIGHT

Copyright 2012 Mark Allen.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.