-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha1.c
42 lines (39 loc) · 1.03 KB
/
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
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <openssl/evp.h>
#include <string.h>
#include <pthread.h>
#include "mutil.h"
unsigned char *digest = NULL;
unsigned int size = 0;
char *input = NULL;
char *hdigest = NULL;
int
main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: sha1 <space separated strings as input>\n");
return 1;
}
for (int i = 1; i < argc; ++i) {
size += strlen(argv[i]);
}
size++; // for the NULL
//printf("allocating %d bytes for full string\n", size);
input = (char *)malloc(size*sizeof(char));
assert( input != NULL );
for (int i = 1; i < argc; ++i) {
if (i == 1) {
strcpy(input, argv[i]);
} else {
strcat(input, argv[i]);
}
}
//printf("input string is '%s'\n", input);
digest = digest_sha1((unsigned char *)input, strlen(input), &size);
hdigest = tohex(digest, size);
printf("%s\n", hdigest);
OPENSSL_free(digest);
free(hdigest);
free(input);
}