Check string是SSH公钥

时间:2015-02-27 09:08:47

标签: perl ssh

我有一个字符串,我想检查它是一个SSH公钥。我试过这个($pk包含文件名而不是字符串):

#!/usr/bin/perl
use strict;
use warnings;
use Crypt::RSA;

my $pk = "$ENV{HOME}/.ssh/id_rsa.pub";
my $public = new Crypt::RSA::Key::Public( Filename => $pk );

但我得到的错误就像

Bareword found where operator expected at (eval 13) line 1
< ... >
Can't bless non-reference value at perl5/lib/perl5/Crypt/RSA/Key/Public.pm line 28.

2 个答案:

答案 0 :(得分:1)

好的,我found a&#34;非Perl&#34;溶液:

#!/usr/bin/perl
use strict;
use warnings;
use autodie;

my $string  = '123';
my $string2 = <<'EOF';
<real public key string>
EOF

for my $pk ( $string, $string2 ) {
    chomp $pk;

    my $file = '/tmp/pk';
    open my $fh, '>', $file;
    print {$fh} $pk;
    close $fh;

    if ( !system("ssh-keygen -l -f $file > /dev/null 2>&1") ) {
        print "[$pk] OK\n";
    } else {
        print "[$pk] NOT OK\n";
    }
}

答案 1 :(得分:0)

参考之前的,但类似的答案:

Is it possible to sign my data using SSH private key in perl?

我认为您对Crypt::RSA有困难,因为密钥文件的格式不同。