]> git.tokkee.org Git - inkscape.git/commitdiff

Code

new hash tool
authorishmal <ishmal@users.sourceforge.net>
Mon, 2 Oct 2006 21:17:17 +0000 (21:17 +0000)
committerishmal <ishmal@users.sourceforge.net>
Mon, 2 Oct 2006 21:17:17 +0000 (21:17 +0000)
src/dom/util/digest.cpp [new file with mode: 0644]
src/dom/util/digest.h [new file with mode: 0644]

diff --git a/src/dom/util/digest.cpp b/src/dom/util/digest.cpp
new file mode 100644 (file)
index 0000000..3c30ad5
--- /dev/null
@@ -0,0 +1,1287 @@
+/**\r
+ * Secure Hashing Tool\r
+ * *\r
+ * Authors:\r
+ *   Bob Jamison\r
+ *\r
+ * Copyright (C) 2006 Bob Jamison\r
+ *\r
+ *  This library is free software; you can redistribute it and/or\r
+ *  modify it under the terms of the GNU Lesser General Public\r
+ *  License as published by the Free Software Foundation; either\r
+ *  version 2.1 of the License, or (at your option) any later version.\r
+ *\r
+ *  This library is distributed in the hope that it will be useful,\r
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ *  Lesser General Public License for more details.\r
+ *\r
+ *  You should have received a copy of the GNU Lesser General Public\r
+ *  License along with this library; if not, write to the Free Software\r
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
+ */\r
+\r
+#include "digest.h"\r
+\r
+\r
+//########################################################################\r
+//##  U T I L\r
+//########################################################################\r
+\r
+/**\r
+ * Use this to print out a 64-bit int when otherwise difficult\r
+ */\r
+/*\r
+static void pl(unsigned long long val)\r
+{\r
+    for (int shift=56 ; shift>=0 ; shift-=8)\r
+        {\r
+        int ch = (val >> shift) & 0xff;\r
+        printf("%02x", ch);\r
+        }\r
+}\r
+*/\r
+\r
+\r
+static char *hexDigits = "0123456789abcdef";\r
+\r
+static std::string toHex(const std::vector<unsigned char> &bytes)\r
+{\r
+    std::string str;\r
+    std::vector<unsigned char>::const_iterator iter;\r
+    for (iter = bytes.begin() ; iter != bytes.end() ; iter++)\r
+       {\r
+       unsigned char ch = *iter;\r
+       str.push_back(hexDigits[(ch>>4) & 0x0f]);\r
+       str.push_back(hexDigits[(ch   ) & 0x0f]);\r
+       }\r
+    return str;\r
+}\r
+\r
+\r
+//########################################################################\r
+//##  D I G E S T\r
+//########################################################################\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+std::string Digest::finishHex()\r
+{\r
+    std::vector<unsigned char> hash = finish();\r
+    std::string str = toHex(hash);\r
+    return str;\r
+}\r
+\r
+\r
+//4.1.1 and 4.1.2\r
+#define SHA_ROTL(X,n)  ( ((X) << (n)) | ( ((X) & 0xffffffffL) >> (32-(n))) )\r
+#define        SHA_Ch(x,y,z)  ((z)^((x)&((y)^(z))))\r
+#define        SHA_Maj(x,y,z) (((x)&(y))^((z)&((x)^(y))))\r
+\r
+\r
+//########################################################################\r
+//##  S H A 1\r
+//########################################################################\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+void Sha1Digest::reset()\r
+{\r
+    lenW   = 0;\r
+    size   = 0;\r
+\r
+    // Initialize H with the magic constants (see FIPS180 for constants)\r
+    H[0] = 0x67452301L;\r
+    H[1] = 0xefcdab89L;\r
+    H[2] = 0x98badcfeL;\r
+    H[3] = 0x10325476L;\r
+    H[4] = 0xc3d2e1f0L;\r
+\r
+    for (int i = 0 ; i < 80 ; i++)\r
+        W[i] = 0;\r
+}\r
+\r
+\r
+\r
+\r
+void Sha1Digest::hashblock()\r
+{\r
+    //for (int t = 0; t < 16 ; t++)\r
+    //    printf("%2d %08lx\n", t, W[t]);\r
+\r
+    //see 6.1.2\r
+    for (int t = 16; t < 80 ; t++)\r
+        W[t] = SHA_ROTL((W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]), 1);\r
+\r
+    unsigned long a = H[0];\r
+    unsigned long b = H[1];\r
+    unsigned long c = H[2];\r
+    unsigned long d = H[3];\r
+    unsigned long e = H[4];\r
+\r
+    unsigned long T;\r
+\r
+    int t = 0;\r
+    for ( ; t < 20 ; t++)\r
+        {\r
+        //see 4.1.1 for the boolops on B,C, and D\r
+        T = (SHA_ROTL(a,5) + ((b&c)^(~b&d)) +  //Ch(b,c,d))\r
+                e + 0x5a827999L + W[t]) & 0xffffffffL;\r
+        e = d; d = c; c = SHA_ROTL(b, 30); b = a; a = T;\r
+        //printf("%2d %08lx %08lx %08lx %08lx %08lx\n", t, a, b, c, d, e);\r
+        }\r
+    for ( ; t < 40 ; t++)\r
+        {\r
+        T = (SHA_ROTL(a,5) + (b^c^d) +\r
+                e + 0x6ed9eba1L + W[t]) & 0xffffffffL;\r
+        e = d; d = c; c = SHA_ROTL(b, 30); b = a; a = T;\r
+        //printf("%2d %08lx %08lx %08lx %08lx %08lx\n", t, a, b, c, d, e);\r
+        }\r
+    for ( ; t < 60 ; t++)\r
+        {\r
+        T = (SHA_ROTL(a,5) + ((b&c)^(b&d)^(c&d)) +\r
+                e + 0x8f1bbcdcL + W[t]) & 0xffffffffL;\r
+        e = d; d = c; c = SHA_ROTL(b, 30); b = a; a = T;\r
+        //printf("%2d %08lx %08lx %08lx %08lx %08lx\n", t, a, b, c, d, e);\r
+        }\r
+    for ( ; t < 80 ; t++)\r
+        {\r
+        T = (SHA_ROTL(a,5) + (b^c^d) +\r
+                e + 0xca62c1d6L + W[t]) & 0xffffffffL;\r
+        e = d; d = c; c = SHA_ROTL(b, 30); b = a; a = T;\r
+        //printf("%2d %08lx %08lx %08lx %08lx %08lx\n", t, a, b, c, d, e);\r
+        }\r
+\r
+    H[0] += a;\r
+    H[1] += b;\r
+    H[2] += c;\r
+    H[3] += d;\r
+    H[4] += e;\r
+}\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+void Sha1Digest::update(unsigned char val)\r
+{\r
+    int wordNr = lenW >> 2;\r
+    W[wordNr] <<= 8;\r
+    W[wordNr] |= (unsigned long)val;\r
+    size += 8;\r
+    lenW++;\r
+    if (lenW >= 64)\r
+        {\r
+        hashblock();\r
+        lenW = 0;\r
+        }\r
+}\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+std::vector<unsigned char> Sha1Digest::finish()\r
+{\r
+    //save our size before padding\r
+    unsigned long long sizeOut = size;\r
+    \r
+    // Pad with a binary 1 (0x80)\r
+    update((unsigned char)0x80);\r
+    //append 0's to make a 56-byte buf.\r
+       //use mod, so that we will loop around once if already over 56\r
+    while (lenW != 56)\r
+        update((unsigned char)0x00);\r
+    //append 64-bit size\r
+    for (int shift = 56 ; shift>=0 ; shift-= 8)\r
+        {\r
+        unsigned char ch = (unsigned char)((sizeOut >> shift) & 0xff);\r
+        update(ch);\r
+        }\r
+\r
+    // Output hash\r
+    std::vector<unsigned char> ret;\r
+    for (int wordNr = 0 ; wordNr < 5 ; wordNr++)\r
+        {\r
+        ret.push_back((unsigned char)((H[wordNr] >> 24) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >> 16) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >>  8) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr]      ) & 0xff));\r
+        }\r
+\r
+    // Re-initialize the context (also zeroizes contents)\r
+    reset();\r
+\r
+    return ret;\r
+\r
+}\r
+\r
+\r
+//########################################################################\r
+//##  SHA224\r
+//########################################################################\r
+\r
+\r
+/**\r
+ * SHA-224 and SHA-512 share the same operations and constants\r
+ */ \r
+\r
+#define        SHA_Rot32(x,s) (((x) >> s) | ((x) << (32 - s)))\r
+#define        SHA_SIGMA0(x)  (SHA_Rot32(x,  2) ^ SHA_Rot32(x, 13) ^ SHA_Rot32(x, 22))\r
+#define        SHA_SIGMA1(x)  (SHA_Rot32(x,  6) ^ SHA_Rot32(x, 11) ^ SHA_Rot32(x, 25))\r
+#define        SHA_sigma0(x)  (SHA_Rot32(x,  7) ^ SHA_Rot32(x, 18) ^ ((x) >> 3))\r
+#define        SHA_sigma1(x)  (SHA_Rot32(x, 17) ^ SHA_Rot32(x, 19) ^ ((x) >> 10))\r
+\r
+\r
+static unsigned long sha256constants[64] =\r
+{\r
+0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,\r
+0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,\r
+0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,\r
+0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,\r
+0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,\r
+0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,\r
+0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,\r
+0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,\r
+0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,\r
+0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,\r
+0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,\r
+0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,\r
+0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,\r
+0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,\r
+0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,\r
+0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL\r
+};\r
+\r
+\r
+\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+void Sha224Digest::reset()\r
+{\r
+    lenW   = 0;\r
+    size   = 0;\r
+\r
+    // Initialize H with the magic constants (see FIPS180 for constants)\r
+    H[0] = 0xc1059ed8L;\r
+    H[1] = 0x367cd507L;\r
+    H[2] = 0x3070dd17L;\r
+    H[3] = 0xf70e5939L;\r
+    H[4] = 0xffc00b31L;\r
+    H[5] = 0x68581511L;\r
+    H[6] = 0x64f98fa7L;\r
+    H[7] = 0xbefa4fa4L;\r
+\r
+    for (int i = 0 ; i < 64 ; i++)\r
+        W[i] = 0;\r
+}\r
+\r
+\r
+\r
+\r
+\r
+void Sha224Digest::hashblock()\r
+{\r
+    //for (int t = 0; t < 16 ; t++)\r
+    //    printf("%2d %08lx\n", t, W[t]);\r
+\r
+    //see 6.2.2\r
+    for (int t = 16; t < 64 ; t++)\r
+        W[t] = SHA_sigma1(W[t-2]) + W[t-7] + SHA_sigma0(W[t-15]) + W[t-16];\r
+\r
+    unsigned long a = H[0];\r
+    unsigned long b = H[1];\r
+    unsigned long c = H[2];\r
+    unsigned long d = H[3];\r
+    unsigned long e = H[4];\r
+    unsigned long f = H[5];\r
+    unsigned long g = H[6];\r
+    unsigned long h = H[7];\r
+\r
+    for (int t = 0 ; t < 64 ; t++)\r
+        {\r
+        //see 4.1.1 for the boolops\r
+        unsigned long T1 = h + SHA_SIGMA1(e) + SHA_Ch(e,f,g) +\r
+            sha256constants[t] + W[t];\r
+        unsigned long T2 = SHA_SIGMA0(a) + SHA_Maj(a,b,c);\r
+        h = g; g = f; f = e; e = d  + T1 ; d = c; c = b; b = a; a = T1 + T2;\r
+        //printf("%2d %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",\r
+               //         t, a, b, c, d, e, f, g, h);\r
+        }\r
+\r
+    H[0] += a;\r
+    H[1] += b;\r
+    H[2] += c;\r
+    H[3] += d;\r
+    H[4] += e;\r
+    H[5] += f;\r
+    H[6] += g;\r
+    H[7] += h;\r
+}\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+void Sha224Digest::update(unsigned char val)\r
+{\r
+    int wordNr = lenW >> 2;\r
+    W[wordNr] <<= 8;\r
+    W[wordNr] |= (unsigned long)val;\r
+    size += 8;\r
+    lenW++;\r
+    if (lenW >= 64)\r
+        {\r
+        hashblock();\r
+        lenW = 0;\r
+        }\r
+}\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+std::vector<unsigned char> Sha224Digest::finish()\r
+{\r
+    //save our size before padding\r
+    unsigned long long sizeOut = size;\r
+    \r
+    // Pad with a binary 1 (0x80)\r
+    update((unsigned char)0x80);\r
+    //append 0's to make a 56-byte buf.\r
+       //use mod, so that we will loop around once if already over 56\r
+    while (lenW != 56)\r
+        update((unsigned char)0x00);\r
+    //append 64-bit size\r
+    for (int shift = 56 ; shift>=0 ; shift-= 8)\r
+        {\r
+        unsigned char ch = (unsigned char)((sizeOut >> shift) & 0xff);\r
+        update(ch);\r
+        }\r
+\r
+    // Output hash\r
+    std::vector<unsigned char> ret;\r
+    for (int wordNr = 0 ; wordNr < 7 ; wordNr++)\r
+        {\r
+        ret.push_back((unsigned char)((H[wordNr] >> 24) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >> 16) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >>  8) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr]      ) & 0xff));\r
+        }\r
+\r
+    // Re-initialize the context (also zeroizes contents)\r
+    reset();\r
+\r
+    return ret;\r
+\r
+}\r
+\r
+//########################################################################\r
+//##  SHA256\r
+//########################################################################\r
+\r
+\r
+\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+void Sha256Digest::reset()\r
+{\r
+    lenW   = 0;\r
+    size   = 0;\r
+\r
+    // Initialize H with the magic constants (see FIPS180 for constants)\r
+    H[0] = 0x6a09e667L;\r
+    H[1] = 0xbb67ae85L;\r
+    H[2] = 0x3c6ef372L;\r
+    H[3] = 0xa54ff53aL;\r
+    H[4] = 0x510e527fL;\r
+    H[5] = 0x9b05688cL;\r
+    H[6] = 0x1f83d9abL;\r
+    H[7] = 0x5be0cd19L;\r
+\r
+    for (int i = 0 ; i < 64 ; i++)\r
+        W[i] = 0;\r
+}\r
+\r
+\r
+\r
+\r
+\r
+void Sha256Digest::hashblock()\r
+{\r
+    //for (int t = 0; t < 16 ; t++)\r
+    //    printf("%2d %08lx\n", t, W[t]);\r
+\r
+    //see 6.2.2\r
+    for (int t = 16; t < 64 ; t++)\r
+        W[t] = SHA_sigma1(W[t-2]) + W[t-7] + SHA_sigma0(W[t-15]) + W[t-16];\r
+\r
+    unsigned long a = H[0];\r
+    unsigned long b = H[1];\r
+    unsigned long c = H[2];\r
+    unsigned long d = H[3];\r
+    unsigned long e = H[4];\r
+    unsigned long f = H[5];\r
+    unsigned long g = H[6];\r
+    unsigned long h = H[7];\r
+\r
+    for (int t = 0 ; t < 64 ; t++)\r
+        {\r
+        //see 4.1.1 for the boolops\r
+        unsigned long T1 = h + SHA_SIGMA1(e) + SHA_Ch(e,f,g) +\r
+            sha256constants[t] + W[t];\r
+        unsigned long T2 = SHA_SIGMA0(a) + SHA_Maj(a,b,c);\r
+        h = g; g = f; f = e; e = d  + T1 ; d = c; c = b; b = a; a = T1 + T2;\r
+        //printf("%2d %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",\r
+               //         t, a, b, c, d, e, f, g, h);\r
+        }\r
+\r
+    H[0] += a;\r
+    H[1] += b;\r
+    H[2] += c;\r
+    H[3] += d;\r
+    H[4] += e;\r
+    H[5] += f;\r
+    H[6] += g;\r
+    H[7] += h;\r
+}\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+void Sha256Digest::update(unsigned char val)\r
+{\r
+    int wordNr = lenW >> 2;\r
+    W[wordNr] <<= 8;\r
+    W[wordNr] |= (unsigned long)val;\r
+    size += 8;\r
+    lenW++;\r
+    if (lenW >= 64)\r
+        {\r
+        hashblock();\r
+        lenW = 0;\r
+        }\r
+}\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+std::vector<unsigned char> Sha256Digest::finish()\r
+{\r
+    //save our size before padding\r
+    unsigned long long sizeOut = size;\r
+    \r
+    // Pad with a binary 1 (0x80)\r
+    update((unsigned char)0x80);\r
+    //append 0's to make a 56-byte buf.\r
+       //use mod, so that we will loop around once if already over 56\r
+    while (lenW != 56)\r
+        update((unsigned char)0x00);\r
+    //append 64-bit size\r
+    for (int shift = 56 ; shift>=0 ; shift-= 8)\r
+        {\r
+        unsigned char ch = (unsigned char)((sizeOut >> shift) & 0xff);\r
+        update(ch);\r
+        }\r
+\r
+    // Output hash\r
+    std::vector<unsigned char> ret;\r
+    for (int wordNr = 0 ; wordNr < 8 ; wordNr++)\r
+        {\r
+        ret.push_back((unsigned char)((H[wordNr] >> 24) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >> 16) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >>  8) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr]      ) & 0xff));\r
+        }\r
+\r
+    // Re-initialize the context (also zeroizes contents)\r
+    reset();\r
+\r
+    return ret;\r
+\r
+}\r
+\r
+\r
+\r
+//########################################################################\r
+//##  SHA384\r
+//########################################################################\r
+\r
+/**\r
+ * SHA-384 and SHA-512 share the same operations and constants\r
+ */\r
+  \r
+#undef SHA_SIGMA0\r
+#undef SHA_SIGMA1\r
+#undef SHA_sigma0\r
+#undef SHA_sigma1\r
+\r
+#define        SHA_Rot64(x,s) (((x) >> s) | ((x) << (64 - s)))\r
+#define        SHA_SIGMA0(x)  (SHA_Rot64(x, 28) ^ SHA_Rot64(x, 34) ^ SHA_Rot64(x, 39))\r
+#define        SHA_SIGMA1(x)  (SHA_Rot64(x, 14) ^ SHA_Rot64(x, 18) ^ SHA_Rot64(x, 41))\r
+#define        SHA_sigma0(x)  (SHA_Rot64(x,  1) ^ SHA_Rot64(x,  8) ^ ((x) >> 7))\r
+#define        SHA_sigma1(x)  (SHA_Rot64(x, 19) ^ SHA_Rot64(x, 61) ^ ((x) >> 6))\r
+\r
+\r
+static unsigned long long sha512constants[80] =\r
+{\r
+0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,\r
+0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,\r
+0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,\r
+0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,\r
+0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,\r
+0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,\r
+0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,\r
+0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,\r
+0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,\r
+0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,\r
+0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,\r
+0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,\r
+0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,\r
+0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,\r
+0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,\r
+0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,\r
+0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,\r
+0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,\r
+0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,\r
+0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,\r
+0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,\r
+0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,\r
+0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,\r
+0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,\r
+0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,\r
+0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,\r
+0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,\r
+0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,\r
+0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,\r
+0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,\r
+0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,\r
+0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,\r
+0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,\r
+0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,\r
+0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,\r
+0x113f9804bef90daeULL, 0x1b710b35131c471bULL,\r
+0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,\r
+0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,\r
+0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,\r
+0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL\r
+};\r
+\r
+\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+void Sha384Digest::reset()\r
+{\r
+    lenW   = 0;\r
+    size   = 0;\r
+\r
+    // SHA-384 differs from SHA-512 by these constants\r
+    H[0] = 0xcbbb9d5dc1059ed8ULL;\r
+    H[1] = 0x629a292a367cd507ULL;\r
+    H[2] = 0x9159015a3070dd17ULL;\r
+    H[3] = 0x152fecd8f70e5939ULL;\r
+    H[4] = 0x67332667ffc00b31ULL;\r
+    H[5] = 0x8eb44a8768581511ULL;\r
+    H[6] = 0xdb0c2e0d64f98fa7ULL;\r
+    H[7] = 0x47b5481dbefa4fa4ULL;\r
+\r
+    for (int i = 0 ; i < 80 ; i++)\r
+        W[i] = 0;\r
+}\r
+\r
+\r
+\r
+\r
+\r
+void Sha384Digest::hashblock()\r
+{\r
+    /*\r
+       for (int t = 0; t < 16 ; t++)\r
+        {\r
+        printf("%2d ", t);\r
+        pl(W[t]);\r
+        printf("\n");\r
+        }\r
+    */\r
+\r
+    //see 6.2.2\r
+    for (int t = 16; t < 80 ; t++)\r
+        W[t] = SHA_sigma1(W[t-2]) + W[t-7] + SHA_sigma0(W[t-15]) + W[t-16];\r
+\r
+    unsigned long long a = H[0];\r
+    unsigned long long b = H[1];\r
+    unsigned long long c = H[2];\r
+    unsigned long long d = H[3];\r
+    unsigned long long e = H[4];\r
+    unsigned long long f = H[5];\r
+    unsigned long long g = H[6];\r
+    unsigned long long h = H[7];\r
+\r
+    for (int t = 0 ; t < 80 ; t++)\r
+        {\r
+        //see 4.1.1 for the boolops\r
+        unsigned long long T1 = h + SHA_SIGMA1(e) + SHA_Ch(e,f,g) +\r
+            sha512constants[t] + W[t];\r
+        unsigned long long T2 = SHA_SIGMA0(a) + SHA_Maj(a,b,c);\r
+        h = g; g = f; f = e; e = d  + T1 ; d = c; c = b; b = a; a = T1 + T2;\r
+        }\r
+\r
+    H[0] += a;\r
+    H[1] += b;\r
+    H[2] += c;\r
+    H[3] += d;\r
+    H[4] += e;\r
+    H[5] += f;\r
+    H[6] += g;\r
+    H[7] += h;\r
+}\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+void Sha384Digest::update(unsigned char val)\r
+{\r
+    int wordNr = lenW >> 3;\r
+    W[wordNr] <<= 8;\r
+    W[wordNr] |= (unsigned long)val;\r
+    size += 8;\r
+    lenW++;\r
+    if (lenW >= 128)\r
+        {\r
+        hashblock();\r
+        lenW = 0;\r
+        }\r
+}\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+std::vector<unsigned char> Sha384Digest::finish()\r
+{\r
+    //save our size before padding\r
+    unsigned long long sizeOut = size;\r
+    \r
+    // Pad with a binary 1 (0x80)\r
+    update((unsigned char)0x80);\r
+    //append 0's to make a 112-byte buf.\r
+       //we will loop around once if already over 112\r
+    while (lenW != 112)\r
+        update((unsigned char)0x00);\r
+        \r
+    //append 128-bit size\r
+    for (int i = 0 ; i < 8 ; i++) //64 upper bits\r
+        update((unsigned char)0x00);\r
+    for (int shift = 56 ; shift>=0 ; shift-= 8) //64 lower length bits\r
+        {\r
+        unsigned char ch = (unsigned char)((sizeOut >> shift) & 0xff);\r
+        update(ch);\r
+        }\r
+\r
+    // Output hash\r
+    //for SHA-384, we use the left-most 6 64-bit words\r
+    std::vector<unsigned char> ret;\r
+    for (int wordNr = 0 ; wordNr < 6 ; wordNr++)\r
+        {\r
+        ret.push_back((unsigned char)((H[wordNr] >> 56) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >> 48) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >> 40) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >> 32) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >> 24) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >> 16) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >>  8) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr]      ) & 0xff));\r
+        }\r
+\r
+    // Re-initialize the context (also zeroizes contents)\r
+    reset();\r
+\r
+    return ret;\r
+\r
+}\r
+\r
+\r
+//########################################################################\r
+//##  SHA512\r
+//########################################################################\r
+\r
+\r
+\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+void Sha512Digest::reset()\r
+{\r
+    lenW   = 0;\r
+    size   = 0;\r
+\r
+    // Initialize H with the magic constants (see FIPS180 for constants)\r
+    H[0] = 0x6a09e667f3bcc908ULL;\r
+    H[1] = 0xbb67ae8584caa73bULL;\r
+    H[2] = 0x3c6ef372fe94f82bULL;\r
+    H[3] = 0xa54ff53a5f1d36f1ULL;\r
+    H[4] = 0x510e527fade682d1ULL;\r
+    H[5] = 0x9b05688c2b3e6c1fULL;\r
+    H[6] = 0x1f83d9abfb41bd6bULL;\r
+    H[7] = 0x5be0cd19137e2179ULL;\r
+\r
+    for (int i = 0 ; i < 80 ; i++)\r
+        W[i] = 0;\r
+}\r
+\r
+\r
+\r
+\r
+\r
+void Sha512Digest::hashblock()\r
+{\r
+    /*\r
+       for (int t = 0; t < 16 ; t++)\r
+        {\r
+        printf("%2d ", t);\r
+        pl(W[t]);\r
+        printf("\n");\r
+        }\r
+    */\r
+\r
+    //see 6.2.2\r
+    for (int t = 16; t < 80 ; t++)\r
+        W[t] = SHA_sigma1(W[t-2]) + W[t-7] + SHA_sigma0(W[t-15]) + W[t-16];\r
+\r
+    unsigned long long a = H[0];\r
+    unsigned long long b = H[1];\r
+    unsigned long long c = H[2];\r
+    unsigned long long d = H[3];\r
+    unsigned long long e = H[4];\r
+    unsigned long long f = H[5];\r
+    unsigned long long g = H[6];\r
+    unsigned long long h = H[7];\r
+\r
+    for (int t = 0 ; t < 80 ; t++)\r
+        {\r
+        //see 4.1.1 for the boolops\r
+        unsigned long long T1 = h + SHA_SIGMA1(e) + SHA_Ch(e,f,g) +\r
+            sha512constants[t] + W[t];\r
+        unsigned long long T2 = SHA_SIGMA0(a) + SHA_Maj(a,b,c);\r
+        h = g; g = f; f = e; e = d  + T1 ; d = c; c = b; b = a; a = T1 + T2;\r
+        }\r
+\r
+    H[0] += a;\r
+    H[1] += b;\r
+    H[2] += c;\r
+    H[3] += d;\r
+    H[4] += e;\r
+    H[5] += f;\r
+    H[6] += g;\r
+    H[7] += h;\r
+}\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+void Sha512Digest::update(unsigned char val)\r
+{\r
+    int wordNr = lenW >> 3;\r
+    W[wordNr] <<= 8;\r
+    W[wordNr] |= (unsigned long)val;\r
+    size += 8;\r
+    lenW++;\r
+    if (lenW >= 128)\r
+        {\r
+        hashblock();\r
+        lenW = 0;\r
+        }\r
+}\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+std::vector<unsigned char> Sha512Digest::finish()\r
+{\r
+    //save our size before padding\r
+    unsigned long long sizeOut = size;\r
+    \r
+    // Pad with a binary 1 (0x80)\r
+    update((unsigned char)0x80);\r
+    //append 0's to make a 112-byte buf.\r
+       //we will loop around once if already over 112\r
+    while (lenW != 112)\r
+        update((unsigned char)0x00);\r
+        \r
+    //append 128-bit size\r
+    for (int i = 0 ; i < 8 ; i++) //64 upper bits\r
+        update((unsigned char)0x00);\r
+    for (int shift = 56 ; shift>=0 ; shift-= 8) //64 lower length bits\r
+        {\r
+        unsigned char ch = (unsigned char)((sizeOut >> shift) & 0xff);\r
+        update(ch);\r
+        }\r
+\r
+    // Output hash\r
+    std::vector<unsigned char> ret;\r
+    for (int wordNr = 0 ; wordNr < 8 ; wordNr++)\r
+        {\r
+        ret.push_back((unsigned char)((H[wordNr] >> 56) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >> 48) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >> 40) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >> 32) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >> 24) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >> 16) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr] >>  8) & 0xff));\r
+        ret.push_back((unsigned char)((H[wordNr]      ) & 0xff));\r
+        }\r
+\r
+    // Re-initialize the context (also zeroizes contents)\r
+    reset();\r
+\r
+    return ret;\r
+\r
+}\r
+\r
+\r
+\r
+//########################################################################\r
+//##  M D 5\r
+//########################################################################\r
+\r
+static int md5r[64] =\r
+{\r
+   7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22, \r
+   5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,\r
+   4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,\r
+   6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21\r
+};\r
+\r
+static unsigned long md5k[64] =\r
+{\r
+0xd76aa478L, 0xe8c7b756L, 0x242070dbL, 0xc1bdceeeL, \r
+0xf57c0fafL, 0x4787c62aL, 0xa8304613L, 0xfd469501L, \r
+0x698098d8L, 0x8b44f7afL, 0xffff5bb1L, 0x895cd7beL, \r
+0x6b901122L, 0xfd987193L, 0xa679438eL, 0x49b40821L, \r
+0xf61e2562L, 0xc040b340L, 0x265e5a51L, 0xe9b6c7aaL, \r
+0xd62f105dL, 0x02441453L, 0xd8a1e681L, 0xe7d3fbc8L, \r
+0x21e1cde6L, 0xc33707d6L, 0xf4d50d87L, 0x455a14edL, \r
+0xa9e3e905L, 0xfcefa3f8L, 0x676f02d9L, 0x8d2a4c8aL, \r
+0xfffa3942L, 0x8771f681L, 0x6d9d6122L, 0xfde5380cL, \r
+0xa4beea44L, 0x4bdecfa9L, 0xf6bb4b60L, 0xbebfbc70L, \r
+0x289b7ec6L, 0xeaa127faL, 0xd4ef3085L, 0x04881d05L, \r
+0xd9d4d039L, 0xe6db99e5L, 0x1fa27cf8L, 0xc4ac5665L, \r
+0xf4292244L, 0x432aff97L, 0xab9423a7L, 0xfc93a039L, \r
+0x655b59c3L, 0x8f0ccc92L, 0xffeff47dL, 0x85845dd1L, \r
+0x6fa87e4fL, 0xfe2ce6e0L, 0xa3014314L, 0x4e0811a1L, \r
+0xf7537e82L, 0xbd3af235L, 0x2ad7d2bbL, 0xeb86d391L\r
+};\r
+\r
+#define MD5_ROTL(X,n) (((X) << (n)) | ((X) >> (32-(n))))\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+void Md5Digest::reset()\r
+{\r
+    size  = 0;\r
+    lenW  = 0;\r
+\r
+    hash[0]  = 0x67452301L;\r
+    hash[1]  = 0xefcdab89L;\r
+    hash[2]  = 0x98badcfeL;\r
+    hash[3]  = 0x10325476L;\r
+\r
+    for (int i = 0 ; i < 64 ; i++)\r
+        W[i] = 0;\r
+\r
+}\r
+\r
+\r
+\r
+\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+void Md5Digest::hashblock()\r
+{\r
+    //for (int t = 0; t < 16 ; t++)\r
+    //    printf("%2d %08lx\n", t, W[t]);\r
+\r
+    unsigned long a = hash[0];\r
+    unsigned long b = hash[1];\r
+    unsigned long c = hash[2];\r
+    unsigned long d = hash[3];\r
+\r
+    int t = 0;\r
+    for ( ; t < 16 ; t++)\r
+        {\r
+        unsigned long f = d ^ ( b & ( c ^ d));\r
+        unsigned int g = t;\r
+        unsigned long temp = d; d = c; c = b;\r
+        b += MD5_ROTL(((a + f + md5k[t] + W[g])), md5r[t]);\r
+        a = temp;\r
+        //printf("%2d %08lx %08lx %08lx %08lx\n", t, a, b, c, d);\r
+        }\r
+    for ( ; t < 32 ; t++)\r
+        {\r
+        unsigned long f = c ^ ( d & ( b ^ c));\r
+        unsigned int g = (5 * t + 1) & 0xf;\r
+        unsigned long temp = d; d = c; c = b;\r
+        b += MD5_ROTL(((a + f + md5k[t] + W[g])), md5r[t]);\r
+        a = temp;\r
+        }\r
+    for ( ; t < 48 ; t++)\r
+        {\r
+        unsigned long f = b ^ c ^ d;\r
+        unsigned int g = (3 * t + 5) & 0xf;\r
+        unsigned long temp = d; d = c; c = b;\r
+        b += MD5_ROTL(((a + f + md5k[t] + W[g])), md5r[t]);\r
+        a = temp;\r
+        }\r
+    for ( ; t < 64 ; t++)\r
+        {\r
+        unsigned long f = c ^ (b | ~d);\r
+        unsigned int g = (7 * t) & 0xf;\r
+        unsigned long temp = d; d = c; c = b;\r
+        b += MD5_ROTL(((a + f + md5k[t] + W[g])), md5r[t]);\r
+        a = temp;\r
+        }\r
+\r
+    hash[0] += a;\r
+    hash[1] += b;\r
+    hash[2] += c;\r
+    hash[3] += d;\r
+}\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+void Md5Digest::update(unsigned char val)\r
+{\r
+    int wordNr = lenW >> 2;\r
+    /*\r
+       W[wordNr] <<= 8;\r
+    W[wordNr] |= (unsigned long)val;\r
+    */\r
+    W[wordNr] = ( (W[wordNr] >> 8) & 0x00ffffff ) |\r
+                ( ((unsigned long)val) << 24    );\r
+    size += 8;\r
+    lenW++;\r
+    if (lenW >= 64)\r
+        {\r
+        hashblock();\r
+        lenW = 0;\r
+        }\r
+}\r
+\r
+\r
+\r
+\r
+/**\r
+ *\r
+ */\r
+std::vector<unsigned char> Md5Digest::finish()\r
+{\r
+    //save our size before padding\r
+    unsigned long long sizeOut = size;\r
+    \r
+    // Pad with a binary 1 (0x80)\r
+    update((unsigned char)0x80);\r
+    //append 0's to make a 56-byte buf.\r
+       //use mod, so that we will loop around once if already over 56\r
+    while (lenW != 56)\r
+        update((unsigned char)0x00);\r
+\r
+\r
+    //Append the length.  Lower 32 bits first\r
+    update( (unsigned char) ((sizeOut    ) & 0xff));\r
+    update( (unsigned char) ((sizeOut>> 8) & 0xff));\r
+    update( (unsigned char) ((sizeOut>>16) & 0xff));\r
+    update( (unsigned char) ((sizeOut>>24) & 0xff));\r
+    update( (unsigned char) ((sizeOut>>32) & 0xff));\r
+    update( (unsigned char) ((sizeOut>>40) & 0xff));\r
+    update( (unsigned char) ((sizeOut>>48) & 0xff));\r
+    update( (unsigned char) ((sizeOut>>56) & 0xff));\r
+\r
+    //Output hash\r
+    std::vector<unsigned char> ret;\r
+    for (int wordNr = 0 ; wordNr<4 ; wordNr++)\r
+        {\r
+        unsigned long w = hash[wordNr];\r
+        ret.push_back( (unsigned char) ((w      ) & 0xff) );\r
+        ret.push_back( (unsigned char) ((w >>  8) & 0xff) );\r
+        ret.push_back( (unsigned char) ((w >> 16) & 0xff) );\r
+        ret.push_back( (unsigned char) ((w >> 24) & 0xff) );\r
+        }\r
+\r
+    // Re-initialize the context (also zeroizes contents)\r
+    reset();\r
+\r
+    return ret;\r
+}\r
+\r
+\r
+\r
+\r
+\r
+\r
+//########################################################################\r
+//## T E S T S\r
+//########################################################################\r
+\r
+/**\r
+ * Compile this file alone with -DDIGEST_TEST to run the\r
+ * tests below:\r
+ * > gcc -DDIGEST_TEST digest.cpp -o digest\r
+ * > digest   \r
+ */\r
\r
+ #ifdef DIGEST_TEST\r
+\r
+\r
+typedef struct\r
+{\r
+    char *msg;\r
+    char *val;\r
+} TestPair;\r
+\r
+static TestPair md5tests[] =\r
+{\r
+  {\r
+  "",\r
+  "d41d8cd98f00b204e9800998ecf8427e"\r
+  },\r
+  {\r
+  "a",\r
+  "0cc175b9c0f1b6a831c399e269772661"\r
+  },\r
+  {\r
+  "abc",\r
+  "900150983cd24fb0d6963f7d28e17f72"\r
+  },\r
+  {\r
+  "message digest",\r
+  "f96b697d7cb7938d525a2f31aaf161d0"\r
+  },\r
+  {\r
+  "abcdefghijklmnopqrstuvwxyz",\r
+  "c3fcd3d76192e4007dfb496cca67e13b"\r
+  },\r
+  {\r
+  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",\r
+  "d174ab98d277d9f5a5611c2c9f419d9f"\r
+  },\r
+  {\r
+  "12345678901234567890123456789012345678901234567890123456789012345678901234567890",\r
+  "57edf4a22be3c955ac49da2e2107b67a"\r
+  },\r
+  {\r
+  NULL,\r
+  NULL\r
+  }\r
+};\r
+\r
+\r
+\r
+static TestPair sha1tests[] =\r
+{\r
+  {\r
+  "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",\r
+  "84983e441c3bd26ebaae4aa1f95129e5e54670f1"\r
+  },\r
+  {\r
+  NULL,\r
+  NULL\r
+  }\r
+};\r
+\r
+static TestPair sha224tests[] =\r
+{\r
+  {\r
+  "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",\r
+  "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525"\r
+  },\r
+  {\r
+  NULL,\r
+  NULL\r
+  }\r
+};\r
+\r
+static TestPair sha256tests[] =\r
+{\r
+  {\r
+  "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",\r
+  "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"\r
+  },\r
+  {\r
+  NULL,\r
+  NULL\r
+  }\r
+};\r
+\r
+static TestPair sha384tests[] =\r
+{\r
+  {\r
+  "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"\r
+       "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",\r
+  "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"\r
+       "fcc7c71a557e2db966c3e9fa91746039"\r
+  },\r
+  {\r
+  NULL,\r
+  NULL\r
+  }\r
+};\r
+\r
+static TestPair sha512tests[] =\r
+{\r
+  {\r
+  "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"\r
+       "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",\r
+  "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"\r
+       "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"\r
+  },\r
+  {\r
+  NULL,\r
+  NULL\r
+  }\r
+};\r
+\r
+\r
+bool hashTests(Digest &digest, TestPair *tp)\r
+{\r
+    for (TestPair *pair = tp ; pair->msg ; pair++)\r
+        {\r
+        digest.reset();\r
+        std::string msg = pair->msg;\r
+        std::string val = pair->val;\r
+        digest.append(msg);\r
+        std::string res = digest.finishHex();\r
+        printf("### Msg '%s':\n   hash '%s'\n   exp  '%s'\n",\r
+            msg.c_str(), res.c_str(), val.c_str());\r
+        if (res != val)\r
+            {\r
+            printf("ERROR: Hash mismatch\n");\r
+            return false;\r
+            }\r
+        }\r
+    return true;\r
+}\r
+\r
+\r
+bool millionATest(Digest &digest, const std::string &exp)\r
+{\r
+    digest.reset();\r
+       for (int i=0 ; i<1000000 ; i++)\r
+        digest.append('a');\r
+    std::string res = digest.finishHex();\r
+    printf("\nHash of 1,000,000 'a'\n   calc %s\n   exp  %s\n",\r
+            res.c_str(), exp.c_str());\r
+    if (res != exp)\r
+        {\r
+        printf("ERROR: Mismatch.\n");\r
+        return false;\r
+        }\r
+    return true;\r
+}\r
+\r
+static bool doTests()\r
+{\r
+    printf("##########################################\n");\r
+    printf("## MD5\n");\r
+    printf("##########################################\n");\r
+    Md5Digest md5;\r
+    if (!hashTests(md5, md5tests))\r
+        return false;\r
+    if (!millionATest(md5, "7707d6ae4e027c70eea2a935c2296f21"))\r
+        return false;\r
+    printf("\n\n\n");\r
+    printf("##########################################\n");\r
+    printf("## SHA1\n");\r
+    printf("##########################################\n");\r
+    Sha1Digest sha1;\r
+    if (!hashTests(sha1, sha1tests))\r
+        return false;\r
+    if (!millionATest(sha1, "34aa973cd4c4daa4f61eeb2bdbad27316534016f"))\r
+        return false;\r
+    printf("\n\n\n");\r
+    printf("##########################################\n");\r
+    printf("## SHA224\n");\r
+    printf("##########################################\n");\r
+    Sha224Digest sha224;\r
+    if (!hashTests(sha224, sha224tests))\r
+        return false;\r
+    if (!millionATest(sha224,\r
+            "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67"))\r
+        return false;\r
+    printf("\n\n\n");\r
+    printf("##########################################\n");\r
+    printf("## SHA256\n");\r
+    printf("##########################################\n");\r
+    Sha256Digest sha256;\r
+    if (!hashTests(sha256, sha256tests))\r
+        return false;\r
+    if (!millionATest(sha256,\r
+            "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"))\r
+        return false;\r
+    printf("\n\n\n");\r
+    printf("##########################################\n");\r
+    printf("## SHA384\n");\r
+    printf("##########################################\n");\r
+    Sha384Digest sha384;\r
+    if (!hashTests(sha384, sha384tests))\r
+        return false;\r
+    /**/\r
+       if (!millionATest(sha384,\r
+                "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"\r
+                "07b8b3dc38ecc4ebae97ddd87f3d8985"))\r
+        return false;\r
+    /**/\r
+    printf("\n\n\n");\r
+    printf("##########################################\n");\r
+    printf("## SHA512\n");\r
+    printf("##########################################\n");\r
+    Sha512Digest sha512;\r
+    if (!hashTests(sha512, sha512tests))\r
+        return false;\r
+    if (!millionATest(sha512,\r
+                "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"\r
+             "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"))\r
+        return false;\r
+    return true;\r
+}\r
+\r
+\r
+int main(int argc, char **argv)\r
+{\r
+    doTests();\r
+    printf("####### done ########\n");\r
+    return 0;\r
+}\r
+\r
+\r
+#endif /* DIGEST_TEST */\r
+\r
+//########################################################################\r
+//## E N D    O F    F I L E\r
+//########################################################################\r
diff --git a/src/dom/util/digest.h b/src/dom/util/digest.h
new file mode 100644 (file)
index 0000000..3667a25
--- /dev/null
@@ -0,0 +1,493 @@
+#ifndef __DIGEST_H__\r
+#define __DIGEST_H__\r
+/**\r
+ * Secure Hashing Tool\r
+ * *\r
+ * Authors:\r
+ *   Bob Jamison\r
+ *\r
+ * Copyright (C) 2006 Bob Jamison\r
+ *\r
+ *  This library is free software; you can redistribute it and/or\r
+ *  modify it under the terms of the GNU Lesser General Public\r
+ *  License as published by the Free Software Foundation; either\r
+ *  version 2.1 of the License, or (at your option) any later version.\r
+ *\r
+ *  This library is distributed in the hope that it will be useful,\r
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ *  Lesser General Public License for more details.\r
+ *\r
+ *  You should have received a copy of the GNU Lesser General Public\r
+ *  License along with this library; if not, write to the Free Software\r
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
+ */\r
+\r
+/**\r
+ *\r
+ *  This base class and its subclasses  provide an easy API for providing\r
+ *  several different types of secure hashing functions for whatever use\r
+ *  a developer might need.  This is not intended as a high-performance\r
+ *  replacement for the fine implementations already available.  Rather, it\r
+ *  is a small and simple (and maybe a bit slow?) tool for moderate common\r
+ *  hashing requirements, like for communications and authentication.\r
+ *  \r
+ *  These hashes are intended to be simple to use.  For example:\r
+ *  Sha256Digest digest;\r
+ *  digest.append("The quick brown dog");\r
+ *  std::string result = digest.finishHex();\r
+ *  \r
+ *  There are several forms of append() for convenience.\r
+ *  finish() and finishHex() call reset() for both security and\r
+ *  to prepare for the next use.\r
+ *          \r
+ */\r
+\r
+#include <vector>\r
+#include <string>\r
+\r
+\r
+/**\r
+ *  Base class.  Do not use this class directly.  Rather, use of of the\r
+ *  subclasses below.   \r
+ *  For all subclasses, overload reset(), update(unsigned char), and finish()\r
+ */\r
+class Digest\r
+{\r
+public:\r
+\r
+    /**\r
+     *  Different types of hash algorithms.\r
+     */\r
+    typedef enum\r
+        {\r
+        HASH_NONE,\r
+        HASH_SHA1,\r
+        HASH_SHA224,\r
+        HASH_SHA256,\r
+        HASH_SHA384,\r
+        HASH_SHA512,\r
+        HASH_MD5\r
+        } HashType;\r
+        \r
+    /**\r
+     *  Constructor, with no type\r
+     */\r
+    Digest() : hashType(HASH_NONE)\r
+        { reset(); }\r
+\r
+    /**\r
+     *  Destructor\r
+     */\r
+    virtual ~Digest()\r
+        { reset(); }\r
+\r
+    /**\r
+     *  Return one of the enumerated hash types above\r
+     */\r
+    virtual int getType()\r
+        { return hashType; }\r
+\r
+    /**\r
+     *  Append a single byte to the hash\r
+     */\r
+    void append(unsigned char ch)\r
+        { update(ch); }\r
+\r
+    /**\r
+     *  Append a string to the hash\r
+     */\r
+    virtual void append(const std::string &str)\r
+        {\r
+        for (unsigned int i=0 ; i<str.size() ; i++)\r
+            update((unsigned char)str[i]);\r
+        }\r
+\r
+    /**\r
+     *  Append a byte buffer to the hash\r
+     */\r
+    virtual void append(unsigned char *buf, int len)\r
+        {\r
+        for (int i=0 ; i<len ; i++)\r
+            update(buf[i]);\r
+        }\r
+\r
+    /**\r
+     *  Append a byte vector to the hash\r
+     */\r
+    virtual void append(const std::vector<unsigned char> buf)\r
+        {\r
+        for (unsigned int i=0 ; i<buf.size() ; i++)\r
+            update(buf[i]);\r
+        }\r
+\r
+    /**\r
+     *  Finish the hash and return a hexidecimal version of the computed\r
+     *  value     \r
+     */\r
+    virtual std::string finishHex();\r
+\r
+    /**\r
+     *  Initialize the fields of this hash engine to its starting values.    \r
+     *  Overload this in every subclass\r
+     */\r
+    virtual void reset()\r
+        {}\r
+\r
+    /**\r
+     *  Finish the hash and return its computed value    \r
+     *  Overload this in every subclass\r
+     */\r
+    virtual std::vector<unsigned char> finish()\r
+        {\r
+        std::vector<unsigned char> ret;\r
+        return ret;\r
+        }\r
+\r
+protected:\r
+\r
+    /**\r
+     *  Update the hash with a given byte    \r
+     *  Overload this in every subclass\r
+     */\r
+    virtual void update(unsigned char ch)\r
+        {}\r
+\r
+    /**\r
+     * The enumerated type of the hash\r
+     */             \r
+    int hashType;\r
+};\r
+\r
+\r
+\r
+\r
+\r
+/**\r
+ *  SHA-1,  \r
+ *  Section 6.1, SECURE HASH STANDARD\r
+ *  Federal Information Processing Standards Publication 180-2\r
+ *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf\r
+ */\r
+class Sha1Digest : public Digest\r
+{\r
+public:\r
+\r
+    /**\r
+     *  Constructor\r
+     */\r
+    Sha1Digest()\r
+        { hashType = HASH_SHA1; reset(); }\r
+\r
+    /**\r
+     *  Destructor\r
+     */\r
+    virtual ~Sha1Digest()\r
+        { reset(); }\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual void reset();\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual std::vector<unsigned char> finish();\r
+\r
+protected:\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual void update(unsigned char val);\r
+\r
+private:\r
+\r
+    void hashblock();\r
+\r
+    unsigned long H[5];\r
+    unsigned long W[80];\r
+    unsigned long long size;\r
+    int lenW;\r
+\r
+};\r
+\r
+\r
+\r
+\r
+\r
+\r
+/**\r
+ *  SHA-224,  \r
+ *  Section 6.1, SECURE HASH STANDARD\r
+ *  Federal Information Processing Standards Publication 180-2\r
+ *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf\r
+ */\r
+class Sha224Digest : public Digest\r
+{\r
+public:\r
+\r
+    /**\r
+     *  Constructor\r
+     */\r
+    Sha224Digest()\r
+        { hashType = HASH_SHA224; reset(); }\r
+\r
+    /**\r
+     *  Destructor\r
+     */\r
+    virtual ~Sha224Digest()\r
+        { reset(); }\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual void reset();\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual std::vector<unsigned char> finish();\r
+\r
+protected:\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual void update(unsigned char val);\r
+\r
+private:\r
+\r
+    void hashblock();\r
+\r
+    unsigned long H[8];\r
+    unsigned long W[64];\r
+    unsigned long long size;\r
+    int lenW;\r
+\r
+};\r
+\r
+\r
+\r
+/**\r
+ *  SHA-256,  \r
+ *  Section 6.1, SECURE HASH STANDARD\r
+ *  Federal Information Processing Standards Publication 180-2\r
+ *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf\r
+ */\r
+class Sha256Digest : public Digest\r
+{\r
+public:\r
+\r
+    /**\r
+     *  Constructor\r
+     */\r
+    Sha256Digest()\r
+        { hashType = HASH_SHA256; reset(); }\r
+\r
+    /**\r
+     *  Destructor\r
+     */\r
+    virtual ~Sha256Digest()\r
+        { reset(); }\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual void reset();\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual std::vector<unsigned char> finish();\r
+\r
+protected:\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual void update(unsigned char val);\r
+\r
+private:\r
+\r
+    void hashblock();\r
+\r
+    unsigned long H[8];\r
+    unsigned long W[64];\r
+    unsigned long long size;\r
+    int lenW;\r
+\r
+};\r
+\r
+\r
+/**\r
+ *  SHA-384,\r
+ *  Section 6.1, SECURE HASH STANDARD\r
+ *  Federal Information Processing Standards Publication 180-2\r
+ *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf\r
+ */\r
+class Sha384Digest : public Digest\r
+{\r
+public:\r
+\r
+    /**\r
+     *  Constructor\r
+     */\r
+    Sha384Digest()\r
+        { hashType = HASH_SHA384; reset(); }\r
+\r
+    /**\r
+     *  Destructor\r
+     */\r
+    virtual ~Sha384Digest()\r
+        { reset(); }\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual void reset();\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual std::vector<unsigned char> finish();\r
+\r
+protected:\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual void update(unsigned char val);\r
+\r
+private:\r
+\r
+    void hashblock();\r
+\r
+    unsigned long long H[8];\r
+    unsigned long long W[80];\r
+    unsigned long long size;\r
+    int lenW;\r
+\r
+};\r
+\r
+\r
+\r
+\r
+/**\r
+ *  SHA-512,  \r
+ *  Section 6.1, SECURE HASH STANDARD\r
+ *  Federal Information Processing Standards Publication 180-2\r
+ *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf\r
+ */\r
+class Sha512Digest : public Digest\r
+{\r
+public:\r
+\r
+    /**\r
+     *  Constructor\r
+     */\r
+    Sha512Digest()\r
+        { hashType = HASH_SHA512; reset(); }\r
+\r
+    /**\r
+     *  Destructor\r
+     */\r
+    virtual ~Sha512Digest()\r
+        { reset(); }\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual void reset();\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual std::vector<unsigned char> finish();\r
+\r
+protected:\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual void update(unsigned char val);\r
+\r
+private:\r
+\r
+    void hashblock();\r
+\r
+    unsigned long long H[8];\r
+    unsigned long long W[80];\r
+    unsigned long long size;\r
+    int lenW;\r
+\r
+};\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+/**\r
+ * IETF RFC 1321, MD5 Specification\r
+ * http://www.ietf.org/rfc/rfc1321.txt \r
+ */\r
+class Md5Digest :  public Digest\r
+{\r
+public:\r
+\r
+    /**\r
+     *  Constructor\r
+     */\r
+    Md5Digest()\r
+        { hashType = HASH_MD5; reset(); }\r
+\r
+    /**\r
+     *  Destructor\r
+     */\r
+    virtual ~Md5Digest()\r
+        { reset(); }\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual void reset();\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual std::vector<unsigned char> finish();\r
+\r
+protected:\r
+\r
+    /**\r
+     *  Overloaded from Digest\r
+     */\r
+    virtual void update(unsigned char val);\r
+\r
+private:\r
+\r
+    void hashblock();\r
+\r
+    unsigned long hash[8];\r
+    unsigned long W[64];\r
+    unsigned long long size;\r
+    int lenW;\r
+\r
+};\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+#endif /*  __DIGEST_H__ */\r
+\r
+\r