Ruby SHA1符号返回的结果与OpenSSL符号不同

时间:2013-04-10 14:57:05

标签: ruby openssl

当我在linux命令行执行openssl命令时:

echo -n "aaaaaa" | /usr/bin/openssl dgst -sha1 -sign myprivatekey.pem | /usr/bin/openssl enc -base64 -A

我得到了这个结果:

2GmJqeVDbGFsaJjMKcjW4JhPYBdI7mSwNYKrmdHtM2xpWVVSdhXc0d3zidF790mz9n9nb2yX+jMZpzNNd8xAx/WeSRxnYCg0Y/TXoMG4ynbCnttpzxrN08TJNggLOz0fWbzfMQZbHy+WghU8SlOhQExngtH8yvtE71060C/KKtI=

通过运行这个ruby脚本:

require 'openssl'
require 'base64'

pkey = OpenSSL::PKey::RSA.new(File.read("myprivatekey.pem"))
plaintext = OpenSSL::Digest::SHA1.new("aaaaaa").digest
enc = pkey.private_encrypt(plaintext)
puts Base64.encode64(enc)

结果不同:

rHdXk0itL88vrz84QvCpRHM7J1w/B+Jaoo7n3fQ1D0UiPYQ4jClLQcO3iQLVifq/jMXBYvPLzQVZa/J9D7gjOwKYkKlu5c18HV8U4h0jXM7yTNCYlekhVsBThUcfb2kb1Lf2Ow3cjwL7ss66bvUc9+bwDGHtp3uJ9/GbJlZdphE=

任何人都可以帮助我吗?为什么我在红宝石部分做错了?我想复制linux命令行命令!

1 个答案:

答案 0 :(得分:1)

找到解决方案...... 签名与private_encrypt不同......但仍然不明白为什么:)

require 'openssl'
require 'base64'

pkey = OpenSSL::PKey::read(File.read("privatekey.pem"))
enc = pkey.sign(OpenSSL::Digest::SHA1.new,"aaaaaa")       
puts Base64.encode64(enc)