aboutsummaryrefslogtreecommitdiff
path: root/src/md5.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/md5.cpp')
-rw-r--r--src/md5.cpp191
1 files changed, 191 insertions, 0 deletions
diff --git a/src/md5.cpp b/src/md5.cpp
new file mode 100644
index 0000000..aa965ed
--- /dev/null
+++ b/src/md5.cpp
@@ -0,0 +1,191 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include "md5.h"
5
6// This performs a wrapping bitwise shift, kinda' fun!
7
8#define bit_roll( num, cnt ) \
9 (((num) << (cnt)) | (((num) >> (32 - (cnt))) & ~(-1<<(cnt))))
10
11//#define md5_cmn( q, a, b, x, s, t ) (bit_roll((a + q + x + t), s) + b)
12
13// The following are handy wrappers for the cmn function
14#define md5_ff( a, b, c, d, x, s, t ) \
15 (md5_cmn((b & c) | ((~b) & d), a, b, x, s, t))
16
17#define md5_gg( a, b, c, d, x, s, t ) \
18 (md5_cmn((b & d) | (c & (~d)), a, b, x, s, t))
19
20#define md5_hh( a, b, c, d, x, s, t ) \
21 (md5_cmn(b ^ c ^ d, a, b, x, s, t))
22
23#define md5_ii( a, b, c, d, x, s, t ) \
24 (md5_cmn(c ^ (b | (~d)), a, b, x, s, t))
25
26inline long md5_cmn( long q, long a, long b, long x, long s, long t )
27{
28 return bit_roll((a + q + x + t), s) + b;
29}
30
31Bu::Md5::Md5()
32{
33 reset();
34}
35
36Bu::Md5::~Md5()
37{
38}
39
40void Bu::Md5::reset()
41{
42 // These are the magic seed numbers...
43
44 sum[0] = 1732584193;
45 sum[1] = -271733879;
46 sum[2] = -1732584194;
47 sum[3] = 271733878;
48
49 iBytes = 0;
50 memset( inbuf, 0, 4*16 );
51 iFill = 0;
52}
53
54void Bu::Md5::setSalt( const Bu::FString & /*sSalt*/ )
55{
56}
57
58void Bu::Md5::addData( const char *sData, int iSize )
59{
60 int iInPos = 0;
61 for(;;)
62 {
63 for( ; iFill < 16*4 && iInPos < iSize; iFill++, iInPos++ )
64 {
65 inbuf[iFill>>2] |= ((long)sData[iInPos]) << ((iFill*8)%32);
66 }
67 if( iFill < 16*4 )
68 break;
69 compBlock( inbuf, sum );
70 memset( inbuf, 0, 4*16 );
71 iFill = 0;
72 }
73 iBytes += iSize;
74}
75
76Bu::FString Bu::Md5::getResult()
77{
78 static const char hex_tab[] = {"0123456789abcdef"};
79 char str[33];
80
81 long lsum[4];
82 memcpy( lsum, sum, 4*4 );
83 long lbuf[16];
84 memcpy( lbuf, inbuf, 4*16 );
85
86 lbuf[iFill>>2] |= 0x80 << ((iFill*8)%32);
87 uint64_t iBits = iBytes*8;
88 if( iBytes > 0 && iFill>>2 >= 14 )
89 {
90 compBlock( lbuf, lsum );
91 memset( lbuf, 0, 4*16 );
92 memcpy( lbuf+14, &iBits, 8 );
93 compBlock( lbuf, lsum );
94 }
95 else
96 {
97 memcpy( lbuf+14, &iBits, 8 );
98 compBlock( lbuf, lsum );
99 }
100
101 int k = 0;
102 for( int i = 0; i < 16; i++ )
103 {
104 str[k++] = hex_tab[(lsum[i>>2] >> ((i%4)*8+4)) & 0xF];
105 str[k++] = hex_tab[(lsum[i>>2] >> ((i%4)*8 )) & 0xF];
106 }
107
108 return Bu::FString( str, 32 );
109}
110
111void Bu::Md5::compBlock( long *x, long *lsum )
112{
113 long a = lsum[0];
114 long b = lsum[1];
115 long c = lsum[2];
116 long d = lsum[3];
117
118 a = md5_ff(a, b, c, d, x[ 0], 7 , -680876936);
119 d = md5_ff(d, a, b, c, x[ 1], 12, -389564586);
120 c = md5_ff(c, d, a, b, x[ 2], 17, 606105819);
121 b = md5_ff(b, c, d, a, x[ 3], 22, -1044525330);
122 a = md5_ff(a, b, c, d, x[ 4], 7 , -176418897);
123 d = md5_ff(d, a, b, c, x[ 5], 12, 1200080426);
124 c = md5_ff(c, d, a, b, x[ 6], 17, -1473231341);
125 b = md5_ff(b, c, d, a, x[ 7], 22, -45705983);
126 a = md5_ff(a, b, c, d, x[ 8], 7 , 1770035416);
127 d = md5_ff(d, a, b, c, x[ 9], 12, -1958414417);
128 c = md5_ff(c, d, a, b, x[10], 17, -42063);
129 b = md5_ff(b, c, d, a, x[11], 22, -1990404162);
130 a = md5_ff(a, b, c, d, x[12], 7 , 1804603682);
131 d = md5_ff(d, a, b, c, x[13], 12, -40341101);
132 c = md5_ff(c, d, a, b, x[14], 17, -1502002290);
133 b = md5_ff(b, c, d, a, x[15], 22, 1236535329);
134
135 a = md5_gg(a, b, c, d, x[ 1], 5 , -165796510);
136 d = md5_gg(d, a, b, c, x[ 6], 9 , -1069501632);
137 c = md5_gg(c, d, a, b, x[11], 14, 643717713);
138 b = md5_gg(b, c, d, a, x[ 0], 20, -373897302);
139 a = md5_gg(a, b, c, d, x[ 5], 5 , -701558691);
140 d = md5_gg(d, a, b, c, x[10], 9 , 38016083);
141 c = md5_gg(c, d, a, b, x[15], 14, -660478335);
142 b = md5_gg(b, c, d, a, x[ 4], 20, -405537848);
143 a = md5_gg(a, b, c, d, x[ 9], 5 , 568446438);
144 d = md5_gg(d, a, b, c, x[14], 9 , -1019803690);
145 c = md5_gg(c, d, a, b, x[ 3], 14, -187363961);
146 b = md5_gg(b, c, d, a, x[ 8], 20, 1163531501);
147 a = md5_gg(a, b, c, d, x[13], 5 , -1444681467);
148 d = md5_gg(d, a, b, c, x[ 2], 9 , -51403784);
149 c = md5_gg(c, d, a, b, x[ 7], 14, 1735328473);
150 b = md5_gg(b, c, d, a, x[12], 20, -1926607734);
151
152 a = md5_hh(a, b, c, d, x[ 5], 4 , -378558);
153 d = md5_hh(d, a, b, c, x[ 8], 11, -2022574463);
154 c = md5_hh(c, d, a, b, x[11], 16, 1839030562);
155 b = md5_hh(b, c, d, a, x[14], 23, -35309556);
156 a = md5_hh(a, b, c, d, x[ 1], 4 , -1530992060);
157 d = md5_hh(d, a, b, c, x[ 4], 11, 1272893353);
158 c = md5_hh(c, d, a, b, x[ 7], 16, -155497632);
159 b = md5_hh(b, c, d, a, x[10], 23, -1094730640);
160 a = md5_hh(a, b, c, d, x[13], 4 , 681279174);
161 d = md5_hh(d, a, b, c, x[ 0], 11, -358537222);
162 c = md5_hh(c, d, a, b, x[ 3], 16, -722521979);
163 b = md5_hh(b, c, d, a, x[ 6], 23, 76029189);
164 a = md5_hh(a, b, c, d, x[ 9], 4 , -640364487);
165 d = md5_hh(d, a, b, c, x[12], 11, -421815835);
166 c = md5_hh(c, d, a, b, x[15], 16, 530742520);
167 b = md5_hh(b, c, d, a, x[ 2], 23, -995338651);
168
169 a = md5_ii(a, b, c, d, x[ 0], 6 , -198630844);
170 d = md5_ii(d, a, b, c, x[ 7], 10, 1126891415);
171 c = md5_ii(c, d, a, b, x[14], 15, -1416354905);
172 b = md5_ii(b, c, d, a, x[ 5], 21, -57434055);
173 a = md5_ii(a, b, c, d, x[12], 6 , 1700485571);
174 d = md5_ii(d, a, b, c, x[ 3], 10, -1894986606);
175 c = md5_ii(c, d, a, b, x[10], 15, -1051523);
176 b = md5_ii(b, c, d, a, x[ 1], 21, -2054922799);
177 a = md5_ii(a, b, c, d, x[ 8], 6 , 1873313359);
178 d = md5_ii(d, a, b, c, x[15], 10, -30611744);
179 c = md5_ii(c, d, a, b, x[ 6], 15, -1560198380);
180 b = md5_ii(b, c, d, a, x[13], 21, 1309151649);
181 a = md5_ii(a, b, c, d, x[ 4], 6 , -145523070);
182 d = md5_ii(d, a, b, c, x[11], 10, -1120210379);
183 c = md5_ii(c, d, a, b, x[ 2], 15, 718787259);
184 b = md5_ii(b, c, d, a, x[ 9], 21, -343485551);
185
186 lsum[0] = a + lsum[0];
187 lsum[1] = b + lsum[1];
188 lsum[2] = c + lsum[2];
189 lsum[3] = d + lsum[3];
190}
191