-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhash.c
More file actions
38 lines (32 loc) · 726 Bytes
/
hash.c
File metadata and controls
38 lines (32 loc) · 726 Bytes
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
#include "types.h"
#include "user.h"
int
main(int argc, char *argv[])
{
static const char hex_chars[] = "0123456789abcdef";
int i, n, size, fd;
char h[32];
char buffer[2048];
if(argc != 2){
printf(2, "usage: %s [file]\n", argv[0]);
exit();
}
if((fd = open(argv[1], 0)) < 0){
printf(1, "%s: cannot open '%s'\n", argv[0], argv[1]);
exit();
}
while((n = read(fd, buffer, sizeof(buffer))) > 0)
size = +n;
close(fd);
n = hash(&buffer, &h, size);
if(n == -1){
printf(2, "hashing failed\n");
exit();
}
for(i = 0; i < 32; ++i){
printf (1, "%c", hex_chars[0xf & h[i] >> 4]);
printf (1, "%c", hex_chars[0xf & h[i]]);
}
printf(1, " - %s\n", argv[1]);
exit();
}