SHA512:使用OpenSSL库的C程序

4

我有一个用C语言编写的程序,用于计算输入文件的sha256哈希值。它使用了openSSL库。以下是程序的核心代码:

#include <openssl/sha.h>

SHA256_CTX ctx;
unsigned char buffer[512];

SHA256_Init(&ctx);
SHA256_Update(&ctx, buffer, len);
SHA256_Final(buffer, &ctx);

fwrite(&buffer,32,1,stdout);

我需要将它更改为计算sha512哈希。

我是否可以直接(天真地)将所有函数的名称从SHA256更改为SHA512,然后在最后一步中使用fwrite 64字节代替32字节?那就可以了吗?还是我需要做更多的更改?


1
请查看此链接:https://www.openssl.org/docs/man1.0.2/man3/SHA512.html - Manthan Tilva
1
我建议使用通用的EVP API,它可以让你使用任何支持的摘要算法来编写相同的代码。 - Shawn
2个回答

6

没问题,这会起作用。 SHA函数系列的man页面 列出了以下内容:

 int SHA256_Init(SHA256_CTX *c);
 int SHA256_Update(SHA256_CTX *c, const void *data, size_t len);
 int SHA256_Final(unsigned char *md, SHA256_CTX *c);
 unsigned char *SHA256(const unsigned char *d, size_t n,
                       unsigned char *md);

...

int SHA512_Init(SHA512_CTX *c);
 int SHA512_Update(SHA512_CTX *c, const void *data, size_t len);
 int SHA512_Final(unsigned char *md, SHA512_CTX *c);
 unsigned char *SHA512(const unsigned char *d, size_t n,
                       unsigned char *md);

...

SHA1_Init() initializes a SHA_CTX structure.

SHA1_Update() can be called repeatedly with chunks of the message to be hashed (len bytes at data).

SHA1_Final() places the message digest in md, which must have space for SHA_DIGEST_LENGTH == 20 bytes of output, and erases the SHA_CTX.

The SHA224, SHA256, SHA384 and SHA512 families of functions operate in the same way as for the SHA1 functions. Note that SHA224 and SHA256 use a SHA256_CTX object instead of SHA_CTX. SHA384 and SHA512 use SHA512_CTX. The buffer md must have space for the output from the SHA variant being used (defined by SHA224_DIGEST_LENGTH, SHA256_DIGEST_LENGTH, SHA384_DIGEST_LENGTH and SHA512_DIGEST_LENGTH). Also note that, as for the SHA1() function above, the SHA224(), SHA256(), SHA384() and SHA512() functions are not thread safe if md is NULL.

为了确认,让我们看一些代码段。首先是 SHA256:

SHA256_CTX ctx;
unsigned char buffer[512];

char *str = "this is a test";
int len = strlen(str);

strcpy(buffer,str);

SHA256_Init(&ctx);
SHA256_Update(&ctx, buffer, len);
SHA256_Final(buffer, &ctx);

fwrite(&buffer,32,1,stdout);

当作如下运行:

./test1 | od -t x1

输出:

0000000 2e 99 75 85 48 97 2a 8e 88 22 ad 47 fa 10 17 ff
0000020 72 f0 6f 3f f6 a0 16 85 1f 45 c3 98 73 2b c5 0c
0000040

这与以下输出相匹配:

echo -n "this is a test" | openssl sha256

翻译内容如下:

(stdin)= 2e99758548972a8e8822ad47fa1017ff72f06f3ff6a016851f45c398732bc50c

现在是您建议更改后的相同代码:

SHA512_CTX ctx;
unsigned char buffer[512];

char *str = "this is a test";
int len = strlen(str);

strcpy(buffer,str);

SHA512_Init(&ctx);
SHA512_Update(&ctx, buffer, len);
SHA512_Final(buffer, &ctx);

fwrite(&buffer,64,1,stdout);

当通过 "od" 传递时,输出如下:
0000000 7d 0a 84 68 ed 22 04 00 c0 b8 e6 f3 35 ba a7 e0
0000020 70 ce 88 0a 37 e2 ac 59 95 b9 a9 7b 80 90 26 de
0000040 62 6d a6 36 ac 73 65 24 9b b9 74 c7 19 ed f5 43
0000060 b5 2e d2 86 64 6f 43 7d c7 f8 10 cc 20 68 37 5c
0000100

这与以下输出相匹配:

echo -n "this is a test" | openssl sha512 

这是:

(stdin)= 7d0a8468ed220400c0b8e6f335baa7e070ce880a37e2ac5995b9a97b809026de626da636ac7365249bb974c719edf543b52ed286646f437dc7f810cc2068375c

我建议只分配所需大小的缓冲区:unsigned char buffer[512 / 8]; - Kai Petzke

1

我正在使用macOS和openssl。这对我有效:

#include <openssl/sha.h>
#include <stdio.h>
#include <string.h>

int main() {

  unsigned char data[] = "some text";
  unsigned char hash[SHA512_DIGEST_LENGTH];
  SHA512(data, strlen((char *)data), hash);

  for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
    printf("%02x", hash[i]);
  putchar('\n');
}

我使用以下命令进行编译:

~$ gcc -o sha512 sha512.c \
    -I /usr/local/opt/openssl/include \
    -L /usr/local/opt/openssl/lib \
    -lcrypto 

~S ./sha512
e2732baedca3eac1407828637de1dbca702c3fc9ece16cf536ddb8d6139cd85dfe7464b8235
b29826f608ccf4ac643e29b19c637858a3d8710a59111df42ddb5


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接