cryptutils

Cryptographic Software Utilities
git clone http://git.omkov.net/cryptutils
Log | Tree | Refs | README | LICENCE | Download

cryptutils/src/fnv1a64.c (86 lines, 2.2 KiB) -rw-r--r-- file download

08f264b Jamozed 2021-08-23 13:27:57
0
// fnv1a64.c, version 1.0.0
08f264b Jamozed 2021-08-23 13:27:57
1
// OMKOV cryptutils fnv1a64
08f264b Jamozed 2021-08-23 13:27:57
2
// Copyright (C) 2021, Jakob Wakeling
45f70e3 Jamozed 2022-03-06 17:15:30
3
// MIT Licence
08f264b Jamozed 2021-08-23 13:27:57
4
c01a723 Jamozed 2022-02-05 19:04:03
5
#include "util/error.c"
c01a723 Jamozed 2022-02-05 19:04:03
6
#include "util/optget.h"
08f264b Jamozed 2021-08-23 13:27:57
7
08f264b Jamozed 2021-08-23 13:27:57
8
#include <stdbool.h>
08f264b Jamozed 2021-08-23 13:27:57
9
#include <stdint.h>
08f264b Jamozed 2021-08-23 13:27:57
10
#include <stdio.h>
08f264b Jamozed 2021-08-23 13:27:57
11
08f264b Jamozed 2021-08-23 13:27:57
12
#define VERSION "1.0.0"
08f264b Jamozed 2021-08-23 13:27:57
13
08f264b Jamozed 2021-08-23 13:27:57
14
static struct lop lops[] = {
08f264b Jamozed 2021-08-23 13:27:57
15
	{ "help",    ARG_NUL, 256 },
08f264b Jamozed 2021-08-23 13:27:57
16
	{ "version", ARG_NUL, 257 },
08f264b Jamozed 2021-08-23 13:27:57
17
	{ "bsd",     ARG_NUL, 258 },
08f264b Jamozed 2021-08-23 13:27:57
18
	{ NULL, 0, 0 }
08f264b Jamozed 2021-08-23 13:27:57
19
};
08f264b Jamozed 2021-08-23 13:27:57
20
08f264b Jamozed 2021-08-23 13:27:57
21
static bool qflag;
08f264b Jamozed 2021-08-23 13:27:57
22
static int form;
08f264b Jamozed 2021-08-23 13:27:57
23
08f264b Jamozed 2021-08-23 13:27:57
24
static inline void fnv1a64(const char *file);
08f264b Jamozed 2021-08-23 13:27:57
25
08f264b Jamozed 2021-08-23 13:27:57
26
static void hlp(void);
08f264b Jamozed 2021-08-23 13:27:57
27
static void ver(void);
08f264b Jamozed 2021-08-23 13:27:57
28
08f264b Jamozed 2021-08-23 13:27:57
29
int main(int ac, char *av[]) { A0 = av[0];
08f264b Jamozed 2021-08-23 13:27:57
30
	struct opt opt = OPTGET_INIT; opt.str = "q"; opt.lops = lops;
08f264b Jamozed 2021-08-23 13:27:57
31
	for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) {
08f264b Jamozed 2021-08-23 13:27:57
32
	case 'q': { qflag = true; break; }
08f264b Jamozed 2021-08-23 13:27:57
33
	case 256: { hlp(); return 0; }
08f264b Jamozed 2021-08-23 13:27:57
34
	case 257: { ver(); return 0; }
08f264b Jamozed 2021-08-23 13:27:57
35
	case 258: { form = 1; break; }
08f264b Jamozed 2021-08-23 13:27:57
36
	default: { return 1; }
08f264b Jamozed 2021-08-23 13:27:57
37
	}
08f264b Jamozed 2021-08-23 13:27:57
38
	
08f264b Jamozed 2021-08-23 13:27:57
39
	if (opt.ind == ac) { fnv1a64(NULL); }
08f264b Jamozed 2021-08-23 13:27:57
40
	else for (char **p = &av[opt.ind]; *p; ++p) { fnv1a64(*p); }
08f264b Jamozed 2021-08-23 13:27:57
41
	
08f264b Jamozed 2021-08-23 13:27:57
42
	return warned;
