ruby 中的 OpenSSL:私钥的 PKCS#8 格式
我已经用 ruby 创建了一个 RSA 私钥:
require 'openssl'
key = OpenSSL::PKey::RSA.generate(1024)
我可以获取 PEM 或 DER 格式的密钥:
key.to_pem
key.to_der
但似乎没有办法将其转换为 PKCS#8 格式。我想出的最好的方法是在另一个进程中调用 openssl:
require 'open3'
Open3.popen3('openssl pkcs8 -topk8 -inform PEM -outform PEM -passout pass:password') do |stdin, stdout, stderr|
stdin.write(key.to_pem)
unless (err = stderr.read).empty? then raise err end
stdout.read
end
一定有更好的方法,但我找不到。 ruby 中的 OpenSSL 类库是否有执行此操作的机制?
I've created an RSA private key in ruby with:
require 'openssl'
key = OpenSSL::PKey::RSA.generate(1024)
I can get the key in PEM or DER formats:
key.to_pem
key.to_der
But there doesn't seem to be a way to get it into PKCS#8 format. The best I've come up with is to call out to openssl in another process:
require 'open3'
Open3.popen3('openssl pkcs8 -topk8 -inform PEM -outform PEM -passout pass:password') do |stdin, stdout, stderr|
stdin.write(key.to_pem)
unless (err = stderr.read).empty? then raise err end
stdout.read
end
There must be a better way that I just can't find. Does the OpenSSL class library in ruby have a mechanism for doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Ruby 主干中支持将私钥导出到 PKCS #8格式,在
OpenSSL::PKey::PKey
(和子类)上使用新的private_to_pem
和private_to_der
方法。所以,很快,或者现在,如果您喜欢生活在最前沿,我们最终将能够以本机方式编写 PKCS#8 密钥。There is support in Ruby trunk for exporting private keys to PKCS#8 format, using the new
private_to_pem
andprivate_to_der
methods onOpenSSL::PKey::PKey
(and subclasses). So Real Soon Now, or Right Now if you like living on the bleeding edge, we'll finally be able to write out PKCS#8 keys natively.