forked from albertobsd/keyhunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashing.c
149 lines (128 loc) · 4.57 KB
/
hashing.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
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/ripemd.h>
#include <string.h>
#include <stdio.h>
#include "hashing.h"
#include "sha3/sha3.h"
int sha256(const unsigned char *data, size_t length, unsigned char *digest) {
SHA256_CTX ctx;
if (SHA256_Init(&ctx) != 1) {
printf("Failed to initialize SHA256 context\n");
return 1;
}
if (SHA256_Update(&ctx, data, length) != 1) {
printf("Failed to update digest\n");
return 1;
}
if (SHA256_Final(digest, &ctx) != 1) {
printf("Failed to finalize digest\n");
return 1;
}
return 0; // Success
}
int sha256_4(size_t length, const unsigned char *data0, const unsigned char *data1,
const unsigned char *data2, const unsigned char *data3,
unsigned char *digest0, unsigned char *digest1,
unsigned char *digest2, unsigned char *digest3) {
SHA256_CTX ctx[4];
if (SHA256_Init(&ctx[0]) != 1 || SHA256_Init(&ctx[1]) != 1 ||
SHA256_Init(&ctx[2]) != 1 || SHA256_Init(&ctx[3]) != 1) {
printf("Failed to initialize SHA256 contexts\n");
return 1;
}
if (SHA256_Update(&ctx[0], data0, length) != 1 ||
SHA256_Update(&ctx[1], data1, length) != 1 ||
SHA256_Update(&ctx[2], data2, length) != 1 ||
SHA256_Update(&ctx[3], data3, length) != 1) {
printf("Failed to update digests\n");
return 1;
}
if (SHA256_Final(digest0, &ctx[0]) != 1 ||
SHA256_Final(digest1, &ctx[1]) != 1 ||
SHA256_Final(digest2, &ctx[2]) != 1 ||
SHA256_Final(digest3, &ctx[3]) != 1) {
printf("Failed to finalize digests\n");
return 1;
}
return 0; // Success
}
// Function for hashing
int keccak(const unsigned char *data, size_t length, unsigned char *digest) {
SHA3_256_CTX ctx;
SHA3_256_Init(&ctx);
SHA3_256_Update(&ctx,data,length);
KECCAK_256_Final(digest,&ctx);
return 0; // Success
}
int rmd160(const unsigned char *data, size_t length, unsigned char *digest) {
RIPEMD160_CTX ctx;
if (RIPEMD160_Init(&ctx) != 1) {
printf("Failed to initialize RIPEMD-160 context\n");
return 1;
}
if (RIPEMD160_Update(&ctx, data, length) != 1) {
printf("Failed to update digest\n");
return 1;
}
if (RIPEMD160_Final(digest, &ctx) != 1) {
printf("Failed to finalize digest\n");
return 1;
}
return 0; // Success
}
int rmd160_4(size_t length, const unsigned char *data0, const unsigned char *data1,
const unsigned char *data2, const unsigned char *data3,
unsigned char *digest0, unsigned char *digest1,
unsigned char *digest2, unsigned char *digest3) {
RIPEMD160_CTX ctx[4];
if (RIPEMD160_Init(&ctx[0]) != 1 || RIPEMD160_Init(&ctx[1]) != 1 ||
RIPEMD160_Init(&ctx[2]) != 1 || RIPEMD160_Init(&ctx[3]) != 1) {
printf("Failed to initialize RIPEMD-160 contexts\n");
return 1;
}
if (RIPEMD160_Update(&ctx[0], data0, length) != 1 ||
RIPEMD160_Update(&ctx[1], data1, length) != 1 ||
RIPEMD160_Update(&ctx[2], data2, length) != 1 ||
RIPEMD160_Update(&ctx[3], data3, length) != 1) {
printf("Failed to update digests\n");
return 1;
}
if (RIPEMD160_Final(digest0, &ctx[0]) != 1 ||
RIPEMD160_Final(digest1, &ctx[1]) != 1 ||
RIPEMD160_Final(digest2, &ctx[2]) != 1 ||
RIPEMD160_Final(digest3, &ctx[3]) != 1) {
printf("Failed to finalize digests\n");
return 1;
}
return 0; // Success
}
bool sha256_file(const char* file_name, uint8_t* digest) {
FILE* file = fopen(file_name, "rb");
if (file == NULL) {
printf("Failed to open file: %s\n", file_name);
return false;
}
uint8_t buffer[8192]; // Buffer to read file contents
size_t bytes_read;
SHA256_CTX ctx;
if (SHA256_Init(&ctx) != 1) {
printf("Failed to initialize SHA256 context\n");
fclose(file);
return false;
}
while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) > 0) {
if (SHA256_Update(&ctx, buffer, bytes_read) != 1) {
printf("Failed to update digest\n");
fclose(file);
return false;
}
}
if (SHA256_Final(digest, &ctx) != 1) {
printf("Failed to finalize digest\n");
fclose(file);
return false;
}
fclose(file);
return true;
}