在Ruby/Rails中的openssl smime [英] openssl smime in ruby/rails
问题描述
所以,我有这个应用程序,可以创建包含图像和内容的zip文件
So, i have this application that creates a zip file with images and stuff
,我想使用smime
签名.
如果我使用终端命令:
openssl smime -binary -sign -passin "pass:MYPASS" -signer ./MyCertificate.pem -inkey ./MyKey.pem -in ./manifest.in -out ./signature.out -outform DER
格式化:
openssl smime -binary -sign -passin "pass:MYPASS" \
-signer ./MyCertificate.pem -inkey ./MyKey.pem \
-in ./manifest.in -out ./signature.out -outform DER
manifest.in
是包含要签名文本的文件,而signature.out
是输出文件.
the manifest.in
is the file witch contains the text to be signed and signature.out
is the output file.
我对签名不了解很多,但是我相信这段代码正在使用PKCS7对我的文件进行签名
i don't know a lot about signing but i believe this code is signing my file using PKCS7
如何使用ruby/rails重新创建相同的结果?
how can i recreate the same result with ruby/rails?
我尝试查看 OpenSSL ,但我找不到对我有用的任何东西
i have tried to look in the documentation of OpenSSL but i couldn't find anything usefull for me
编辑
如果这可以帮助某人, 这就是文档所说的
if this helps someone, this is what the documentation says
我需要建立一个:
A detached PKCS#7 signature of the manifest
推荐答案
找到了一种方法.
像这样:
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
这篇关于在Ruby/Rails中的openssl smime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!