C语言/Openssl库实现SHA256


Creative Commons LicenseCreative Commons LicenseCreative Commons License

使用openssl的库函数实现文件和字符串的SHA256的摘要,依赖libssl/libcrypto

1
LDFLAGS = -lssl -lcrypto

1 OPENSSL主要的SHA HASH函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int SHA1_Init(SHA_CTX *c);
int SHA1_Update(SHA_CTX *c, const void *data, size_t len);
int SHA1_Final(unsigned char *md, SHA_CTX *c);
unsigned char *SHA1(const unsigned char *d, size_t n,
unsigned char *md);

int SHA224_Init(SHA256_CTX *c);
int SHA224_Update(SHA256_CTX *c, const void *data, size_t len);
int SHA224_Final(unsigned char *md, SHA256_CTX *c);
unsigned char *SHA224(const unsigned char *d, size_t n,
unsigned char *md);

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 SHA384_Init(SHA512_CTX *c);
int SHA384_Update(SHA512_CTX *c, const void *data, size_t len);
int SHA384_Final(unsigned char *md, SHA512_CTX *c);
unsigned char *SHA384(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);

2 SHA256测试程序

https://github.com/Jona-lee/sha256

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/sha.h>

static int is_filename(char *name)
{
if (access(name, F_OK) != -1)
return 1;
else
return 0;
}

#define READ_SIZE 32768
int sha256_file (char* path, unsigned char *digest)
{
char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
char *buffer = NULL;
const int bufsize = READ_SIZE;
int bytes_read = 0;

FILE *file = fopen(path, "rb");
if (!file) {
printf("%s: can not open %s\n", __func__, path);
return -1;
}

SHA256_Init(&sha256);

buffer = malloc(bufsize);
if (!buffer) {
printf("%s: malloc failed\n", __func__);
fclose(file);
return -1;
}

while((bytes_read = fread(buffer, 1, bufsize, file)))
SHA256_Update(&sha256, buffer, bytes_read);

SHA256_Final(digest, &sha256);

fclose(file);
free(buffer);

return 0;
}

void sha256_string(char* str, unsigned char *digest)
{
SHA256_CTX c;
SHA256_Init(&c);
SHA256_Update(&c, str, strlen(str));
SHA256_Final(digest, &c);
}

void print_hex_hash(unsigned char* digest, int length)
{
int i;
for(i=0; i < length; i++)
printf("%02x", digest[i]);
}

int main(int argc, char *argv[])
{
unsigned char sha256_digest[SHA256_DIGEST_LENGTH];
int i = 0;

for (i = 1; i < argc; i++) {
if (is_filename(argv[i])) {
printf("File('%s'): ", argv[i]);
sha256_file(argv[i], sha256_digest);
} else {
printf("String('%s'): ", argv[i]);
sha256_string(argv[i], sha256_digest);
}
print_hex_hash(sha256_digest, SHA256_DIGEST_LENGTH);
printf("\n");
}

return 0;
}

-------------本文结束感谢您的阅读-------------
如果文章对您有帮助,也可以打赏支持喔!