-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrity.c
More file actions
43 lines (30 loc) · 1.08 KB
/
integrity.c
File metadata and controls
43 lines (30 loc) · 1.08 KB
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 <string.h>
#include <stdint.h>
// Convert bytes to hex string
void to_hex(const unsigned char *hash, int len, char *output) {
for (int i = 0; i < len; i++)
sprintf(output + (i * 2), "%02x", hash[i]);
output[len * 2] = '\0';
}
/* ------------------------------------
* LEVEL 3: Key-Mixed Rolling Hash
* ------------------------------------
* - Uses key as dynamic modifier
* - Adds feedback and modular arithmetic
* - Stronger custom design
*/
void key_mixed_rolling_hash(const char *data, int datalen, const char *key, unsigned char *hash, int hash_len) {
int keylen = strlen(key);
uint64_t acc = 0x123456789ABCDEFULL;
for (int i = 0; i < datalen; i++) {
unsigned char d = data[i];
unsigned char k = key[i % keylen];
acc ^= ((uint64_t)d << (i % 8)) | ((uint64_t)k << ((i + 3) % 8));
acc = (acc * 1315423911ULL + k * 2654435761ULL) ^ (acc >> 11);
}
for (int i = 0; i < hash_len; i++) {
acc = (acc >> 7) ^ (acc << 3) ^ key[i % keylen];
hash[i] = (acc ^ (acc >> 16)) & 0xFF;
}
}