Code

Rearrange to enable code that does not directly rely on lcms.
[inkscape.git] / src / dom / util / digest.cpp
1 /**
2  * Secure Hashing Tool
3  * *
4  * Authors:
5  *   Bob Jamison
6  *
7  * Copyright (C) 2006-2008 Bob Jamison
8  *
9  *  This library is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU Lesser General Public
11  *  License as published by the Free Software Foundation; either
12  *  version 2.1 of the License, or (at your option) any later version.
13  *
14  *  This library is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  Lesser General Public License for more details.
18  *
19  *  You should have received a copy of the GNU Lesser General Public
20  *  License along with this library; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  */
24 #include "digest.h"
27 //########################################################################
28 //##  U T I L I T Y
29 //########################################################################
31 /**
32  * Use this to print out a 64-bit int when otherwise difficult
33  */
34 /*
35 static void pl(uint64_t val)
36 {
37     for (int shift=56 ; shift>=0 ; shift-=8)
38         {
39         int ch = (val >> shift) & 0xff;
40         printf("%02x", ch);
41         }
42 }
43 */
47 /**
48  * 3These truncate their arguments to
49  * unsigned 32-bit or unsigned 64-bit.
50  */
51 #define TR32(x) ((x) & 0xffffffffL)
52 #define TR64(x) ((x) & 0xffffffffffffffffLL)
55 static const char *hexDigits = "0123456789abcdef";
57 static std::string toHex(const std::vector<unsigned char> &bytes)
58 {
59     std::string str;
60     std::vector<unsigned char>::const_iterator iter;
61     for (iter = bytes.begin() ; iter != bytes.end() ; iter++)
62        {
63        unsigned char ch = *iter;
64        str.push_back(hexDigits[(ch>>4) & 0x0f]);
65        str.push_back(hexDigits[(ch   ) & 0x0f]);
66        }
67     return str;
68 }
71 //########################################################################
72 //##  D I G E S T
73 //########################################################################
76 /**
77  *
78  */
79 std::string Digest::finishHex()
80 {
81     std::vector<unsigned char> hash = finish();
82     std::string str = toHex(hash);
83     return str;
84 }
86 /**
87  * Convenience method.  This is a simple way of getting a hash
88  */
89 std::vector<unsigned char> Digest::hash(Digest::HashType typ,
90                        unsigned char *buf,
91                        int len)
92 {
93     std::vector<unsigned char> ret;
94     switch (typ)
95         {
96         case HASH_MD5:
97             {
98             Md5 digest;
99             digest.append(buf, len);
100             ret = digest.finish();
101             break;
102             }
103         case HASH_SHA1:
104             {
105             Sha1 digest;
106             digest.append(buf, len);
107             ret = digest.finish();
108             break;
109             }
110         case HASH_SHA224:
111             {
112             Sha224 digest;
113             digest.append(buf, len);
114             ret = digest.finish();
115             break;
116             }
117         case HASH_SHA256:
118             {
119             Sha256 digest;
120             digest.append(buf, len);
121             ret = digest.finish();
122             break;
123             }
124         case HASH_SHA384:
125             {
126             Sha384 digest;
127             digest.append(buf, len);
128             ret = digest.finish();
129             break;
130             }
131         case HASH_SHA512:
132             {
133             Sha512 digest;
134             digest.append(buf, len);
135             ret = digest.finish();
136             break;
137             }
138         default:
139             {
140             break;
141             }
142         }
143     return ret;
147 /**
148  * Convenience method.  Same as above, but for a std::string
149  */
150 std::vector<unsigned char> Digest::hash(Digest::HashType typ,
151                        const std::string &str)
153     return hash(typ, (unsigned char *)str.c_str(), str.size());
156 /**
157  * Convenience method.  Return a hexidecimal string of the hash of the buffer.
158  */
159 std::string Digest::hashHex(Digest::HashType typ,
160                unsigned char *buf,
161                int len)
163     std::vector<unsigned char> dig = hash(typ, buf, len);
164     return toHex(dig);
167 /**
168  * Convenience method.  Return a hexidecimal string of the hash of the
169  *  string argument
170  */
171 std::string Digest::hashHex(Digest::HashType typ,
172                const std::string &str)
174     std::vector<unsigned char> dig = hash(typ, str);
175     return toHex(dig);
180 //4.1.1 and 4.1.2
181 #define SHA_ROTL(X,n) ((((X) << (n)) & 0xffffffffL) | (((X) >> (32-(n))) & 0xffffffffL))
182 #define SHA_Ch(x,y,z)  ((z)^((x)&((y)^(z))))
183 #define SHA_Maj(x,y,z) (((x)&(y))^((z)&((x)^(y))))
186 //########################################################################
187 //##  S H A 1
188 //########################################################################
191 /**
192  *
193  */
194 void Sha1::reset()
196     longNr    = 0;
197     byteNr    = 0;
199     // Initialize H with the magic constants (see FIPS180 for constants)
200     hashBuf[0] = 0x67452301L;
201     hashBuf[1] = 0xefcdab89L;
202     hashBuf[2] = 0x98badcfeL;
203     hashBuf[3] = 0x10325476L;
204     hashBuf[4] = 0xc3d2e1f0L;
206     for (int i = 0; i < 4; i++)
207         inb[i] = 0;
209     for (int i = 0; i < 80; i++)
210         inBuf[i] = 0;
212     clearByteCount();
216 /**
217  *
218  */
219 void Sha1::update(unsigned char ch)
221     incByteCount();
223     inb[byteNr++] = (uint32_t)ch;
224     if (byteNr >= 4)
225         {
226         inBuf[longNr++] = inb[0] << 24 | inb[1] << 16 |
227                           inb[2] << 8  | inb[3];
228         byteNr = 0;
229         }
230     if (longNr >= 16)
231         {
232         transform();
233         longNr = 0;
234         }
238 void Sha1::transform()
240     uint32_t *W = inBuf;
241     uint32_t *H = hashBuf;
243     //for (int t = 0; t < 16 ; t++)
244     //    printf("%2d %08lx\n", t, W[t]);
246     //see 6.1.2
247     for (int t = 16; t < 80 ; t++)
248         W[t] = SHA_ROTL((W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]), 1);
250     uint32_t a = H[0];
251     uint32_t b = H[1];
252     uint32_t c = H[2];
253     uint32_t d = H[3];
254     uint32_t e = H[4];
256     uint32_t T;
258     int t = 0;
259     for ( ; t < 20 ; t++)
260         {
261         //see 4.1.1 for the boolops on B,C, and D
262         T = TR32(SHA_ROTL(a,5) + ((b&c)|((~b)&d)) +  //Ch(b,c,d))
263                 e + 0x5a827999L + W[t]);
264         e = d; d = c; c = SHA_ROTL(b, 30); b = a; a = T;
265         //printf("%2d %08lx %08lx %08lx %08lx %08lx\n", t, a, b, c, d, e);
266         }
267     for ( ; t < 40 ; t++)
268         {
269         T = TR32(SHA_ROTL(a,5) + (b^c^d) + e + 0x6ed9eba1L + W[t]);
270         e = d; d = c; c = SHA_ROTL(b, 30); b = a; a = T;
271         //printf("%2d %08lx %08lx %08lx %08lx %08lx\n", t, a, b, c, d, e);
272         }
273     for ( ; t < 60 ; t++)
274         {
275         T = TR32(SHA_ROTL(a,5) + ((b&c)^(b&d)^(c&d)) +
276                 e + 0x8f1bbcdcL + W[t]);
277         e = d; d = c; c = SHA_ROTL(b, 30); b = a; a = T;
278         //printf("%2d %08lx %08lx %08lx %08lx %08lx\n", t, a, b, c, d, e);
279         }
280     for ( ; t < 80 ; t++)
281         {
282         T = TR32(SHA_ROTL(a,5) + (b^c^d) +
283                 e + 0xca62c1d6L + W[t]);
284         e = d; d = c; c = SHA_ROTL(b, 30); b = a; a = T;
285         //printf("%2d %08lx %08lx %08lx %08lx %08lx\n", t, a, b, c, d, e);
286         }
288     H[0] = TR32(H[0] + a);
289     H[1] = TR32(H[1] + b);
290     H[2] = TR32(H[2] + c);
291     H[3] = TR32(H[3] + d);
292     H[4] = TR32(H[4] + e);
298 /**
299  *
300  */
301 std::vector<unsigned char> Sha1::finish()
303     //snapshot the bit count now before padding
304     getBitCount();
306     //Append terminal char
307     update(0x80);
309     //pad until we have a 56 of 64 bytes, allowing for 8 bytes at the end
310     while ((nrBytes & 63) != 56)
311         update(0);
313     //##### Append length in bits
314     appendBitCount();
316     //copy out answer
317     std::vector<unsigned char> res;
318     for (int i=0 ; i<5 ; i++)
319         {
320         res.push_back((unsigned char)((hashBuf[i] >> 24) & 0xff));
321         res.push_back((unsigned char)((hashBuf[i] >> 16) & 0xff));
322         res.push_back((unsigned char)((hashBuf[i] >>  8) & 0xff));
323         res.push_back((unsigned char)((hashBuf[i]      ) & 0xff));
324         }
326     // Re-initialize the context (also zeroizes contents)
327     reset();
329     return res;
335 //########################################################################
336 //##  SHA224
337 //########################################################################
340 /**
341  * SHA-224 and SHA-512 share the same operations and constants
342  */ 
344 #define    SHA_Rot32(x,s) ((((x) >> s)&0xffffffffL) | (((x) << (32 - s))&0xffffffffL))
345 #define    SHA_SIGMA0(x)  (SHA_Rot32(x,  2) ^ SHA_Rot32(x, 13) ^ SHA_Rot32(x, 22))
346 #define    SHA_SIGMA1(x)  (SHA_Rot32(x,  6) ^ SHA_Rot32(x, 11) ^ SHA_Rot32(x, 25))
347 #define    SHA_sigma0(x)  (SHA_Rot32(x,  7) ^ SHA_Rot32(x, 18) ^ ((x) >> 3))
348 #define    SHA_sigma1(x)  (SHA_Rot32(x, 17) ^ SHA_Rot32(x, 19) ^ ((x) >> 10))
351 static uint32_t sha256table[64] =
353     0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
354     0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
355     0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
356     0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
357     0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
358     0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
359     0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
360     0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
361     0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
362     0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
363     0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
364     0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
365     0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
366     0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
367     0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
368     0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
369 };
375 /**
376  *
377  */
378 void Sha224::reset()
380     longNr   = 0;
381     byteNr   = 0;
383     // Initialize H with the magic constants (see FIPS180 for constants)
384     hashBuf[0] = 0xc1059ed8L;
385     hashBuf[1] = 0x367cd507L;
386     hashBuf[2] = 0x3070dd17L;
387     hashBuf[3] = 0xf70e5939L;
388     hashBuf[4] = 0xffc00b31L;
389     hashBuf[5] = 0x68581511L;
390     hashBuf[6] = 0x64f98fa7L;
391     hashBuf[7] = 0xbefa4fa4L;
393     for (int i = 0 ; i < 64 ; i++)
394         inBuf[i] = 0;
396     for (int i = 0 ; i < 4 ; i++)
397         inb[i] = 0;
399     clearByteCount();
403 /**
404  *
405  */
406 void Sha224::update(unsigned char ch)
408     incByteCount();
410     inb[byteNr++] = (uint32_t)ch;
411     if (byteNr >= 4)
412         {
413         inBuf[longNr++] = inb[0] << 24 | inb[1] << 16 |
414                           inb[2] << 8  | inb[3];
415         byteNr = 0;
416         }
417     if (longNr >= 16)
418         {
419         transform();
420         longNr = 0;
421         }
425 void Sha224::transform()
427     uint32_t *W = inBuf;
428     uint32_t *H = hashBuf;
430     //for (int t = 0; t < 16 ; t++)
431     //    printf("%2d %08lx\n", t, W[t]);
433     //see 6.2.2
434     for (int t = 16; t < 64 ; t++)
435         W[t] = TR32(SHA_sigma1(W[t-2]) + W[t-7] + SHA_sigma0(W[t-15]) + W[t-16]);
437     uint32_t a = H[0];
438     uint32_t b = H[1];
439     uint32_t c = H[2];
440     uint32_t d = H[3];
441     uint32_t e = H[4];
442     uint32_t f = H[5];
443     uint32_t g = H[6];
444     uint32_t h = H[7];
446     for (int t = 0 ; t < 64 ; t++)
447         {
448         //see 4.1.1 for the boolops
449         uint32_t T1 = TR32(h + SHA_SIGMA1(e) + SHA_Ch(e,f,g) +
450             sha256table[t] + W[t]);
451         uint32_t T2 = TR32(SHA_SIGMA0(a) + SHA_Maj(a,b,c));
452         h = g; g = f; f = e; e = TR32(d  + T1); d = c; c = b; b = a; a = TR32(T1 + T2);
453         //printf("%2d %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
454         //         t, a, b, c, d, e, f, g, h);
455         }
457     H[0] = TR32(H[0] + a);
458     H[1] = TR32(H[1] + b);
459     H[2] = TR32(H[2] + c);
460     H[3] = TR32(H[3] + d);
461     H[4] = TR32(H[4] + e);
462     H[5] = TR32(H[5] + f);
463     H[6] = TR32(H[6] + g);
464     H[7] = TR32(H[7] + h);
469 /**
470  *
471  */
472 std::vector<unsigned char> Sha224::finish()
474     //save our size before padding
475     getBitCount();
476     
477     // Pad with a binary 1 (0x80)
478     update(0x80);
479     //append 0's to make a 56-byte buf.
480     while ((nrBytes & 63) != 56)
481         update(0);
483     //##### Append length in bits
484     appendBitCount();
486     // Output hash
487     std::vector<unsigned char> ret;
488     for (int i = 0 ; i < 7 ; i++)
489         {
490         ret.push_back((unsigned char)((hashBuf[i] >> 24) & 0xff));
491         ret.push_back((unsigned char)((hashBuf[i] >> 16) & 0xff));
492         ret.push_back((unsigned char)((hashBuf[i] >>  8) & 0xff));
493         ret.push_back((unsigned char)((hashBuf[i]      ) & 0xff));
494         }
496     // Re-initialize the context (also zeroizes contents)
497     reset();
499     return ret;
505 //########################################################################
506 //##  SHA256
507 //########################################################################
510 /**
511  *
512  */
513 void Sha256::reset()
515     longNr   = 0;
516     byteNr   = 0;
518     // Initialize H with the magic constants (see FIPS180 for constants)
519     hashBuf[0] = 0x6a09e667L;
520     hashBuf[1] = 0xbb67ae85L;
521     hashBuf[2] = 0x3c6ef372L;
522     hashBuf[3] = 0xa54ff53aL;
523     hashBuf[4] = 0x510e527fL;
524     hashBuf[5] = 0x9b05688cL;
525     hashBuf[6] = 0x1f83d9abL;
526     hashBuf[7] = 0x5be0cd19L;
528     for (int i = 0 ; i < 64 ; i++)
529         inBuf[i] = 0;
530     for (int i = 0 ; i < 4 ; i++)
531         inb[i] = 0;
533     clearByteCount();
537 /**
538  *
539  */
540 void Sha256::update(unsigned char ch)
542     incByteCount();
544     inb[byteNr++] = (uint32_t)ch;
545     if (byteNr >= 4)
546         {
547         inBuf[longNr++] = inb[0] << 24 | inb[1] << 16 |
548                           inb[2] << 8  | inb[3];
549         byteNr = 0;
550         }
551     if (longNr >= 16)
552         {
553         transform();
554         longNr = 0;
555         }
561 void Sha256::transform()
563     uint32_t *H = hashBuf;
564     uint32_t *W = inBuf;
566     //for (int t = 0; t < 16 ; t++)
567     //    printf("%2d %08lx\n", t, W[t]);
569     //see 6.2.2
570     for (int t = 16; t < 64 ; t++)
571         W[t] = TR32(SHA_sigma1(W[t-2]) + W[t-7] + SHA_sigma0(W[t-15]) + W[t-16]);
573     uint32_t a = H[0];
574     uint32_t b = H[1];
575     uint32_t c = H[2];
576     uint32_t d = H[3];
577     uint32_t e = H[4];
578     uint32_t f = H[5];
579     uint32_t g = H[6];
580     uint32_t h = H[7];
582     for (int t = 0 ; t < 64 ; t++)
583         {
584         //see 4.1.1 for the boolops
585         uint32_t T1 = TR32(h + SHA_SIGMA1(e) + SHA_Ch(e,f,g) +
586             sha256table[t] + W[t]);
587         uint32_t T2 = TR32(SHA_SIGMA0(a) + SHA_Maj(a,b,c));
588         h = g; g = f; f = e; e = TR32(d  + T1); d = c; c = b; b = a; a = TR32(T1 + T2);
589         //printf("%2d %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
590         //         t, a, b, c, d, e, f, g, h);
591         }
593     H[0] = TR32(H[0] + a);
594     H[1] = TR32(H[1] + b);
595     H[2] = TR32(H[2] + c);
596     H[3] = TR32(H[3] + d);
597     H[4] = TR32(H[4] + e);
598     H[5] = TR32(H[5] + f);
599     H[6] = TR32(H[6] + g);
600     H[7] = TR32(H[7] + h);
605 /**
606  *
607  */
608 std::vector<unsigned char> Sha256::finish()
610     //save our size before padding
611     getBitCount();
612     
613     // Pad with a binary 1 (0x80)
614     update(0x80);
615     //append 0's to make a 56-byte buf.
616     while ((nrBytes & 63) != 56)
617         update(0);
619     //##### Append length in bits
620     appendBitCount();
622     // Output hash
623     std::vector<unsigned char> ret;
624     for (int i = 0 ; i < 8 ; i++)
625         {
626         ret.push_back((unsigned char)((hashBuf[i] >> 24) & 0xff));
627         ret.push_back((unsigned char)((hashBuf[i] >> 16) & 0xff));
628         ret.push_back((unsigned char)((hashBuf[i] >>  8) & 0xff));
629         ret.push_back((unsigned char)((hashBuf[i]      ) & 0xff));
630         }
632     // Re-initialize the context (also zeroizes contents)
633     reset();
635     return ret;
641 //########################################################################
642 //##  SHA384
643 //########################################################################
646 /**
647  * SHA-384 and SHA-512 share the same operations and constants
648  */
649   
650 #undef SHA_SIGMA0
651 #undef SHA_SIGMA1
652 #undef SHA_sigma0
653 #undef SHA_sigma1
655 #define SHA_Rot64(x,s) (((x) >> s) | ((x) << (64 - s)))
656 #define SHA_SIGMA0(x)  (SHA_Rot64(x, 28) ^ SHA_Rot64(x, 34) ^ SHA_Rot64(x, 39))
657 #define SHA_SIGMA1(x)  (SHA_Rot64(x, 14) ^ SHA_Rot64(x, 18) ^ SHA_Rot64(x, 41))
658 #define SHA_sigma0(x)  (SHA_Rot64(x,  1) ^ SHA_Rot64(x,  8) ^ ((x) >> 7))
659 #define SHA_sigma1(x)  (SHA_Rot64(x, 19) ^ SHA_Rot64(x, 61) ^ ((x) >> 6))
662 static uint64_t sha512table[80] =
664     0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
665     0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
666     0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
667     0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
668     0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
669     0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
670     0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
671     0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
672     0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
673     0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
674     0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
675     0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
676     0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
677     0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
678     0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
679     0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
680     0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
681     0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
682     0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
683     0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
684     0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
685     0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
686     0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
687     0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
688     0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
689     0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
690     0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
691     0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
692     0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
693     0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
694     0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
695     0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
696     0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
697     0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
698     0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
699     0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
700     0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
701     0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
702     0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
703     0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
704 };
709 /**
710  *
711  */
712 void Sha384::reset()
714     longNr   = 0;
715     byteNr   = 0;
717     // SHA-384 differs from SHA-512 by these constants
718     hashBuf[0] = 0xcbbb9d5dc1059ed8ULL;
719     hashBuf[1] = 0x629a292a367cd507ULL;
720     hashBuf[2] = 0x9159015a3070dd17ULL;
721     hashBuf[3] = 0x152fecd8f70e5939ULL;
722     hashBuf[4] = 0x67332667ffc00b31ULL;
723     hashBuf[5] = 0x8eb44a8768581511ULL;
724     hashBuf[6] = 0xdb0c2e0d64f98fa7ULL;
725     hashBuf[7] = 0x47b5481dbefa4fa4ULL;
727     for (int i = 0 ; i < 80 ; i++)
728         inBuf[i] = 0;
729     for (int i = 0 ; i < 8 ; i++)
730         inb[i] = 0;
732     clearByteCount();
736 /**
737  *  Note that this version of update() handles 64-bit inBuf
738  *  values. 
739  */
740 void Sha384::update(unsigned char ch)
742     incByteCount();
744     inb[byteNr++] = (uint64_t)ch;
745     if (byteNr >= 8)
746         {
747         inBuf[longNr++] = inb[0] << 56 | inb[1] << 48 |
748                           inb[2] << 40 | inb[3] << 32 |
749                           inb[4] << 24 | inb[5] << 16 |
750                           inb[6] <<  8 | inb[7];
751         byteNr = 0;
752         }
753     if (longNr >= 16)
754         {
755         transform();
756         longNr = 0;
757         }
763 void Sha384::transform()
765     uint64_t *H = hashBuf;
766     uint64_t *W = inBuf;
768     /*
769     for (int t = 0; t < 16 ; t++)
770         {
771         printf("%2d ", t);
772         pl(W[t]);
773         printf("\n");
774         }
775     */
777     //see 6.2.2
778     for (int t = 16; t < 80 ; t++)
779         W[t] = TR64(SHA_sigma1(W[t-2]) + W[t-7] + SHA_sigma0(W[t-15]) + W[t-16]);
781     uint64_t a = H[0];
782     uint64_t b = H[1];
783     uint64_t c = H[2];
784     uint64_t d = H[3];
785     uint64_t e = H[4];
786     uint64_t f = H[5];
787     uint64_t g = H[6];
788     uint64_t h = H[7];
790     for (int t = 0 ; t < 80 ; t++)
791         {
792         //see 4.1.1 for the boolops
793         uint64_t T1 = TR64(h + SHA_SIGMA1(e) + SHA_Ch(e,f,g) +
794             sha512table[t] + W[t]);
795         uint64_t T2 = TR64(SHA_SIGMA0(a) + SHA_Maj(a,b,c));
796         h = g; g = f; f = e; e = TR64(d  + T1); d = c; c = b; b = a; a = TR64(T1 + T2);
797         }
799     H[0] = TR64(H[0] + a);  
800     H[1] = TR64(H[1] + b);  
801     H[2] = TR64(H[2] + c);  
802     H[3] = TR64(H[3] + d);  
803     H[4] = TR64(H[4] + e);  
804     H[5] = TR64(H[5] + f);  
805     H[6] = TR64(H[6] + g);  
806     H[7] = TR64(H[7] + h);  
811 /**
812  *
813  */
814 std::vector<unsigned char> Sha384::finish()
816     //save our size before padding
817     getBitCount();
818     
819     // Pad with a binary 1 (0x80)
820     update((unsigned char)0x80);
821     //append 0's to make a 112-byte buf.
822     //we will loop around once if already over 112
823     while ((nrBytes & 127) != 112)
824         update(0);
825         
826     //append 128-bit size
827     //64 upper bits
828     for (int i = 0 ; i < 8 ; i++)
829         update((unsigned char)0x00);
830     //64 lower bits
831     //##### Append length in bits
832     appendBitCount();
834     // Output hash
835     //for SHA-384, we use the left-most 6 64-bit words
836     std::vector<unsigned char> ret;
837     for (int i = 0 ; i < 6 ; i++)
838         {
839         ret.push_back((unsigned char)((hashBuf[i] >> 56) & 0xff));
840         ret.push_back((unsigned char)((hashBuf[i] >> 48) & 0xff));
841         ret.push_back((unsigned char)((hashBuf[i] >> 40) & 0xff));
842         ret.push_back((unsigned char)((hashBuf[i] >> 32) & 0xff));
843         ret.push_back((unsigned char)((hashBuf[i] >> 24) & 0xff));
844         ret.push_back((unsigned char)((hashBuf[i] >> 16) & 0xff));
845         ret.push_back((unsigned char)((hashBuf[i] >>  8) & 0xff));
846         ret.push_back((unsigned char)((hashBuf[i]      ) & 0xff));
847         }
849     // Re-initialize the context (also zeroizes contents)
850     reset();
852     return ret;
857 //########################################################################
858 //##  SHA512
859 //########################################################################
865 /**
866  *
867  */
868 void Sha512::reset()
870     longNr   = 0;
871     byteNr   = 0;
873     // Initialize H with the magic constants (see FIPS180 for constants)
874     hashBuf[0] = 0x6a09e667f3bcc908ULL;
875     hashBuf[1] = 0xbb67ae8584caa73bULL;
876     hashBuf[2] = 0x3c6ef372fe94f82bULL;
877     hashBuf[3] = 0xa54ff53a5f1d36f1ULL;
878     hashBuf[4] = 0x510e527fade682d1ULL;
879     hashBuf[5] = 0x9b05688c2b3e6c1fULL;
880     hashBuf[6] = 0x1f83d9abfb41bd6bULL;
881     hashBuf[7] = 0x5be0cd19137e2179ULL;
883     for (int i = 0 ; i < 80 ; i++)
884         inBuf[i] = 0;
885     for (int i = 0 ; i <  8 ; i++)
886         inb[i] = 0;
888     clearByteCount();
892 /**
893  *  Note that this version of update() handles 64-bit inBuf
894  *  values. 
895  */
896 void Sha512::update(unsigned char ch)
898     incByteCount();
900     inb[byteNr++] = (uint64_t)ch;
901     if (byteNr >= 8)
902         {
903         inBuf[longNr++] = inb[0] << 56 | inb[1] << 48 |
904                           inb[2] << 40 | inb[3] << 32 |
905                           inb[4] << 24 | inb[5] << 16 |
906                           inb[6] <<  8 | inb[7];
907         byteNr = 0;
908         }
909     if (longNr >= 16)
910         {
911         transform();
912         longNr = 0;
913         }
919 void Sha512::transform()
921     uint64_t *W = inBuf;
922     uint64_t *H = hashBuf;
924     /*
925     for (int t = 0; t < 16 ; t++)
926         {
927         printf("%2d ", t);
928         pl(W[t]);
929         printf("\n");
930         }
931     */
933     //see 6.2.2
934     for (int t = 16; t < 80 ; t++)
935         W[t] = TR64(SHA_sigma1(W[t-2]) + W[t-7] + SHA_sigma0(W[t-15]) + W[t-16]);
937     uint64_t a = H[0];
938     uint64_t b = H[1];
939     uint64_t c = H[2];
940     uint64_t d = H[3];
941     uint64_t e = H[4];
942     uint64_t f = H[5];
943     uint64_t g = H[6];
944     uint64_t h = H[7];
946     for (int t = 0 ; t < 80 ; t++)
947         {
948         //see 4.1.1 for the boolops
949         uint64_t T1 = TR64(h + SHA_SIGMA1(e) + SHA_Ch(e,f,g) +
950             sha512table[t] + W[t]);
951         uint64_t T2 = TR64(SHA_SIGMA0(a) + SHA_Maj(a,b,c));
952         h = g; g = f; f = e; e = TR64(d  + T1); d = c; c = b; b = a; a = TR64(T1 + T2);
953         }
955     H[0] = TR64(H[0] + a);  
956     H[1] = TR64(H[1] + b);  
957     H[2] = TR64(H[2] + c);  
958     H[3] = TR64(H[3] + d);  
959     H[4] = TR64(H[4] + e);  
960     H[5] = TR64(H[5] + f);  
961     H[6] = TR64(H[6] + g);  
962     H[7] = TR64(H[7] + h);  
967 /**
968  *
969  */
970 std::vector<unsigned char> Sha512::finish()
972     //save our size before padding
973     getBitCount();
974     
975     // Pad with a binary 1 (0x80)
976     update(0x80);
977     //append 0's to make a 112-byte buf.
978     //we will loop around once if already over 112
979     while ((nrBytes & 127) != 112)
980         update(0);
981         
982     //append 128-bit size
983     //64 upper bits
984     for (int i = 0 ; i < 8 ; i++)
985         update((unsigned char)0x00);
986     //64 lower bits
987     //##### Append length in bits
988     appendBitCount();
990     // Output hash
991     std::vector<unsigned char> ret;
992     for (int i = 0 ; i < 8 ; i++)
993         {
994         ret.push_back((unsigned char)((hashBuf[i] >> 56) & 0xff));
995         ret.push_back((unsigned char)((hashBuf[i] >> 48) & 0xff));
996         ret.push_back((unsigned char)((hashBuf[i] >> 40) & 0xff));
997         ret.push_back((unsigned char)((hashBuf[i] >> 32) & 0xff));
998         ret.push_back((unsigned char)((hashBuf[i] >> 24) & 0xff));
999         ret.push_back((unsigned char)((hashBuf[i] >> 16) & 0xff));
1000         ret.push_back((unsigned char)((hashBuf[i] >>  8) & 0xff));
1001         ret.push_back((unsigned char)((hashBuf[i]      ) & 0xff));
1002         }
1004     // Re-initialize the context (also zeroizes contents)
1005     reset();
1007     return ret;
1013 //########################################################################
1014 //##  M D 5
1015 //########################################################################
1017 /**
1018  *
1019  */
1020 void Md5::reset()
1022     hashBuf[0]  = 0x67452301;
1023     hashBuf[1]  = 0xefcdab89;
1024     hashBuf[2]  = 0x98badcfe;
1025     hashBuf[3]  = 0x10325476;
1027     for (int i=0 ; i<16 ; i++)
1028         inBuf[i] = 0;
1029     for (int i=0 ; i<4 ; i++)
1030         inb[i] = 0;
1032     clearByteCount();
1034     byteNr    = 0;
1035     longNr    = 0;
1039 /**
1040  *
1041  */
1042 void Md5::update(unsigned char ch)
1044     incByteCount();
1046     //pack 64 bytes into 16 longs
1047     inb[byteNr++] = (uint32_t)ch;
1048     if (byteNr >= 4)
1049         {
1050         //note the little-endianness
1051         uint32_t val =
1052              inb[3] << 24 | inb[2] << 16 | inb[1] << 8 | inb[0];
1053         inBuf[longNr++] = val;
1054         byteNr = 0;
1055         }
1056     if (longNr >= 16)
1057         {
1058         transform();
1059         longNr = 0;
1060         }
1065 //#  The four core functions - F1 is optimized somewhat
1067 // #define F1(x, y, z) (x & y | ~x & z)
1068 #define F1(x, y, z) (z ^ (x & (y ^ z)))
1069 #define F2(x, y, z) F1(z, x, y)
1070 #define F3(x, y, z) (x ^ y ^ z)
1071 #define F4(x, y, z) (y ^ (x | ~z))
1073 // ## This is the central step in the MD5 algorithm.
1074 #define MD5STEP(f, w, x, y, z, data, s) \
1075     ( w = TR32(w + (f(x, y, z) + data)), w = w<<s | w>>(32-s), w = TR32(w + x) )
1077 /*
1078  * The core of the MD5 algorithm, this alters an existing MD5 hash to
1079  * reflect the addition of 16 longwords of new data.  MD5Update blocks
1080  * the data and converts bytes into longwords for this routine.
1081  * @parm buf points to an array of 4 unsigned 32bit (at least) integers
1082  * @parm in points to an array of 16 unsigned 32bit (at least) integers
1083  */
1084 void Md5::transform()
1086     uint32_t *i = inBuf;
1087     uint32_t a  = hashBuf[0];
1088     uint32_t b  = hashBuf[1];
1089     uint32_t c  = hashBuf[2];
1090     uint32_t d  = hashBuf[3];
1092     MD5STEP(F1, a, b, c, d, i[ 0] + 0xd76aa478,  7);
1093     MD5STEP(F1, d, a, b, c, i[ 1] + 0xe8c7b756, 12);
1094     MD5STEP(F1, c, d, a, b, i[ 2] + 0x242070db, 17);
1095     MD5STEP(F1, b, c, d, a, i[ 3] + 0xc1bdceee, 22);
1096     MD5STEP(F1, a, b, c, d, i[ 4] + 0xf57c0faf,  7);
1097     MD5STEP(F1, d, a, b, c, i[ 5] + 0x4787c62a, 12);
1098     MD5STEP(F1, c, d, a, b, i[ 6] + 0xa8304613, 17);
1099     MD5STEP(F1, b, c, d, a, i[ 7] + 0xfd469501, 22);
1100     MD5STEP(F1, a, b, c, d, i[ 8] + 0x698098d8,  7);
1101     MD5STEP(F1, d, a, b, c, i[ 9] + 0x8b44f7af, 12);
1102     MD5STEP(F1, c, d, a, b, i[10] + 0xffff5bb1, 17);
1103     MD5STEP(F1, b, c, d, a, i[11] + 0x895cd7be, 22);
1104     MD5STEP(F1, a, b, c, d, i[12] + 0x6b901122,  7);
1105     MD5STEP(F1, d, a, b, c, i[13] + 0xfd987193, 12);
1106     MD5STEP(F1, c, d, a, b, i[14] + 0xa679438e, 17);
1107     MD5STEP(F1, b, c, d, a, i[15] + 0x49b40821, 22);
1109     MD5STEP(F2, a, b, c, d, i[ 1] + 0xf61e2562,  5);
1110     MD5STEP(F2, d, a, b, c, i[ 6] + 0xc040b340,  9);
1111     MD5STEP(F2, c, d, a, b, i[11] + 0x265e5a51, 14);
1112     MD5STEP(F2, b, c, d, a, i[ 0] + 0xe9b6c7aa, 20);
1113     MD5STEP(F2, a, b, c, d, i[ 5] + 0xd62f105d,  5);
1114     MD5STEP(F2, d, a, b, c, i[10] + 0x02441453,  9);
1115     MD5STEP(F2, c, d, a, b, i[15] + 0xd8a1e681, 14);
1116     MD5STEP(F2, b, c, d, a, i[ 4] + 0xe7d3fbc8, 20);
1117     MD5STEP(F2, a, b, c, d, i[ 9] + 0x21e1cde6,  5);
1118     MD5STEP(F2, d, a, b, c, i[14] + 0xc33707d6,  9);
1119     MD5STEP(F2, c, d, a, b, i[ 3] + 0xf4d50d87, 14);
1120     MD5STEP(F2, b, c, d, a, i[ 8] + 0x455a14ed, 20);
1121     MD5STEP(F2, a, b, c, d, i[13] + 0xa9e3e905,  5);
1122     MD5STEP(F2, d, a, b, c, i[ 2] + 0xfcefa3f8,  9);
1123     MD5STEP(F2, c, d, a, b, i[ 7] + 0x676f02d9, 14);
1124     MD5STEP(F2, b, c, d, a, i[12] + 0x8d2a4c8a, 20);
1126     MD5STEP(F3, a, b, c, d, i[ 5] + 0xfffa3942,  4);
1127     MD5STEP(F3, d, a, b, c, i[ 8] + 0x8771f681, 11);
1128     MD5STEP(F3, c, d, a, b, i[11] + 0x6d9d6122, 16);
1129     MD5STEP(F3, b, c, d, a, i[14] + 0xfde5380c, 23);
1130     MD5STEP(F3, a, b, c, d, i[ 1] + 0xa4beea44,  4);
1131     MD5STEP(F3, d, a, b, c, i[ 4] + 0x4bdecfa9, 11);
1132     MD5STEP(F3, c, d, a, b, i[ 7] + 0xf6bb4b60, 16);
1133     MD5STEP(F3, b, c, d, a, i[10] + 0xbebfbc70, 23);
1134     MD5STEP(F3, a, b, c, d, i[13] + 0x289b7ec6,  4);
1135     MD5STEP(F3, d, a, b, c, i[ 0] + 0xeaa127fa, 11);
1136     MD5STEP(F3, c, d, a, b, i[ 3] + 0xd4ef3085, 16);
1137     MD5STEP(F3, b, c, d, a, i[ 6] + 0x04881d05, 23);
1138     MD5STEP(F3, a, b, c, d, i[ 9] + 0xd9d4d039,  4);
1139     MD5STEP(F3, d, a, b, c, i[12] + 0xe6db99e5, 11);
1140     MD5STEP(F3, c, d, a, b, i[15] + 0x1fa27cf8, 16);
1141     MD5STEP(F3, b, c, d, a, i[ 2] + 0xc4ac5665, 23);
1143     MD5STEP(F4, a, b, c, d, i[ 0] + 0xf4292244,  6);
1144     MD5STEP(F4, d, a, b, c, i[ 7] + 0x432aff97, 10);
1145     MD5STEP(F4, c, d, a, b, i[14] + 0xab9423a7, 15);
1146     MD5STEP(F4, b, c, d, a, i[ 5] + 0xfc93a039, 21);
1147     MD5STEP(F4, a, b, c, d, i[12] + 0x655b59c3,  6);
1148     MD5STEP(F4, d, a, b, c, i[ 3] + 0x8f0ccc92, 10);
1149     MD5STEP(F4, c, d, a, b, i[10] + 0xffeff47d, 15);
1150     MD5STEP(F4, b, c, d, a, i[ 1] + 0x85845dd1, 21);
1151     MD5STEP(F4, a, b, c, d, i[ 8] + 0x6fa87e4f,  6);
1152     MD5STEP(F4, d, a, b, c, i[15] + 0xfe2ce6e0, 10);
1153     MD5STEP(F4, c, d, a, b, i[ 6] + 0xa3014314, 15);
1154     MD5STEP(F4, b, c, d, a, i[13] + 0x4e0811a1, 21);
1155     MD5STEP(F4, a, b, c, d, i[ 4] + 0xf7537e82,  6);
1156     MD5STEP(F4, d, a, b, c, i[11] + 0xbd3af235, 10);
1157     MD5STEP(F4, c, d, a, b, i[ 2] + 0x2ad7d2bb, 15);
1158     MD5STEP(F4, b, c, d, a, i[ 9] + 0xeb86d391, 21);
1160     hashBuf[0] = TR32(hashBuf[0] + a);
1161     hashBuf[1] = TR32(hashBuf[1] + b);
1162     hashBuf[2] = TR32(hashBuf[2] + c);
1163     hashBuf[3] = TR32(hashBuf[3] + d);
1167 /**
1168  *
1169  */
1170 std::vector<unsigned char> Md5::finish()
1172     //snapshot the bit count now before padding
1173     getBitCount();
1175     //Append terminal char
1176     update(0x80);
1178     //pad until we have a 56 of 64 bytes, allowing for 8 bytes at the end
1179     while (longNr != 14)
1180         update(0);
1182     //##### Append length in bits
1183     // Don't use appendBitCount(), since md5 is little-endian
1184     update((unsigned char)((nrBits    ) & 0xff));
1185     update((unsigned char)((nrBits>> 8) & 0xff));
1186     update((unsigned char)((nrBits>>16) & 0xff));
1187     update((unsigned char)((nrBits>>24) & 0xff));
1188     update((unsigned char)((nrBits>>32) & 0xff));
1189     update((unsigned char)((nrBits>>40) & 0xff));
1190     update((unsigned char)((nrBits>>48) & 0xff));
1191     update((unsigned char)((nrBits>>56) & 0xff));
1193     //copy out answer
1194     std::vector<unsigned char> res;
1195     for (int i=0 ; i<4 ; i++)
1196         {
1197         //note the little-endianness
1198         res.push_back((unsigned char)((hashBuf[i]      ) & 0xff));
1199         res.push_back((unsigned char)((hashBuf[i] >>  8) & 0xff));
1200         res.push_back((unsigned char)((hashBuf[i] >> 16) & 0xff));
1201         res.push_back((unsigned char)((hashBuf[i] >> 24) & 0xff));
1202         }
1204     reset();  // Security!  ;-)
1206     return res;
1214 //########################################################################
1215 //## T E S T S
1216 //########################################################################
1218 /**
1219  * Compile this file alone with -DDIGEST_TEST to run the
1220  * tests below:
1221  * > gcc -DDIGEST_TEST digest.cpp -o testdigest
1222  * > testdigest   
1223  * 
1224  * If you add any new algorithms to this suite, then it is highly
1225  * recommended that you add it to these tests and run it.   
1226  */
1227  
1228 #ifdef DIGEST_TEST
1231 typedef struct
1233     const char *msg;
1234     const char *val;
1235 } TestPair;
1237 static TestPair md5tests[] =
1239   {
1240   "",
1241   "d41d8cd98f00b204e9800998ecf8427e"
1242   },
1243   {
1244   "a",
1245   "0cc175b9c0f1b6a831c399e269772661"
1246   },
1247   {
1248   "abc",
1249   "900150983cd24fb0d6963f7d28e17f72"
1250   },
1251   {
1252   "message digest",
1253   "f96b697d7cb7938d525a2f31aaf161d0"
1254   },
1255   {
1256   "abcdefghijklmnopqrstuvwxyz",
1257   "c3fcd3d76192e4007dfb496cca67e13b"
1258   },
1259   {
1260   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
1261   "d174ab98d277d9f5a5611c2c9f419d9f"
1262   },
1263   {
1264   "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
1265   "57edf4a22be3c955ac49da2e2107b67a"
1266   },
1267   {
1268   NULL,
1269   NULL
1270   }
1271 };
1275 static TestPair sha1tests[] =
1277   {
1278   "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
1279   "84983e441c3bd26ebaae4aa1f95129e5e54670f1"
1280   },
1281   {
1282   NULL,
1283   NULL
1284   }
1285 };
1287 static TestPair sha224tests[] =
1289   {
1290   "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
1291   "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525"
1292   },
1293   {
1294   NULL,
1295   NULL
1296   }
1297 };
1299 static TestPair sha256tests[] =
1301   {
1302   "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
1303   "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
1304   },
1305   {
1306   NULL,
1307   NULL
1308   }
1309 };
1311 static TestPair sha384tests[] =
1313   {
1314   "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
1315        "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
1316   "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"
1317        "fcc7c71a557e2db966c3e9fa91746039"
1318   },
1319   {
1320   NULL,
1321   NULL
1322   }
1323 };
1325 static TestPair sha512tests[] =
1327   {
1328   "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
1329        "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
1330   "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"
1331        "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"
1332   },
1333   {
1334   NULL,
1335   NULL
1336   }
1337 };
1340 bool hashTests(Digest &digest, TestPair *tp)
1342     for (TestPair *pair = tp ; pair->msg ; pair++)
1343         {
1344         digest.reset();
1345         std::string msg = pair->msg;
1346         std::string val = pair->val;
1347         digest.append(msg);
1348         std::string res = digest.finishHex();
1349         printf("### Msg '%s':\n   hash '%s'\n   exp  '%s'\n",
1350             msg.c_str(), res.c_str(), val.c_str());
1351         if (res != val)
1352             {
1353             printf("ERROR: Hash mismatch\n");
1354             return false;
1355             }
1356         }
1357     return true;
1361 bool millionATest(Digest &digest, const std::string &exp)
1363     digest.reset();
1364     for (int i=0 ; i<1000000 ; i++)
1365         digest.append('a');
1366     std::string res = digest.finishHex();
1367     printf("\nHash of 1,000,000 'a'\n   calc %s\n   exp  %s\n",
1368          res.c_str(), exp.c_str());
1369     if (res != exp)
1370         {
1371         printf("ERROR: Mismatch.\n");
1372         return false;
1373         }
1374     return true;
1377 static bool doTests()
1379     printf("##########################################\n");
1380     printf("## MD5\n");
1381     printf("##########################################\n");
1382     Md5 md5;
1383     if (!hashTests(md5, md5tests))
1384         return false;
1385     if (!millionATest(md5, "7707d6ae4e027c70eea2a935c2296f21"))
1386         return false;
1387     printf("\n\n\n");
1388     printf("##########################################\n");
1389     printf("## SHA1\n");
1390     printf("##########################################\n");
1391     Sha1 sha1;
1392     if (!hashTests(sha1, sha1tests))
1393         return false;
1394     if (!millionATest(sha1, "34aa973cd4c4daa4f61eeb2bdbad27316534016f"))
1395         return false;
1396     printf("\n\n\n");
1397     printf("##########################################\n");
1398     printf("## SHA224\n");
1399     printf("##########################################\n");
1400     Sha224 sha224;
1401     if (!hashTests(sha224, sha224tests))
1402         return false;
1403     if (!millionATest(sha224,
1404          "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67"))
1405         return false;
1406     printf("\n\n\n");
1407     printf("##########################################\n");
1408     printf("## SHA256\n");
1409     printf("##########################################\n");
1410     Sha256 sha256;
1411     if (!hashTests(sha256, sha256tests))
1412         return false;
1413     if (!millionATest(sha256,
1414          "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"))
1415         return false;
1416     printf("\n\n\n");
1417     printf("##########################################\n");
1418     printf("## SHA384\n");
1419     printf("##########################################\n");
1420     Sha384 sha384;
1421     if (!hashTests(sha384, sha384tests))
1422         return false;
1423     /**/
1424     if (!millionATest(sha384,
1425          "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"
1426                 "07b8b3dc38ecc4ebae97ddd87f3d8985"))
1427         return false;
1428     /**/
1429     printf("\n\n\n");
1430     printf("##########################################\n");
1431     printf("## SHA512\n");
1432     printf("##########################################\n");
1433     Sha512 sha512;
1434     if (!hashTests(sha512, sha512tests))
1435         return false;
1436     if (!millionATest(sha512,
1437          "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"
1438              "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"))
1439         return false;
1440     return true;
1444 int main(int argc, char **argv)
1446     doTests();
1447     printf("####### done ########\n");
1448     return 0;
1452 #endif /* DIGEST_TEST */
1454 //########################################################################
1455 //## E N D    O F    F I L E
1456 //########################################################################