Author | Jamozed <[email protected]> |
Date | 2021-08-23 01:27:57 |
Commit | 08f264bf78304406c1a9af2d93f2dc8121c9760a |
Parent | 3275b9a753ccf3022037d481b71e18f3f652a4a2 |
fnv1a64: Add fnv1a64 utility
Diffstat
M | CMakeLists.txt | | | 10 | ++++------ |
A | src/fnv1a64.c | | | 115 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 119 insertions, 6 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index a6ba700..6de026e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,5 @@ -# CMakeLists.txt -# CMakeLists file for OMKOV cryptutils - CMAKE_MINIMUM_REQUIRED(VERSION 3.12) -PROJECT(cryptutils C) +PROJECT(cryptutils LANGUAGES C) SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib) SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) @@ -10,14 +7,15 @@ SET(CMAKE_STATIC_LIBRARY_PREFIX "") FIND_PACKAGE(OpenSSL REQUIRED) -FILE(GLOB LIBSRC ${PROJECT_SOURCE_DIR}/src/lib/*) +FILE(GLOB LIB ${PROJECT_SOURCE_DIR}/src/lib/*) -ADD_LIBRARY(lib STATIC ${LIBSRC}) +ADD_LIBRARY(lib STATIC ${LIB}) LINK_LIBRARIES(lib) ADD_EXECUTABLE(alder32 ${PROJECT_SOURCE_DIR}/src/alder32.c) ADD_EXECUTABLE(crc32 ${PROJECT_SOURCE_DIR}/src/crc32.c) +ADD_EXECUTABLE(fnv1a64 ${PROJECT_SOURCE_DIR}/src/fnv1a64.c) ADD_EXECUTABLE(otp ${PROJECT_SOURCE_DIR}/src/otp.c) TARGET_LINK_LIBRARIES(otp OpenSSL::Crypto) diff --git a/src/fnv1a64.c b/src/fnv1a64.c new file mode 100644 index 0000000..5ae0845 --- /dev/null +++ b/src/fnv1a64.c @@ -0,0 +1,115 @@ +// fnv1a64.c, version 1.0.0 +// OMKOV cryptutils fnv1a64 +// Copyright (C) 2021, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimers. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimers in the documentation and/or + other materials provided with the distribution. +* Neither the names of the copyright holders, nor the names of its contributors + may be used to endorse or promote products derived from this Software without + specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. +*/ + +#include "lib/error.c" +#include "lib/optget.h" + +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> + +#define VERSION "1.0.0" + +static struct lop lops[] = { + { "help", ARG_NUL, 256 }, + { "version", ARG_NUL, 257 }, + { "bsd", ARG_NUL, 258 }, + { NULL, 0, 0 } +}; + +static bool qflag; +static int form; + +static inline void fnv1a64(const char *file); + +static void hlp(void); +static void ver(void); + +int main(int ac, char *av[]) { A0 = av[0]; + struct opt opt = OPTGET_INIT; opt.str = "q"; opt.lops = lops; + for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { + case 'q': { qflag = true; break; } + case 256: { hlp(); return 0; } + case 257: { ver(); return 0; } + case 258: { form = 1; break; } + default: { return 1; } + } + + if (opt.ind == ac) { fnv1a64(NULL); } + else for (char **p = &av[opt.ind]; *p; ++p) { fnv1a64(*p); } + + return warned; +} + +static inline void fnv1a64(const char *file) { + uint8_t buf[BUFSIZ * 16]; FILE *fi; + register uint64_t h = 0xCBF29CE484222325; + + if (!file) { fi = stdin; } else { fi = fopen(file, "rb"); } + if (!fi) { warn("%s: %s", file, serr()); return; } + + for (size_t c; (c = fread(buf, 1, sizeof (buf), fi));) { + for (register size_t i = 0; i != c; ++i) { + h ^= buf[i]; h *= 0x00000100000001B3; + } + } + + if (ferror(fi)) { fclose(fi); warn("%s: %s", file, serr()); return; } + + if (!file || qflag) { printf("%016lX\n", h); } else switch (form) { + case 0: { printf("%016lX *%s\n", h, file); break; } + case 1: { printf("FNV1A64 (%s) = %016lX\n", file, h); break; } + } + + if (fi != stdin) { fclose(fi); } return; +} + +/* Print help information */ +static void hlp(void) { + puts("fnv1a64 - Compute the FNV1a-64 for a file\n"); + puts("usage: fnv1a64 [-q] [--bsd] [file...]\n"); + puts("options:"); + puts(" -q Output only the hash value"); + puts(" --bsd Use BSD style hashes"); + puts(" --help Display help information"); + puts(" --version Display version information"); + return; +} + +/* Print version information */ +static void ver(void) { + puts("OMKOV cryptutils fnv1a64, version " VERSION); + puts("Copyright (C) 2021, Jakob Wakeling"); + puts("All rights reserved."); + puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)"); + return; +}