-
Notifications
You must be signed in to change notification settings - Fork 0
/
md5_example.c
49 lines (42 loc) · 1.03 KB
/
md5_example.c
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
#include <stdio.h>
/*
Output should be:
d41d8cd98f00b204e9800998ecf8427e
900150983cd24fb0d6963f7d28e17f72
d174ab98d277d9f5a5611c2c9f419d9f
*/
void print_hash(char hash[])
{
int idx;
for (idx=0; idx < 16; idx++)
printf("%02x",hash[idx]);
printf("\n");
}
int main()
{
char hash[16],
in1[]={""},
in2[]={"abc"},
in3_1[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"},
in3_2[]={"fghijklmnopqrstuvwxyz0123456789"};
unsigned int len;
MD5_CTX ctx;
// First hash
md5_init(&ctx);
md5_update(&ctx,in1,strlen(in1));
md5_final(&ctx,hash);
print_hash(hash);
// Second hash (note the MD5 object can be reused)
md5_init(&ctx);
md5_update(&ctx,in2,strlen(in2));
md5_final(&ctx,hash);
print_hash(hash);
// Third hash (note the data is being added in two chunks)
md5_init(&ctx);
md5_update(&ctx,in3_1,strlen(in3_1));
md5_update(&ctx,in3_2,strlen(in3_2));
md5_final(&ctx,hash);
print_hash(hash);
getchar();
return 0;
}