sha512:使用 openSSL 库的 C 程序

sha512: c program using the openSSL library

我在 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字节?仅此而已,还是我必须进行更多更改?

是的,这行得通。 man page for the SHA family of functions 列出以下内容:

 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

我正在使用 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