1

I am trying to find a Perl solution to identify the type of the keys. I have a list of key files and I need to know which are DSA and which are RSA.

I know I can do it on Linux with the following command:

ssh-keygen -lf id_dsa.pub

I am trying to find a pure Perl solution that would work on both Windows and Linux.

Thank you.

Andrey
  • 1,808
  • 1
  • 16
  • 28
  • There would be RFC or other specification documents out there that describe what these keys look like. Once you have them you should be able to implement parsers for them. – simbabque Jul 06 '16 at 13:52
  • Have you ever looked at what is inside a public key file? because if you do, the answer to your question would be obvious! – salva Jul 07 '16 at 05:48

1 Answers1

0

You can use File::LibMagic which uses the same library as the unix file command.

On ubuntu, I installed it as:

apt-get install libmagic-dev

cpan File::LibMagic

For windows, it is known to work with Cygwin, and it should work with windows if you can install the libmagic libraries. I haven't tried this, so YMMV.

Example

#!/usr/bin/env perl

use warnings;
use strict;

use File::LibMagic;

my $magic = File::LibMagic->new();

my $key_dsa = '/home/felix/.ssh/id_dsa.pub';
my $info_dsa = $magic->info_from_filename( $key_dsa );
print "[ $key_dsa ] : " . $info_dsa->{description} . "\n";

my $key_rsa = '/home/felix/.ssh/id_rsa.pub';
my $info_rsa = $magic->info_from_filename( $key_rsa );
print "[ $key_rsa ] : " . $info_rsa->{description} . "\n";

Output

[ /home/felix/.ssh/id_dsa.pub ] : OpenSSH DSA public key
[ /home/felix/.ssh/id_rsa.pub ] : OpenSSH RSA public key
xxfelixxx
  • 6,512
  • 3
  • 31
  • 38
  • Thank you for the solution. Unfortunately, for Windows it requires Cygwin which I am trying to avoid using. – Andrey Jul 06 '16 at 19:28
  • Yes I did. The installation failed on two different windows environment because of some dependencies. – Andrey Jul 07 '16 at 15:22
  • You could just try reading the keys and seeing if it starts with `ssh-dss` for dsa or `ssh-rsa` for rsa. – xxfelixxx Jul 08 '16 at 03:43