-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·71 lines (51 loc) · 1.6 KB
/
main.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
// ©.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sha1_hash.h"
#define DMSG(msg_fmt, msg_args...) \
printf("%s(%04u): " msg_fmt "\n", __FILE__, __LINE__, ##msg_args)
int main(
int argc,
char **argv)
{
char hash_result[SHA1_HASH_BUFFER_SIZE];
char *target_string;
FILE *file_fp;
char *target_file;
int file_size;
unsigned char *file_buf;
// 處理字串.
target_string = "";
sha1_hash(target_string, strlen(target_string), hash_result, sizeof(hash_result));
DMSG("string (%s) = %s", target_string, hash_result);
target_string = "hellow";
sha1_hash(target_string, strlen(target_string), hash_result, sizeof(hash_result));
DMSG("string (%s) = %s", target_string, hash_result);
// 處理檔案.
target_file = "sha";
file_fp = fopen(target_file, "rb");
if(file_fp == NULL)
{
DMSG("call fopen(%s) fail [%s]", target_file, strerror(errno));
goto FREE_01;
}
fseek(file_fp, 0L, SEEK_END);
file_size = ftell(file_fp);
fseek(file_fp, 0L, SEEK_SET);
file_buf = (unsigned char *) malloc(file_size);
if(file_buf == NULL)
{
DMSG("call malloc(%d) fail [%s]", file_size, strerror(errno));
goto FREE_02;
}
fread(file_buf, sizeof(unsigned char), file_size, file_fp);
sha1_hash(file_buf, file_size, hash_result, sizeof(hash_result));
DMSG("file (%s) = %s", target_file, hash_result);
free(file_buf);
FREE_02:
fclose(file_fp);
FREE_01:
return 0;
}