View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Mark Allen > Convert-SSH2 > Convert::SSH2



Annotate this POD

View/Report Bugs
Module Version: 0.01   Source  


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


Version 0.01


    use 5.010;
    use Convert::SSH2;

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

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



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.



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.


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.


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


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.


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.


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

Returns a formatted string.


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.


Mark Allen, <mrallen1 at>


Please report any bugs or feature requests to bug-convert-ssh2 at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


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

    perldoc Convert::SSH2

You can also look for information at:


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

Converting OpenSSH public keys


Mark Cavage


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 for more information.

syntax highlighting: