-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sha1.c
92 lines (80 loc) · 2 KB
/
test_sha1.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
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sha1.h" /* SHA1_ */
int main(const int argc, const char *argv[])
{
#ifdef DEBUG
printf("DEBUG MODE!\n");
#endif
/*
* STEP 1
* allocate a SHA1 object, initialize counter variables
*/
SHA1_SHA1Object_p_t sha1_p = (SHA1_SHA1Object_p_t)malloc(sizeof(SHA1_SHA1Object_t));
SHA1_ERRCODE err = 0;
long int fsize = 0; /* file size */
long int fsize_read = 0; /* to check against fsize */
FILE *file_p = NULL; /* file pointer */
/*
* Open file. If file is too large, say 5MB, do not allocate.
*/
file_p = fopen(argv[1], "r");
if (file_p == NULL)
{
printf("Pointer to file is NULL! Exiting!\n");
return 1;
}
fseek(file_p, SEEK_SET, 0L);
fseek(file_p, 0L, SEEK_END);
fsize = ftell(file_p);
if (fsize > (5 * 1024 * 1024))
{
printf("File %s larger than 5MiB (%ld). Not allocating.\n", argv[1], fsize);
return 1;
}
#ifdef DEBUG
printf("File size: %ld\n", fsize);
#endif
/*
* Return to beginning of file
* Allocate space for the file
*/
fseek(file_p, SEEK_SET, 0L);
void *msg_p = malloc(fsize);
if (msg_p == NULL)
{
printf("Allocating buffer for message returned NULL!\n");
return 1;
}
/*
* Read data into msg_p array, close file
*/
fsize_read = fread(msg_p, sizeof(char), fsize, file_p);
if (fsize_read != fsize)
{
printf("fread failed with %li\n", fsize_read);
return fsize_read;
}
fclose(file_p);
/*
* Invoke hash algorithm
*/
err = SHA1_process_message((const char *)msg_p, sha1_p);
if (err != SHA1_SUCCESS)
{
printf("ERROR CODE %i\n", err);
}
printf("Computed hash: ");
for (int i=0; i<5; i++)
{
printf("%08X", sha1_p->temp_hash[i]);
}
printf("\n");
/*
* free any dynamically allocated memory
*/
free(msg_p);
free(sha1_p);
return 0;
}