aboutsummaryrefslogtreecommitdiffstats
path: root/digest.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2019-02-09 18:20:57 +0100
committerMattias Andrée <maandree@kth.se>2019-02-09 18:20:57 +0100
commit242430d5e52cf8ee49dcd1701cf134e7454910c6 (patch)
treee3238ce604ffb0d80a4e122cd40d5807372f187f /digest.c
parentMerge pull request #2 from maaku/no-trailing-comma (diff)
downloadlibsha2-242430d5e52cf8ee49dcd1701cf134e7454910c6.tar.gz
libsha2-242430d5e52cf8ee49dcd1701cf134e7454910c6.tar.bz2
libsha2-242430d5e52cf8ee49dcd1701cf134e7454910c6.tar.xz
Change license to ISC and reorganise
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'digest.c')
-rw-r--r--digest.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/digest.c b/digest.c
new file mode 100644
index 0000000..8658388
--- /dev/null
+++ b/digest.c
@@ -0,0 +1,66 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+/**
+ * Absorb the last part of the message and output a hash
+ *
+ * @param state The hashing state
+ * @param message The message, in bits
+ * @param msglen The length of the message, zero if there is nothing more to absorb
+ * @param output The output buffer for the hash
+ */
+void
+libsha2_digest(struct libsha2_state *restrict state, const char *restrict message, size_t msglen, char *output)
+{
+ char *appendix;
+ size_t i, j, k, n;
+
+ if (msglen & ~7) {
+ libsha2_update(state, message, msglen & ~7);
+ message += msglen & ~7;
+ msglen &= 7;
+ }
+
+ k = 8 * state->chunk_size;
+ n = state->chunk_size + 8;
+ n = (k + (n % k)) % k;
+ n = n / 8 - 1;
+
+ appendix = state->appendix;
+ if (msglen) {
+ j = 7 - msglen;
+ *appendix = *message;
+ *appendix |= 1 << j;
+ *appendix &= ~((1 << j) - 1);
+ } else {
+ *appendix = (unsigned char)128;
+ }
+
+ k = state->message_size + msglen;
+ i = state->chunk_size / 8;
+ appendix += n + i - 1;
+ for (i = i < sizeof(size_t) ? i : sizeof(size_t); i--;)
+ *(appendix - i) = (unsigned char)((k >> (i * 8)) & 255);
+
+ n += state->chunk_size;
+ libsha2_update(state, state->appendix, n);
+
+ n = libsha2_algorithm_output_size(state->algorithm);
+ switch (state->algorithm) {
+ case LIBSHA2_224:
+ case LIBSHA2_256:
+ for (i = 0; i < 8; i++)
+ for (j = 0; j < (state->chunk_size / 16); j++)
+ if (k = (i + 1) * (state->chunk_size / 16) - j - 1, k < n)
+ output[k] = (char)((state->h.b32[i] >> (8 * j)) & 255);
+ break;
+
+ default:
+ for (i = 0; i < 8; i++)
+ for (j = 0; j < (state->chunk_size / 16); j++)
+ if (k = (i + 1) * (state->chunk_size / 16) - j - 1, k < n)
+ output[k] = (char)((state->h.b64[i] >> (8 * j)) & 255);
+ break;
+ }
+}