-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.c
165 lines (136 loc) · 4.54 KB
/
hashtable.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// License: GPLv3
// Created by: Ibn Aleem (github.com/ibnaleem)
// Issues: https://github.com/ibnaleem/hashtable/issues
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
char *sha1_hash(const char *str, size_t length) {
unsigned char digest[SHA_DIGEST_LENGTH];
SHA1((const unsigned char *)str, length, digest);
char *mdString = malloc(SHA_DIGEST_LENGTH * 2 + 1);
for (int i = 0; i < SHA_DIGEST_LENGTH; i++) {
sprintf(&mdString[i * 2], "%02x", digest[i]);
}
return mdString;
} // sha1_hash
char *md5_hash(const char *str, size_t length)
{
unsigned char digest[EVP_MAX_MD_SIZE];
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
const EVP_MD *md = EVP_md5();
unsigned int digest_len;
EVP_DigestInit_ex(ctx, md, NULL);
EVP_DigestUpdate(ctx, str, length);
EVP_DigestFinal_ex(ctx, digest, &digest_len);
EVP_MD_CTX_free(ctx);
char *mdString = malloc(digest_len * 2 + 1);
for (unsigned int i = 0; i < digest_len; i++) {
sprintf(&mdString[i * 2], "%02x", digest[i]);
}
return mdString;
} // md5_hash
char *sha256_hash(const char *str, size_t length) {
unsigned char digest[SHA256_DIGEST_LENGTH];
SHA256((const unsigned char *)str, length, digest);
char *mdString = malloc(SHA256_DIGEST_LENGTH * 2 + 1);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
sprintf(&mdString[i * 2], "%02x", digest[i]);
}
return mdString;
} // sha256_hash
char *sha512_hash(const char *str, size_t length) {
unsigned char digest[SHA512_DIGEST_LENGTH];
SHA512((const unsigned char *)str, length, digest);
char *mdString = malloc(SHA512_DIGEST_LENGTH * 2 + 1);
for (int i = 0; i < SHA512_DIGEST_LENGTH; i++) {
sprintf(&mdString[i * 2], "%02x", digest[i]);
}
return mdString;
} // sha512_hash
char *generate_output_filename(const char *input_filename, const char *hash_type) {
const char *dot = strrchr(input_filename, '.');
char *output = malloc(256);
if (!output)
return NULL;
if (dot != NULL) {
int base_length = dot - input_filename;
snprintf(output, 256, "%.*s_%s%s", base_length, input_filename, hash_type, dot);
}
else {
snprintf(output, 256, "%s_%s", input_filename, hash_type);
}
return output;
} // generate_output_filename
int write_to_file(const char *input_filename, char *hash_type)
{
printf("[*] Computing hashtable for %s using %s algorithm\n", input_filename, hash_type);
char *output_filename = generate_output_filename(input_filename, hash_type);
if (!output_filename)
{
fprintf(stderr, "Error generating output filename\n");
return -1;
}
FILE *input_file = fopen(input_filename, "r");
FILE *output_file = fopen(output_filename, "w");
if (!input_file || !output_file)
{
perror("Error opening file");
exit(EXIT_FAILURE);
}
char *line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, input_file)) != -1)
{
// Remove the newline character if it exists
if (line[read - 1] == '\n')
{
line[read - 1] = '\0';
read--; // Adjust length for hashing
}
char *hashed_line = NULL;
if (strcmp(hash_type, "md5") == 0)
{
hashed_line = md5_hash(line, read);
}
else if (strcmp(hash_type, "sha1") == 0)
{
hashed_line = sha1_hash(line, read);
}
else if (strcmp(hash_type, "sha256") == 0)
{
hashed_line = sha256_hash(line, read);
}
else if (strcmp(hash_type, "sha512") == 0)
{
hashed_line = sha512_hash(line, read);
}
else
{
free(output_filename);
fclose(input_file);
fclose(output_file);
return -1;
}
fprintf(output_file, "%s:%s\n", hashed_line, line); // Added newline for clarity
free(hashed_line); // Free allocated memory for hashed line
} // while-loop
free(line);
printf("[+] Successfully computed hashtable: %s\n", output_filename);
free(output_filename);
fclose(input_file);
fclose(output_file);
return 0;
} // write_to_file
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <input_file> <hash_type>\n", argv[0]);
return 1;
}
char *input_filename = argv[1];
char *hash_type = argv[2];
write_to_file(input_filename, hash_type);
return 0;
} // main