使用openssl进行base64文件摘要计算
我正在尝试找出编码以下 openssl 命令的方法:
场景:
给定:文件的 Base64 编码值 (b64.txt)
文件的 Base64 编码 sha1 摘要(恰好是该文件的 20 字节 sha1 摘要)。
问题:我必须使用 C 程序验证给定的文件摘要是否正确。
我的方法:
- 在编写代码之前,我首先尝试 openssl 命令来验证摘要。我是这样做的。
- 我首先解码了这个base64文件,然后找到了该文件的sha1摘要。
我不知道为什么我从来没有得到 20 字节值作为输出。经过反复试验,只有这些有效:
在 Linux 系统上,我执行了以下操作:
base64 -d b64.txt > dec.out
(dec.out 是文本和二进制(无法破译的)文本的混合)openssl dgst -sha1 -binary dec.out > sha1.bin
(假设 dec.out 作为二进制输入,我发现了二进制形式的摘要)base64 sha1.bin > sha1.b64(将 sha1 结果编码为 base64)
现在我的 sha1.b64 给出了一个 20 字节的摘要,与给我的摘要相同。
首先,我想知道命令顺序是否正确,以及是否有更简单的方法来做到这一点。
另外,使用 EVP_Digest* 如何对此进行编程(我的意思是在这些文件中指定了文件的输入格式?)
请澄清。
谢谢
I am trying to figure out the way to code the following openssl commands:
Scenario:
Given: Base64 encoded value of a file (b64.txt)
Base64 encoded sha1 digest of the file(exactly 20 byte sha1 digest of this file).
Problem: I have to verify with a C program if the given digest for the file is correct.
My method:
- I first tried openssl commands to verify the digest before writing a code. Here is how I did it.
- I decoded this base64 file first and then found the sha1 digest of the file.
I wasn't sure why I never got the 20byte value as output. And with trial and error only these worked:
On a linux system I did the following:
base64 -d b64.txt > dec.out
(dec.out was a mix of textual and binary(undecipherable) text)openssl dgst -sha1 -binary dec.out > sha1.bin
(I found out the digest in binary form assuming the dec.out as binary input)base64 sha1.bin > sha1.b64
(encoding the sha1 result in base64)
Now my sha1.b64 gave a 20byte digest which was the same as that given to me.
First of all I would like to know if the sequence of commands are correct in the first place and if there are easier ways to do it.
Also, with EVP_Digest* how to program this( I mean what input format of the file is specified in these?)
Please clarify.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该命令序列看起来是正确的。您可以通过使用 shell 重定向而不是临时文件来简化它:
要使用 OpenSSL 库在 C 中执行相同的操作,您可以使用
BIO
抽象来达到良好的效果:上面的程序将读取 base64 输入
stdin
,并将 Base64 编码的 SHA1 哈希写入stdout
。That sequence of commands looks correct. You can simplify it by using shell redirection instead of temporary files:
To do the same thing in C using the OpenSSL library, you can use the
BIO
abstraction to good effect:The above program will read base64 input on
stdin
, and write the base64 encoded SHA1 hash tostdout
.