【问题标题】:Indentify DSA/RSA keys in Perl在 Perl 中识别 DSA/RSA 密钥
【发布时间】:2016-11-08 14:21:31
【问题描述】:

我正在尝试寻找一种 Perl 解决方案来识别键的类型。 我有一个密钥文件列表,我需要知道哪些是 DSA,哪些是 RSA。

我知道我可以在 Linux 上使用以下命令来做到这一点:

ssh-keygen -lf id_dsa.pub

我正在尝试寻找一个可以在 Windows 和 Linux 上运行的纯 Perl 解决方案。

谢谢。

【问题讨论】:

  • 会有 RFC 或其他规范文档描述这些键的外观。拥有它们后,您应该能够为它们实现解析器。
  • 您是否查看过公钥文件中的内容?因为如果你这样做了,你的问题的答案就很明显了!

标签: perl rsa gnupg pgp dsa


【解决方案1】:

您可以使用 File::LibMagic,它使用与 unix file 命令相同的库。

在 ubuntu 上,我将其安装为:

apt-get install libmagic-dev

cpan File::LibMagic

对于 windows,它可以与 Cygwin 一起使用,如果您可以安装 libmagic 库,它应该可以与 windows 一起使用。这个我没试过,所以YMMV。

示例

#!/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";

输出

[ /home/felix/.ssh/id_dsa.pub ] : OpenSSH DSA public key
[ /home/felix/.ssh/id_rsa.pub ] : OpenSSH RSA public key

【讨论】:

  • 感谢您的解决方案。不幸的是,对于 Windows,它需要 Cygwin,我试图避免使用它。
  • 是的,我做到了。由于某些依赖关系,在两个不同的 windows 环境下安装失败。
  • 您可以尝试读取密钥并查看它是否以 ssh-dss 开头(用于 dsa 或以ssh-rsa 用于 rsa)。
猜你喜欢
  • 2013-10-30
  • 2013-05-07
  • 2012-09-16
  • 1970-01-01
  • 2014-01-14
  • 2012-10-06
  • 1970-01-01
  • 2015-11-12
  • 2013-09-20
相关资源
最近更新 更多