使用openssl进行base64文件摘要计算

发布于 2024-10-17 10:59:31 字数 815 浏览 4 评论 0原文

我正在尝试找出编码以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉态萌生 2024-10-24 10:59:31

该命令序列看起来是正确的。您可以通过使用 shell 重定向而不是临时文件来简化它:

base64 -d b64.txt | openssl dgst -sha1 -binary | base64

要使用 OpenSSL 库在 C 中执行相同的操作,您可以使用 BIO 抽象来达到良好的效果:

#include <stdio.h>
#include <openssl/bio.h>
#include <openssl/evp.h>

int main(int argc, char *argv[])
{
    BIO *bio_in, *b64, *md, *bio_out;
    char buf[1024];
    char mdbuf[EVP_MAX_MD_SIZE];
    int mdlen;

    /* setup input BIO chain */
    bio_in = BIO_new_fp(stdin, BIO_NOCLOSE);

    b64 = BIO_new(BIO_f_base64());
    bio_in = BIO_push(b64, bio_in);

    md = BIO_new(BIO_f_md());
    BIO_set_md(md, EVP_sha1());
    bio_in = BIO_push(md, bio_in);

    /* reading through the MD BIO calculates the digest */
    while (BIO_read(bio_in, buf, sizeof buf) > 0)
        ;

    /* retrieve the message digest */
    mdlen = BIO_gets(md, mdbuf, sizeof mdbuf);

    /* setup output BIO chain */
    bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);

    b64 = BIO_new(BIO_f_base64());
    bio_out = BIO_push(b64, bio_out);

    /* write out digest */
    BIO_write(bio_out, mdbuf, mdlen);
    BIO_flush(bio_out);

    BIO_free_all(bio_in);
    BIO_free_all(bio_out);

    return 0;
}

上面的程序将读取 base64 输入stdin,并将 Base64 编码的 SHA1 哈希写入 stdout

That sequence of commands looks correct. You can simplify it by using shell redirection instead of temporary files:

base64 -d b64.txt | openssl dgst -sha1 -binary | base64

To do the same thing in C using the OpenSSL library, you can use the BIO abstraction to good effect:

#include <stdio.h>
#include <openssl/bio.h>
#include <openssl/evp.h>

int main(int argc, char *argv[])
{
    BIO *bio_in, *b64, *md, *bio_out;
    char buf[1024];
    char mdbuf[EVP_MAX_MD_SIZE];
    int mdlen;

    /* setup input BIO chain */
    bio_in = BIO_new_fp(stdin, BIO_NOCLOSE);

    b64 = BIO_new(BIO_f_base64());
    bio_in = BIO_push(b64, bio_in);

    md = BIO_new(BIO_f_md());
    BIO_set_md(md, EVP_sha1());
    bio_in = BIO_push(md, bio_in);

    /* reading through the MD BIO calculates the digest */
    while (BIO_read(bio_in, buf, sizeof buf) > 0)
        ;

    /* retrieve the message digest */
    mdlen = BIO_gets(md, mdbuf, sizeof mdbuf);

    /* setup output BIO chain */
    bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);

    b64 = BIO_new(BIO_f_base64());
    bio_out = BIO_push(b64, bio_out);

    /* write out digest */
    BIO_write(bio_out, mdbuf, mdlen);
    BIO_flush(bio_out);

    BIO_free_all(bio_in);
    BIO_free_all(bio_out);

    return 0;
}

The above program will read base64 input on stdin, and write the base64 encoded SHA1 hash to stdout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文