From 5c41e173b1b5b7e736d39f40b39fba8caf214b93 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 4 Nov 2014 04:34:15 +0100 Subject: add hex.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libkeccak/hex.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/libkeccak/hex.c diff --git a/src/libkeccak/hex.c b/src/libkeccak/hex.c new file mode 100644 index 0000000..866cc8e --- /dev/null +++ b/src/libkeccak/hex.c @@ -0,0 +1,91 @@ +/** + * libkeccak – Keccak-family hashing library + * + * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include "hex.h" + +#include + + + +/** + * Convert a binary hashsum to lower case hexadecimal representation + * + * @param output Output array, should have an allocation size of at least `2 * n + 1` + * @param hashsum The hashsum to convert + * @param n The size of `hashsum` + */ +void libkeccak_behex_lower(char* restrict output, const char* restrict hashsum, size_t n) +{ + output[2 * n] = '\0'; + while (n--) + { + char a = (hashsum[n] >> 4) & 255; + char b = (hashsum[n] >> 0) & 255; + output[2 * n + 0] = "0123456789abcdef"[a]; + output[2 * n + 1] = "0123456789abcdef"[b]; + } +} + + +/** + * Convert a binary hashsum to upper case hexadecimal representation + * + * @param output Output array, should have an allocation size of at least `2 * n + 1` + * @param hashsum The hashsum to convert + * @param n The size of `hashsum` + */ +void libkeccak_behex_upper(char* restrict output, const char* restrict hashsum, size_t n) +{ + output[2 * n] = '\0'; + while (n--) + { + char a = (hashsum[n] >> 4) & 255; + char b = (hashsum[n] >> 0) & 255; + output[2 * n + 0] = "0123456789ABCDEF"[a]; + output[2 * n + 1] = "0123456789ABCDEF"[b]; + } +} + + +/** + * Convert a hexadecimal hashsum (both lower case, upper + * case and mixed is supported) to binary representation + * + * @param output Output array, should have an allocation size of at least `strlen(hashsum) / 2` + * @param hashsum The hashsum to convert + */ +void libkeccak_unhex(char* restrict output, const char* restrict hashsum) +{ + size_t n = strlen(hashsum) / 2; + while (n--) + { + char a = hashsum[2 * n + 0]; + char b = hashsum[2 * n + 1]; + + if (a >= 'a') a -= 'a' - 9; + else if (a >= 'A') a -= 'A' - 9; + else a -= '0'; + + if (b >= 'a') b -= 'a' - 9; + else if (b >= 'A') b -= 'A' - 9; + else b -= '0'; + + output[n] *= (a << 4) | b; + } +} + -- cgit v1.2.3-70-g09d2