使用SHA256摘要进行OpenSSL RSA签名

时间:2022-05-28 05:02:36

I'm trying to figure out the equivalent shell script of a small ruby script I have. This is the ruby script:

我试图弄清楚我所拥有的小红宝石脚本的等效shell脚本。这是ruby脚本:

require 'openssl'
require 'base64'

k = OpenSSL::PKey::RSA.new(File.read("key.pem"))
res = File.read("res.tmp")
digest = OpenSSL::Digest::SHA256.new
signature = k.sign(digest,res)
File.write("foo1.txt",Base64.strict_encode64(signature))

That's it. It takes some data, gets the SHA256 hash of it, and then signs that with a private key I have. I think that the equivalent command on the terminal should be:

而已。它需要一些数据,获取它的SHA256哈希,然后用我拥有的私钥签名。我认为终端上的等效命令应该是:

openssl sha -sha256 -sign key.pem < res.tmp | base64 -w 0 > foo2.txt

But these do not produce the same output. Could anyone enlighten me as to why?

但这些不会产生相同的输出。任何人都可以告诉我为什么?

-- EDIT --

- 编辑 -

I'm adding some more information so that people can try to reproduce this on their computers if so desired. The contents of res.tmp are:

我正在添加更多信息,以便人们可以尝试在他们的计算机上重现这一点,如果需要的话。 res.tmp的内容是:

This is some sample text. 
This is some sample text. 
This is some sample text. 
This is some more sample text. 
This is some more sample text. 
This is some more sample text.

The private key (which is not a private key that is actually used for any production system, just clarifying that) is:

私钥(不是实际用于任何生产系统的私钥,只是说明这一点)是:

-----BEGIN PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALNY2EtJTj78tvnL
G1v+5HFqn8mZ85AToUqj/n86/eymcMqEjskHAoVmU9FOr+ZsnSxopNvUVLCzDkv6
w88tUtHGblzFtzJpQrxvtH8VCd2BNUc6Esxx7lmQeCxJMMPnmY2ZkDZuH6L+UsVL
DbM7LCvJyluo/A3AP68G9YL+XTjFAgMBAAECgYBkJzsyX894UTwFJq0ypJcB1x9A
P97KGIw72HTorBLdMt1N2tS54lZAFLK98gk8zm6/O/jEYkChJHzZZUIv0gmq+HOG
CHLyBNsf3BzmBXmwLRIOf54MxwaafysjpHifuYB4Po7G/utvlYqIxg1mBo/KtnMC
Wl2Enmrunei6gN3SAQJBAN94UfraP7lG50OYWhkcso95xpGrCeoFT64dV9I56FhU
q4tD6SSeIXj50hvSH2MQMdFBjsUHPIt+X7zw17ywgwUCQQDNdETg4FD3YT1xolWD
IFEowLlKfNqSaDIQia0dMIzk1/IqOoYhRKH03A97Wae1w/iyMgefyOVvkk+FZUs7
r0rBAkAnZox1wTNJFIJD/cGs+c1V1K+5EUIPO95/oXbRfxpDMLKKPHAH38WhEdME
yrhz++/8qCVnAc6f/akdpA01nJ2NAkEArC1GE9aow8fgADz0wMDygt6P6ZacbZmY
azeVtiKb0KQQM8d75KFpwJQy/UJzQ+aJonw+2282p7vLnJT46XnLgQJALxzSBu0D
hXIDTAMBpon+zgXjiy0ndiigLDPh1jJyCXrH0tBwj3FN+Br3yxbZT2LmE6PLIaJy
rkTCKqlnkG2h+w==
-----END PRIVATE KEY-----

Now, anyone should be able to run the commands I gave above and get the same results. The shell script produces this as its output:

现在,任何人都应该能够运行我上面给出的命令并获得相同的结果。 shell脚本生成它作为输出:

l4BxJyo/GQ0vUF5YR/vO7NtX5Sn/9bGfNHiVGS+W1CMfrwVlCtT0afHERXowx5T8mOiZ90VCJioHMj9Z6ssmfF1SpUbpoo1HYwNBCAfEcIjPLBj4N4KDLpy4gbMZtHEo2B8DZitYLwYDyvkCEudrMiU9b39DqOL+p3pwjJxT5iE=

With no trailing newline of course. The ruby script produces:

当然没有尾随换行符。 ruby脚本产生:

l4BxJyo/GQ0vUF5YR/vO7NtX5Sn/9bGfNHiVGS+W1CMfrwVlCtT0afHERXowx5T8mOiZ90VCJioHMj9Z6ssmfF1SpUbpoo1HYwNBCAfEcIjPLBj4N4KDLpy4gbMZtHEo2B8DZitYLwYDyvkCEudrMiU9b39DqOL+p3pwjJxT5iE=

Also without a trailing newline. I've only base 64 encoded the results to make them presentable to humans. That part is not the problem. The real issue is that my invocations of openssl in the ruby script and the shell script are doing different things. In the comments people keep asking about intermediate results. Just to be clear, the only thing that can be considered an "intermediate result" in the raw binary result before the base64 encoding (and it would be both unhelpful and impossible for me to post that here). The openssl command I'm running is just one opaque command (in both scripts), so I don't have access to the SHA256 hash in either case. I suppose that it would be generated with this command:

也没有尾随换行符。我只用64编码结果,使它们可以呈现给人类。那部分不是问题。真正的问题是我在ruby脚本和shell脚本中调用openssl正在做不同的事情。在评论中,人们不断询问中间结果。为了清楚起见,在base64编码之前的原始二进制结果中唯一可以被认为是“中间结果”的东西(这对我来说在这里发布它是无益的,也是不可能的)。我正在运行的openssl命令只是一个不透明的命令(在两个脚本中),所以在任何一种情况下我都无法访问SHA256哈希。我想它会用这个命令生成:

sha256sum res.tmp

Which gives this at the command line:

在命令行中给出了这个:

688a84cb84ce3b203460a2775248a31d4c3e9590a41ba27134dc342c328b8f9c  res.tmp

But I cannot be certain that the hash above is actually any intermediate result of either of the two scripts. Let me know if I can provide more information. Thanks.

但我无法确定上面的哈希实际上是两个脚本中任何一个的任何中间结果。如果我能提供更多信息,请告诉我。谢谢。

1 个解决方案

#1


1  

Somehow, I ended up mistaken, and it appears that these two scripts are indeed producing the same output. Sorry.

不知何故,我最终错了,看来这两个脚本确实产生了相同的输出。抱歉。

#1


1  

Somehow, I ended up mistaken, and it appears that these two scripts are indeed producing the same output. Sorry.

不知何故,我最终错了,看来这两个脚本确实产生了相同的输出。抱歉。