08f264b Jamozed 2021-08-23 13:27:57
43
}
08f264b Jamozed 2021-08-23 13:27:57
44
08f264b Jamozed 2021-08-23 13:27:57
45
static inline void fnv1a64(const char *file) {
08f264b Jamozed 2021-08-23 13:27:57
46
	uint8_t buf[BUFSIZ * 16]; FILE *fi;
08f264b Jamozed 2021-08-23 13:27:57
47
	register uint64_t h = 0xCBF29CE484222325;
08f264b Jamozed 2021-08-23 13:27:57
48
	
08f264b Jamozed 2021-08-23 13:27:57
49
	if (!file) { fi = stdin; } else { fi = fopen(file, "rb"); }
08f264b Jamozed 2021-08-23 13:27:57
50
	if (!fi) { warn("%s: %s", file, serr()); return; }
08f264b Jamozed 2021-08-23 13:27:57
51
	
08f264b Jamozed 2021-08-23 13:27:57
52
	for (size_t c; (c = fread(buf, 1, sizeof (buf), fi));) {
08f264b Jamozed 2021-08-23 13:27:57
53
		for (register size_t i = 0; i != c; ++i) {
08f264b Jamozed 2021-08-23 13:27:57
54
			h ^= buf[i]; h *= 0x00000100000001B3;
08f264b Jamozed 2021-08-23 13:27:57
55
		}
08f264b Jamozed 2021-08-23 13:27:57
56
	}
08f264b Jamozed 2021-08-23 13:27:57
57
	
08f264b Jamozed 2021-08-23 13:27:57
58
	if (ferror(fi)) { fclose(fi); warn("%s: %s", file, serr()); return; }
08f264b Jamozed 2021-08-23 13:27:57
59
	
08f264b Jamozed 2021-08-23 13:27:57
60
	if (!file || qflag) { printf("%016lX\n", h); } else switch (form) {
08f264b Jamozed 2021-08-23 13:27:57
61
	case 0: { printf("%016lX *%s\n", h, file); break; }
08f264b Jamozed 2021-08-23 13:27:57
62
	case 1: { printf("FNV1A64 (%s) = %016lX\n", file, h); break; }
08f264b Jamozed 2021-08-23 13:27:57
63
	}
08f264b Jamozed 2021-08-23 13:27:57
64
	
08f264b Jamozed 2021-08-23 13:27:57
65
	if (fi != stdin) { fclose(fi); } return;
08f264b Jamozed 2021-08-23 13:27:57
66
}
08f264b Jamozed 2021-08-23 13:27:57
67
08f264b Jamozed 2021-08-23 13:27:57
68
/* Print help information */
08f264b Jamozed 2021-08-23 13:27:57
69
static void hlp(void) {
08f264b Jamozed 2021-08-23 13:27:57
70
	puts("fnv1a64 - Compute the FNV1a-64 for a file\n");
08f264b Jamozed 2021-08-23 13:27:57
71
	puts("usage: fnv1a64 [-q] [--bsd] [file...]\n");
08f264b Jamozed 2021-08-23 13:27:57
72
	puts("options:");
08f264b Jamozed 2021-08-23 13:27:57
73
	puts("  -q         Output only the hash value");
08f264b Jamozed 2021-08-23 13:27:57
74
	puts("  --bsd      Use BSD style hashes");
08f264b Jamozed 2021-08-23 13:27:57
75
	puts("  --help     Display help information");
08f264b Jamozed 2021-08-23 13:27:57
76
	puts("  --version  Display version information");
08f264b Jamozed 2021-08-23 13:27:57
77
}
08f264b Jamozed 2021-08-23 13:27:57
78
08f264b Jamozed 2021-08-23 13:27:57
79
/* Print version information */
08f264b Jamozed 2021-08-23 13:27:57
80
static void ver(void) {
08f264b Jamozed 2021-08-23 13:27:57
81
	puts("OMKOV cryptutils fnv1a64, version " VERSION);
08f264b Jamozed 2021-08-23 13:27:57
82
	puts("Copyright (C) 2021, Jakob Wakeling");
45f70e3 Jamozed 2022-03-06 17:15:30
83
	puts("MIT Licence (https://opensource.org/licenses/MIT)");
08f264b Jamozed 2021-08-23 13:27:57
84
}
85