【问题标题】:Get serial number from a certificate using Ruby使用 Ruby 从证书中获取序列号
【发布时间】:2018-04-11 00:11:54
【问题描述】:

有没有办法用 Ruby 知道证书序列号?我知道我可以通过以下方式获得它:

openssl x509 -inform DER -in file.cer -noout -serial >"serial.txt"

【问题讨论】:

    标签: ruby-on-rails ruby openssl x509certificate der


    【解决方案1】:

    您可以直接在 ruby​​ 中运行 shell 命令并将其输出到变量gets。我认为您将不得不按照以下方式做一些事情:

    serial = `openssl x509 -inform DER -in file.cer -noout -serial`
    

    这里有更详细的答案:Calling shell commands from Ruby

    【讨论】:

    • 谢谢!它帮助很大,我能够使用文件的完整路径运行它,但我不知道如何使用变量运行它(在每个文件上使用它)。我使用了这个:serial = openssl x509 -inform DER -in /home/faviovelez/webapps/projects/007/mosaicone007/lib/cer/2/LAN7008173R5.cer -noout -serial 并得到了这个:“serial=3230303031303030303030333030303232383135\n”你知道如何使用可以替换为文件名和路径的变量运行它吗?
    • 知道了!我刚刚这样做了: file = File.join(Rails.root, 'lib', 'cer', '2', 'LAN7008173R5.cer') serial = openssl x509 -inform DER -in #{file} -noout -serial 得到了我需要的答案!非常感谢! @Ziyan Junaideen
    【解决方案2】:

    标准库中有模块OpenSSL,包含类OpenSSL::X509::Certificate,方法为#serial

    【讨论】:

    • 谢谢,我用了这个:new_file = File.read(Rails.root.join('lib', 'cer', '2', 'LAN7008173R5.cer')) translate = OpenSSL:: X509::Certificate.new new_file 但得到了这个:#<:x509::certificate: subject="#<OpenSSL::X509::Name:0x005603888a9ee0">, issuer=#<:x509::name:0x005603888a9f58> , serial=#<:bn:0x005603888a9f80>, not_before=2016-10-25 21:52:11 UTC, not_after=2020-10-25 21:52:11 UTC> 但是当我在 shell I 中运行命令时得到:serial="serial=3230303031303030303030333030303232383135"(这是预期的结果)
    • 对不起,我不明白你的问题。现在您在变量translate 中获得了证书对象。你打电话给translate.serial了吗?你收到了什么?
    • 是的,我得到了 => #<:bn:0x0055747e71bd88> 但解决了:file = File.join(Rails.root, 'lib', 'cer', '2', 'LAN7008173R5.cer') 序列 = openssl x509 -inform DER -in #{file} -noout -serial 得到这个:“serial=3230303031303030303030333030303232383135\n”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 2017-09-13
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    相关资源
    最近更新 更多