summaryrefslogtreecommitdiffstats
path: root/sha1/test/test1.c
diff options
context:
space:
mode:
Diffstat (limited to 'sha1/test/test1.c')
-rw-r--r--sha1/test/test1.c138
1 files changed, 138 insertions, 0 deletions
diff --git a/sha1/test/test1.c b/sha1/test/test1.c
new file mode 100644
index 0000000..7dcde2e
--- /dev/null
+++ b/sha1/test/test1.c
@@ -0,0 +1,138 @@
+/*******************************************************************************
+ * Teeny SHA-1
+ *
+ * The below sha1digest() calculates a SHA-1 hash value for a
+ * specified data buffer and generates a hex representation of the
+ * result. This implementation is a re-forming of the SHA-1 code at
+ * https://github.com/jinqiangshou/EncryptionLibrary.
+ *
+ * Copyright (c) 2017 CTrabant
+ *
+ * License: MIT, see included LICENSE file for details.
+ *
+ * To use the sha1digest() function either copy it into an existing
+ * project source code file or include this file in a project and put
+ * the declaration (example below) in the sources files where needed.
+ ******************************************************************************/
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define print_be(a) \
+ printf(#a "= %02x %02x %02x %02x\n", \
+ (a >> 0)&0xff,\
+ (a >> 8)&0xff,\
+ (a >> 16)&0xff,\
+ (a >> 24)&0xff);
+
+/* Declaration:
+extern int sha1digest(uint8_t *digest, char *hexdigest, const uint8_t *data, size_t databytes);
+*/
+
+/*******************************************************************************
+ * sha1digest: https://github.com/CTrabant/teeny-sha1
+ *
+ * Calculate the SHA-1 value for supplied data buffer and generate a
+ * text representation in hexadecimal.
+ *
+ * Based on https://github.com/jinqiangshou/EncryptionLibrary, credit
+ * goes to @jinqiangshou, all new bugs are mine.
+ *
+ * @input:
+ * data -- data to be hashed
+ * databytes -- bytes in data buffer to be hashed
+ *
+ * @output:
+ * digest -- the result, MUST be at least 20 bytes
+ * hexdigest -- the result in hex, MUST be at least 41 bytes
+ *
+ * At least one of the output buffers must be supplied. The other, if not
+ * desired, may be set to NULL.
+ *
+ * @return: 0 on success and non-zero on error.
+ ******************************************************************************/
+int main(void)
+{
+#define SHA1ROTATELEFT(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
+
+ uint32_t W[80];
+ uint32_t H[] = {0x67452301,
+ 0xEFCDAB89,
+ 0x98BADCFE,
+ 0x10325476,
+ 0xC3D2E1F0};
+ uint32_t a;
+ uint32_t b;
+ uint32_t c;
+ uint32_t d;
+ uint32_t e;
+ uint32_t f = 0;
+ uint32_t k = 0;
+
+ uint32_t idx;
+ uint32_t lidx;
+ uint32_t widx;
+ uint32_t didx = 0;
+
+ int32_t wcount;
+ uint32_t temp;
+
+{
+
+ /* Main loop */
+ a = H[0];
+ b = H[1];
+ c = H[2];
+ d = H[3];
+ e = H[4];
+
+ for (idx = 0; idx <= 79; idx++)
+ {
+ if (idx <= 19)
+ {
+ f = (b & c) | ((~b) & d);
+ k = 0x5A827999;
+ }
+ else if (idx >= 20 && idx <= 39)
+ {
+ f = b ^ c ^ d;
+ k = 0x6ED9EBA1;
+ }
+ else if (idx >= 40 && idx <= 59)
+ {
+ f = (b & c) | (b & d) | (c & d);
+ k = 0x8F1BBCDC;
+ }
+ else if (idx >= 60 && idx <= 79)
+ {
+ f = b ^ c ^ d;
+ k = 0xCA62C1D6;
+ }
+ print_be(f);
+ temp = SHA1ROTATELEFT (a, 5) + f + e + k;
+ e = d;
+ d = c;
+ c = SHA1ROTATELEFT (b, 30);
+ b = a;
+ a = temp;
+
+ printf("Round %d\n",idx);
+
+ print_be(a);
+ print_be(b);
+ print_be(c);
+ print_be(d);
+ print_be(e);
+ }
+
+ H[0] += a;
+ H[1] += b;
+ H[2] += c;
+ H[3] += d;
+ H[4] += e;
+ }
+
+ return 0;
+} /* End of sha1digest() */