【问题标题】:Generate OpenSSL subject_name hash in ruby在 ruby​​ 中生成 OpenSSL subject_name 哈希
【发布时间】:2018-06-16 18:15:17
【问题描述】:

我已经调查了30261296,但是我仍然无法找到一种在 Ruby 中使用 openssl 和/或摘要 gem 生成相同结果的方法。我试图在 ruby​​ 中复制的 OpenSSL 输出如下:

$ openssl x509 -noout -subject_hash -in DigiCertSHA2SecureServerCA.pem
85cf5865

在阅读很多东西时,我相信这个哈希是从证书的 Subject: 部分生成的,就像专有名称一样。在这种证书情况下,效果如下:

$ openssl x509 -noout -subject -in DigiCertSHA2SecureServerCA.crt
subject=C = US, O = DigiCert Inc, CN = DigiCert SHA2 Secure Server CA

尝试在命令行或 Ruby 中进行 SHA-1 编码(使用 openssl gem 时表示为 /C=US,/O=DigiCert Inc/CN=DigiCert SHA2 Secure Server CA)并没有得到与 OpenSSL 显示的结果相同的结果。

我正在尝试在 Ruby 中更本机地执行此操作,以避免尽可能使用 openssl,因为 openssl 和 digest 与 ruby​​ env 一起出现。最后我需要这个来生成哈希目录树......即85cf5865.0(哈希+'.0')。

我拥有的 CA 是 DigiCertSHA2SecureServerCA.crt - DER 编码。我将 DER 转换为 PEM,因为 openssl 命令行使用它而没有额外的 -inform der 开关。这对 Ruby 的 openssl gem 似乎无关紧要。

【问题讨论】:

  • Java here 有答案。这看起来像是一种皇家痛苦,我会选择向 openssl 掏腰包。

标签: ruby hash openssl sha1


【解决方案1】:

事实证明这非常简单,因为 Ruby 的 OpenSSL 绑定包含 OpenSSL::X509::Name#hash method,这正是我们想要的。

require 'openssl'

# Read the certificate.
cert = OpenSSL::X509::Certificate.new(File.binread("DigiCertSHA2SecureServerCA.crt"))

# Get the subject, which is an OpenSSL::X509::Name object.
name = cert.subject

# hash returns an integer, we want the hex string so call to_s(16).
puts name.hash.to_s(16) #=> 85cf5865

整数将是正数,因为 OpenSSL 返回一个 unsigned int,所以我们可以直接使用 to_s(16) 而不必担心转换负值。

【讨论】:

  • 哇,我不知道我有点接近...无法弄清楚它是整数散列的散列。谢谢!
猜你喜欢
  • 2016-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多