aboutsummaryrefslogtreecommitdiff
path: root/src/stable/md5.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
committerMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
commit469bbcf0701e1eb8a6670c23145b0da87357e178 (patch)
treeb5b062a16e46a6c5d3410b4e574cd0cc09057211 /src/stable/md5.cpp
parentee1b79396076edc4e30aefb285fada03bb45e80d (diff)
downloadlibbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.gz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.bz2
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.xz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.zip
Code is all reorganized. We're about ready to release. I should write up a
little explenation of the arrangement.
Diffstat (limited to 'src/stable/md5.cpp')
-rw-r--r--src/stable/md5.cpp246
1 files changed, 246 insertions, 0 deletions
diff --git a/src/stable/md5.cpp b/src/stable/md5.cpp
new file mode 100644
index 0000000..15cba17
--- /dev/null
+++ b/src/stable/md5.cpp
@@ -0,0 +1,246 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include "bu/md5.h"
12#include "bu/stream.h"
13
14#ifdef SYSTEM_BIG_ENDIAN
15# define toLittleEndian( a, b ) _toLittleEndian( a, b )
16#else
17# define toLittleEndian( a, b ) (void)0
18#endif
19
20Bu::Md5::Md5()
21{
22 reset();
23}
24
25Bu::Md5::~Md5()
26{
27}
28
29void Bu::Md5::reset()
30{
31 // These are the magic seed numbers...
32
33 sum[0] = 0x67452301U;
34 sum[1] = 0xEFCDAB89U;
35 sum[2] = 0x98BADCFEU;
36 sum[3] = 0x10325476U;
37
38 uBits[0] = 0;
39 uBits[1] = 0;
40}
41
42void Bu::Md5::setSalt( const Bu::String & /*sSalt*/ )
43{
44}
45
46void Bu::Md5::addData( const void *sVData, int iSize )
47{
48 const char *sData = (const char *)sVData;
49 uint32_t t;
50
51 t = uBits[0];
52 if( (uBits[0] = t + ((uint32_t)iSize << 3)) < t )
53 uBits[1]++;
54 uBits[1] += iSize >> 29;
55
56 t = (t >> 3) & 0x3f; /* How many bytes we have buffered */
57
58 /* Handle any leading odd-sized chunks */
59 if( t )
60 {
61 unsigned char *p = (unsigned char *) inbuf + t;
62
63 t = 64 - t;
64 if( (uint32_t)iSize < t ) {
65 memcpy( p, sData, iSize );
66 return;
67 }
68 memcpy( p, sData, t );
69 toLittleEndian( inbuf, 16 );
70 compBlock( sum, (uint32_t *)inbuf );
71 sData += t;
72 iSize -= t;
73 }
74
75 /* Process data in 64-byte chunks */
76 while( iSize >= 64 )
77 {
78 memcpy( inbuf, sData, 64 );
79 toLittleEndian( inbuf, 16 );
80 compBlock( sum, (uint32_t *)inbuf );
81 sData += 64;
82 iSize -= 64;
83 }
84
85 /* Handle any remaining bytes of data. */
86 memcpy( inbuf, sData, iSize );
87}
88
89Bu::String Bu::Md5::getResult()
90{
91 uint32_t lsum[4];
92 compCap( lsum );
93 return Bu::String( (const char *)lsum, 4*4 );
94}
95
96void Bu::Md5::writeResult( Bu::Stream &sOut )
97{
98 uint32_t lsum[4];
99 compCap( lsum );
100 sOut.write( lsum, 4*4 );
101}
102
103void Bu::Md5::compCap( uint32_t *sumout )
104{
105 uint8_t tmpbuf[64];
106 memcpy( sumout, sum, 4*4 );
107 memcpy( tmpbuf, inbuf, 64 );
108
109 uint32_t count;
110 uint8_t *p;
111
112 /* Compute number of bytes mod 64 */
113 count = (uBits[0] >> 3) & 0x3F;
114
115 /* Set the first char of padding to 0x80. This is safe since there is
116 always at least one byte free */
117 p = tmpbuf + count;
118 *p++ = 0x80;
119
120 /* Bytes of padding needed to make 64 bytes */
121 count = 64 - 1 - count;
122
123 /* Pad out to 56 mod 64 */
124 if (count < 8) {
125 /* Two lots of padding: Pad the first block to 64 bytes */
126 memset( p, 0, count );
127 toLittleEndian( tmpbuf, 16 );
128 compBlock( sumout, (uint32_t *)tmpbuf );
129
130 /* Now fill the next block with 56 bytes */
131 memset( tmpbuf, 0, 56);
132 } else {
133 /* Pad block to 56 bytes */
134 memset( p, 0, count - 8);
135 }
136 toLittleEndian( tmpbuf, 14 );
137
138 /* Append length in bits and transform */
139 ((uint32_t *) tmpbuf)[14] = uBits[0];
140 ((uint32_t *) tmpbuf)[15] = uBits[1];
141
142 compBlock( sumout, (uint32_t *)tmpbuf );
143 toLittleEndian((unsigned char *)sumout, 4);
144}
145
146#define F1(x, y, z) (z ^ (x & (y ^ z)))
147#define F2(x, y, z) F1(z, x, y)
148#define F3(x, y, z) (x ^ y ^ z)
149#define F4(x, y, z) (y ^ (x | ~z))
150
151/* This is the central step in the MD5 algorithm. */
152#define MD5STEP(f, w, x, y, z, data, s) \
153 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
154
155void Bu::Md5::compBlock( uint32_t *lsum, uint32_t *x )
156{
157 register uint32_t a, b, c, d;
158 a = lsum[0];
159 b = lsum[1];
160 c = lsum[2];
161 d = lsum[3];
162
163 MD5STEP(F1, a, b, c, d, x[0] + 0xd76aa478, 7);
164 MD5STEP(F1, d, a, b, c, x[1] + 0xe8c7b756, 12);
165 MD5STEP(F1, c, d, a, b, x[2] + 0x242070db, 17);
166 MD5STEP(F1, b, c, d, a, x[3] + 0xc1bdceee, 22);
167 MD5STEP(F1, a, b, c, d, x[4] + 0xf57c0faf, 7);
168 MD5STEP(F1, d, a, b, c, x[5] + 0x4787c62a, 12);
169 MD5STEP(F1, c, d, a, b, x[6] + 0xa8304613, 17);
170 MD5STEP(F1, b, c, d, a, x[7] + 0xfd469501, 22);
171 MD5STEP(F1, a, b, c, d, x[8] + 0x698098d8, 7);
172 MD5STEP(F1, d, a, b, c, x[9] + 0x8b44f7af, 12);
173 MD5STEP(F1, c, d, a, b, x[10] + 0xffff5bb1, 17);
174 MD5STEP(F1, b, c, d, a, x[11] + 0x895cd7be, 22);
175 MD5STEP(F1, a, b, c, d, x[12] + 0x6b901122, 7);
176 MD5STEP(F1, d, a, b, c, x[13] + 0xfd987193, 12);
177 MD5STEP(F1, c, d, a, b, x[14] + 0xa679438e, 17);
178 MD5STEP(F1, b, c, d, a, x[15] + 0x49b40821, 22);
179
180 MD5STEP(F2, a, b, c, d, x[1] + 0xf61e2562, 5);
181 MD5STEP(F2, d, a, b, c, x[6] + 0xc040b340, 9);
182 MD5STEP(F2, c, d, a, b, x[11] + 0x265e5a51, 14);
183 MD5STEP(F2, b, c, d, a, x[0] + 0xe9b6c7aa, 20);
184 MD5STEP(F2, a, b, c, d, x[5] + 0xd62f105d, 5);
185 MD5STEP(F2, d, a, b, c, x[10] + 0x02441453, 9);
186 MD5STEP(F2, c, d, a, b, x[15] + 0xd8a1e681, 14);
187 MD5STEP(F2, b, c, d, a, x[4] + 0xe7d3fbc8, 20);
188 MD5STEP(F2, a, b, c, d, x[9] + 0x21e1cde6, 5);
189 MD5STEP(F2, d, a, b, c, x[14] + 0xc33707d6, 9);
190 MD5STEP(F2, c, d, a, b, x[3] + 0xf4d50d87, 14);
191 MD5STEP(F2, b, c, d, a, x[8] + 0x455a14ed, 20);
192 MD5STEP(F2, a, b, c, d, x[13] + 0xa9e3e905, 5);
193 MD5STEP(F2, d, a, b, c, x[2] + 0xfcefa3f8, 9);
194 MD5STEP(F2, c, d, a, b, x[7] + 0x676f02d9, 14);
195 MD5STEP(F2, b, c, d, a, x[12] + 0x8d2a4c8a, 20);
196
197 MD5STEP(F3, a, b, c, d, x[5] + 0xfffa3942, 4);
198 MD5STEP(F3, d, a, b, c, x[8] + 0x8771f681, 11);
199 MD5STEP(F3, c, d, a, b, x[11] + 0x6d9d6122, 16);
200 MD5STEP(F3, b, c, d, a, x[14] + 0xfde5380c, 23);
201 MD5STEP(F3, a, b, c, d, x[1] + 0xa4beea44, 4);
202 MD5STEP(F3, d, a, b, c, x[4] + 0x4bdecfa9, 11);
203 MD5STEP(F3, c, d, a, b, x[7] + 0xf6bb4b60, 16);
204 MD5STEP(F3, b, c, d, a, x[10] + 0xbebfbc70, 23);
205 MD5STEP(F3, a, b, c, d, x[13] + 0x289b7ec6, 4);
206 MD5STEP(F3, d, a, b, c, x[0] + 0xeaa127fa, 11);
207 MD5STEP(F3, c, d, a, b, x[3] + 0xd4ef3085, 16);
208 MD5STEP(F3, b, c, d, a, x[6] + 0x04881d05, 23);
209 MD5STEP(F3, a, b, c, d, x[9] + 0xd9d4d039, 4);
210 MD5STEP(F3, d, a, b, c, x[12] + 0xe6db99e5, 11);
211 MD5STEP(F3, c, d, a, b, x[15] + 0x1fa27cf8, 16);
212 MD5STEP(F3, b, c, d, a, x[2] + 0xc4ac5665, 23);
213
214 MD5STEP(F4, a, b, c, d, x[0] + 0xf4292244, 6);
215 MD5STEP(F4, d, a, b, c, x[7] + 0x432aff97, 10);
216 MD5STEP(F4, c, d, a, b, x[14] + 0xab9423a7, 15);
217 MD5STEP(F4, b, c, d, a, x[5] + 0xfc93a039, 21);
218 MD5STEP(F4, a, b, c, d, x[12] + 0x655b59c3, 6);
219 MD5STEP(F4, d, a, b, c, x[3] + 0x8f0ccc92, 10);
220 MD5STEP(F4, c, d, a, b, x[10] + 0xffeff47d, 15);
221 MD5STEP(F4, b, c, d, a, x[1] + 0x85845dd1, 21);
222 MD5STEP(F4, a, b, c, d, x[8] + 0x6fa87e4f, 6);
223 MD5STEP(F4, d, a, b, c, x[15] + 0xfe2ce6e0, 10);
224 MD5STEP(F4, c, d, a, b, x[6] + 0xa3014314, 15);
225 MD5STEP(F4, b, c, d, a, x[13] + 0x4e0811a1, 21);
226 MD5STEP(F4, a, b, c, d, x[4] + 0xf7537e82, 6);
227 MD5STEP(F4, d, a, b, c, x[11] + 0xbd3af235, 10);
228 MD5STEP(F4, c, d, a, b, x[2] + 0x2ad7d2bb, 15);
229 MD5STEP(F4, b, c, d, a, x[9] + 0xeb86d391, 21);
230
231 lsum[0] += a;
232 lsum[1] += b;
233 lsum[2] += c;
234 lsum[3] += d;
235}
236
237void Bu::Md5::_toLittleEndian( uint8_t *buf, uint32_t count )
238{
239 uint32_t t;
240 do {
241 t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
242 ((unsigned) buf[1] << 8 | buf[0]);
243 *(uint32_t *) buf = t;
244 buf += 4;
245 } while( --count );
246}