-
Notifications
You must be signed in to change notification settings - Fork 144
/
sample12.c
45 lines (36 loc) · 1.1 KB
/
sample12.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
// Fowler-Noll-Vo Hash (FNV1a)
#include <assert.h>
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// default values recommended by http://isthe.com/chongo/tech/comp/fnv/
const uint32_t Prime = 0x01000193; // 16777619
const uint32_t Seed = 0x811C9DC5; // 2166136261
// hash a single byte
uint32_t fnv1a_byte(unsigned char oneByte, uint32_t hash) {
return (oneByte ^ hash) * Prime;
}
/// hash a 32 bit integer (four bytes)
uint32_t fnv1a_dword(uint32_t fourBytes, uint32_t hash) {
const unsigned char* ptr = (const unsigned char*) &fourBytes;
hash = fnv1a_byte(*ptr++, hash);
hash = fnv1a_byte(*ptr++, hash);
hash = fnv1a_byte(*ptr++, hash);
return fnv1a_byte(*ptr , hash);
}
uint32_t SECRET(unsigned long input) {
return fnv1a_dword((uint32_t)input, Seed);
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("Call this program with 1 arguments\n");
return 1;
}
unsigned long input = strtoul(argv[1], 0, 10);
unsigned long output = SECRET(input);
printf("%lu\n", output);
return 0;
}