openssl smime in ruby/rails

送分小仙女□ 提交于 2019-12-04 17:18:58

Found a way.

like this:

  require 'secure_digest'

  def sign_manifest(manifest = {})
    manifest_str = manifest.to_json

    key4_pem = File.read Rails.root.join("lib", "keys", "key.pem")
    pass_phrase = "supera"

    key = OpenSSL::PKey::RSA.new key4_pem, pass_phrase
    cert = OpenSSL::X509::Certificate.new File.read Rails.root.join("lib", "keys", "certificate.pem")

    sign = OpenSSL::PKCS7.sign(cert, key, manifest_str, nil, OpenSSL::PKCS7::BINARY | OpenSSL::PKCS7::NOATTR | OpenSSL::PKCS7::DETACHED).to_der

    sign
  end

Just to clarify my code, manifest param is a hash witch i want to sign it using this code. if i want another item, like a image, string or file i just need do read it as string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!