English 中文(简体)
基于openssl的base64文件摘要计算
原标题:base64 file digest calculation with openssl
  • 时间:2011-02-15 09:28:00
  •  标签:
  • c
  • openssl

我正试图找出以下openssl命令的编码方法:

场景:

给定:文件的Base64编码值(b64.txt)

Base64编码的文件的sha1摘要(正好是该文件的20字节sha1摘要)。

问题:我必须用C程序验证给定的文件摘要是否正确。

我的方法:

  • 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.

我不知道为什么我从来没有得到20字节的值作为输出。经过反复试验,只有这些有效:

在linux系统上,我做了以下操作:

  • 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)

现在我的sha1.b64给出了一个20字节的摘要,和给我的一样。

首先,我想知道命令的顺序是否正确,以及是否有更简单的方法。

此外,使用EVP_Digest*如何对此进行编程(我的意思是,这些文件中指定了什么输入格式?)

请澄清。

谢谢

问题回答

这个命令序列看起来是正确的。您可以通过使用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;
}

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





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签