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BSuYY//jlIpvsfMIdHXfAsuan7w7PH87asp1wdb6lD9snvLZix1UGK7VQg6
wUFYNlMqJh1m7ITVvzhjdnx7EzCKkBXSxEom4mwbvSNvzqOKAWsDE0gvHQ9aCSby
NFBQQMoW94LqrG/kuIQtjwVdZA==
-----END NEW CERTIFICATE REQUEST-----

如何在Ruby中对其进行解码以获取属于请求主题的以下信息?

  def parse_csr(file_path)
    csr = OpenSSL::X509::Request.new(File.read(file_path))
    # puts csr.subject.to_a
    # => [["C", "RU", 19], ["ST", "State", 12], ["L", "City", 12], ["O", "CompanyName", 12], ["OU", "Unit", 12], ["CN", "App 1.0", 12], ["emailAddress", "user@company.com", 22]]
    csr.subject.to_a.inject({}) do |r, s|
      r.merge!(s[0] => s[1])
    end
  end

输出:

=> {"C"=>"RU", "ST"=>"State", "L"=>"City", "O"=>"Company Name", "OU"=>"Unit", "CN"=>"App v1.0", "emailAddress"=>"user@company.com"}