aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-11-04 04:34:15 +0100
committerMattias Andrée <maandree@operamail.com>2014-11-04 04:34:15 +0100
commit5c41e173b1b5b7e736d39f40b39fba8caf214b93 (patch)
tree61c11102346c50af6d282e5762b62aca6c8ca4ff
parentadd digest.h and hex.h (diff)
downloadlibkeccak-5c41e173b1b5b7e736d39f40b39fba8caf214b93.tar.gz
libkeccak-5c41e173b1b5b7e736d39f40b39fba8caf214b93.tar.bz2
libkeccak-5c41e173b1b5b7e736d39f40b39fba8caf214b93.tar.xz
add hex.c
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--src/libkeccak/hex.c91
1 files changed, 91 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
+ */
+#include "hex.h"
+
+#include <string.h>
+
+
+
+/**
+ * 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;
+ }
+}
+