无需 ssh-keygen 即可生成 SSH 密钥对(私有/公共)
我正在开发一个需要生成 SSH 密钥对的 Ruby/Rack 应用程序。尽管我很想从应用程序中调用 ssh-keygen ,但我不能,因为它设计为在 Heroku 上运行,并且它们不支持调用该命令。
我已经能够使用 Ruby 标准库中的 OpenSSL 获取私钥/公钥 RSA 密钥,执行以下操作:
key = OpenSSL::PKey::RSA.generate(2048)
# => -----BEGIN RSA PRIVATE KEY----- ....
key.public_key
# => -----BEGIN RSA PUBLIC KEY----- ....
不幸的是,RSA 公钥和 SSH 公钥不是同一件事,即使它们可以从相同的 RSA 密钥生成。 SSH 公钥如下所示:
ssh-rsa AAAAB3NzaC1yc2EAAAABIwA.....
Is it possible to generated SSH key or conversion RSA key to SSH in Ruby without using ssh-keygen
?
I'm working on a Ruby/Rack application that needs to generate SSH keypairs. As much as I'd like to call ssh-keygen
from the application, I can't because it's designed to run on Heroku and they don't support calling that command.
I've been able to get private/public RSA keys using OpenSSL in the Ruby standard library doing the following:
key = OpenSSL::PKey::RSA.generate(2048)
# => -----BEGIN RSA PRIVATE KEY----- ....
key.public_key
# => -----BEGIN RSA PUBLIC KEY----- ....
Unfortunately an RSA public key and an SSH public key is not the same thing, even though they can be generated from the same RSA key. An SSH public key looks something like the following:
ssh-rsa AAAAB3NzaC1yc2EAAAABIwA.....
Is it possible to generate SSH keys or convert RSA keys to SSH in Ruby without using ssh-keygen
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您遇到问题时,情况可能并非如此,但是 net-ssh 库补丁< a href="https://net-ssh.github.io/net-ssh/OpenSSL/PKey/RSA.html" rel="noreferrer">OpenSSL::PKey::RSA 和 ::DSA 有两种方法:
#ssh_type
- 根据需要返回"ssh-rsa"
或"ssh-dss"
以及
#to_blob
- 返回 OpenSSH 二进制 blob 中的公钥格式。如果您对其进行 base64 编码,这就是您正在寻找的格式。It may not have been the case when you had the problem, but the net-ssh library patches OpenSSL::PKey::RSA and ::DSA with two methods:
#ssh_type
- returns"ssh-rsa"
or"ssh-dss"
as appropriateand
#to_blob
- returns the public key in OpenSSH binary-blob format. If you base64-encode it, it's the format you're looking for.事实证明这比我预期的要复杂得多。我最终编写了 SSHKey gem 来实现它(源代码 GitHub 上)。 SSH 公钥的编码方式与提供的 RSA 公钥完全不同。 SSH 密钥的数据类型编码在 RFC #4251 的第 #5 节中定义。
Turns out this was much more complicated than I anticipated. I ended up writing the SSHKey gem to pull it off (source code on GitHub). SSH Public keys are encoded totally differently from the RSA public key provided. Data type encoding for SSH keys are defined in section #5 of RFC #4251.
包括密钥加密的完整过程记录在此处: http://stuff-things.net/2009/12/11/generate-rsa-key-pairs-in-ruby/
The full process including key encryption is documented here: http://stuff-things.net/2009/12/11/generating-rsa-key-pairs-in-ruby/