-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
83 lines (59 loc) · 1.32 KB
/
hash.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
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 <unistd.h>
#include "hash.h"
unsigned char *md5hash(int fd, unsigned char *md) {
MD5_CTX c;
unsigned char buf[HASH_BUFSIZE];
int read_cnt;
off_t offset;
if(md == NULL) {
md = (char*)malloc(MD5_DIGEST_LENGTH);
}
if((offset = lseek(fd, 0, SEEK_CUR)) < 0) {
fprintf(stderr, "lseek error\n");
exit(1);
}
lseek(fd, 0, SEEK_SET);
MD5_Init(&c);
while(1) {
if((read_cnt = read(fd, buf, HASH_BUFSIZE)) <= 0)
break;
MD5_Update(&c, buf, read_cnt);
}
MD5_Final(md, &c);
lseek(fd, offset, SEEK_SET);
return md;
}
unsigned char *sha1hash(int fd, unsigned char *md) {
SHA_CTX c;
unsigned char buf[HASH_BUFSIZE];
int read_cnt;
off_t offset;
if(md == NULL) {
md = (char*)malloc(SHA_DIGEST_LENGTH);
}
if((offset = lseek(fd, 0, SEEK_CUR)) < 0) {
fprintf(stderr, "lseek error\n");
exit(1);
}
lseek(fd, 0, SEEK_SET);
SHA1_Init(&c);
while(1) {
if((read_cnt = read(fd, buf, HASH_BUFSIZE)) <= 0)
break;
SHA1_Update(&c, buf, read_cnt);
}
SHA1_Final(md, &c);
lseek(fd, offset, SEEK_SET);
return md;
}
char *hash_to_str(char *buf, unsigned char *hash, int hash_len) {
if(hash == NULL)
return NULL;
if(buf == NULL)
buf = (char*)malloc(hash_len * 2 + 1);
for(int i = 0; i < hash_len; i++)
sprintf(buf + i * 2, "%02x", hash[i]);
return buf;
}