diff options
Diffstat (limited to 'src/stable/md5.cpp')
-rw-r--r-- | src/stable/md5.cpp | 218 |
1 files changed, 109 insertions, 109 deletions
diff --git a/src/stable/md5.cpp b/src/stable/md5.cpp index c1d993f..e62cbf4 100644 --- a/src/stable/md5.cpp +++ b/src/stable/md5.cpp | |||
@@ -19,7 +19,7 @@ | |||
19 | 19 | ||
20 | Bu::Md5::Md5() | 20 | Bu::Md5::Md5() |
21 | { | 21 | { |
22 | reset(); | 22 | reset(); |
23 | } | 23 | } |
24 | 24 | ||
25 | Bu::Md5::~Md5() | 25 | Bu::Md5::~Md5() |
@@ -28,15 +28,15 @@ Bu::Md5::~Md5() | |||
28 | 28 | ||
29 | void Bu::Md5::reset() | 29 | void Bu::Md5::reset() |
30 | { | 30 | { |
31 | // These are the magic seed numbers... | 31 | // These are the magic seed numbers... |
32 | 32 | ||
33 | sum[0] = 0x67452301U; | 33 | sum[0] = 0x67452301U; |
34 | sum[1] = 0xEFCDAB89U; | 34 | sum[1] = 0xEFCDAB89U; |
35 | sum[2] = 0x98BADCFEU; | 35 | sum[2] = 0x98BADCFEU; |
36 | sum[3] = 0x10325476U; | 36 | sum[3] = 0x10325476U; |
37 | 37 | ||
38 | uBits[0] = 0; | 38 | uBits[0] = 0; |
39 | uBits[1] = 0; | 39 | uBits[1] = 0; |
40 | } | 40 | } |
41 | 41 | ||
42 | void Bu::Md5::setSalt( const Bu::String & /*sSalt*/ ) | 42 | void Bu::Md5::setSalt( const Bu::String & /*sSalt*/ ) |
@@ -45,102 +45,102 @@ void Bu::Md5::setSalt( const Bu::String & /*sSalt*/ ) | |||
45 | 45 | ||
46 | void Bu::Md5::addData( const void *sVData, int iSize ) | 46 | void Bu::Md5::addData( const void *sVData, int iSize ) |
47 | { | 47 | { |
48 | const char *sData = (const char *)sVData; | 48 | const char *sData = (const char *)sVData; |
49 | uint32_t t; | 49 | uint32_t t; |
50 | 50 | ||
51 | t = uBits[0]; | 51 | t = uBits[0]; |
52 | if( (uBits[0] = t + ((uint32_t)iSize << 3)) < t ) | 52 | if( (uBits[0] = t + ((uint32_t)iSize << 3)) < t ) |
53 | uBits[1]++; | 53 | uBits[1]++; |
54 | uBits[1] += iSize >> 29; | 54 | uBits[1] += iSize >> 29; |
55 | 55 | ||
56 | t = (t >> 3) & 0x3f; /* How many bytes we have buffered */ | 56 | t = (t >> 3) & 0x3f; /* How many bytes we have buffered */ |
57 | 57 | ||
58 | /* Handle any leading odd-sized chunks */ | 58 | /* Handle any leading odd-sized chunks */ |
59 | if( t ) | 59 | if( t ) |
60 | { | 60 | { |
61 | unsigned char *p = (unsigned char *) inbuf + t; | 61 | unsigned char *p = (unsigned char *) inbuf + t; |
62 | 62 | ||
63 | t = 64 - t; | 63 | t = 64 - t; |
64 | if( (uint32_t)iSize < t ) { | 64 | if( (uint32_t)iSize < t ) { |
65 | memcpy( p, sData, iSize ); | 65 | memcpy( p, sData, iSize ); |
66 | return; | 66 | return; |
67 | } | 67 | } |
68 | memcpy( p, sData, t ); | 68 | memcpy( p, sData, t ); |
69 | toLittleEndian( inbuf, 16 ); | 69 | toLittleEndian( inbuf, 16 ); |
70 | compBlock( sum, (uint32_t *)inbuf ); | 70 | compBlock( sum, (uint32_t *)inbuf ); |
71 | sData += t; | 71 | sData += t; |
72 | iSize -= t; | 72 | iSize -= t; |
73 | } | 73 | } |
74 | 74 | ||
75 | /* Process data in 64-byte chunks */ | 75 | /* Process data in 64-byte chunks */ |
76 | while( iSize >= 64 ) | 76 | while( iSize >= 64 ) |
77 | { | 77 | { |
78 | memcpy( inbuf, sData, 64 ); | 78 | memcpy( inbuf, sData, 64 ); |
79 | toLittleEndian( inbuf, 16 ); | 79 | toLittleEndian( inbuf, 16 ); |
80 | compBlock( sum, (uint32_t *)inbuf ); | 80 | compBlock( sum, (uint32_t *)inbuf ); |
81 | sData += 64; | 81 | sData += 64; |
82 | iSize -= 64; | 82 | iSize -= 64; |
83 | } | 83 | } |
84 | 84 | ||
85 | /* Handle any remaining bytes of data. */ | 85 | /* Handle any remaining bytes of data. */ |
86 | memcpy( inbuf, sData, iSize ); | 86 | memcpy( inbuf, sData, iSize ); |
87 | } | 87 | } |
88 | 88 | ||
89 | Bu::String Bu::Md5::getResult() | 89 | Bu::String Bu::Md5::getResult() |
90 | { | 90 | { |
91 | uint32_t lsum[4]; | 91 | uint32_t lsum[4]; |
92 | compCap( lsum ); | 92 | compCap( lsum ); |
93 | return Bu::String( (const char *)lsum, 4*4 ); | 93 | return Bu::String( (const char *)lsum, 4*4 ); |
94 | } | 94 | } |
95 | 95 | ||
96 | void Bu::Md5::writeResult( Bu::Stream &sOut ) | 96 | void Bu::Md5::writeResult( Bu::Stream &sOut ) |
97 | { | 97 | { |
98 | uint32_t lsum[4]; | 98 | uint32_t lsum[4]; |
99 | compCap( lsum ); | 99 | compCap( lsum ); |
100 | sOut.write( lsum, 4*4 ); | 100 | sOut.write( lsum, 4*4 ); |
101 | } | 101 | } |
102 | 102 | ||
103 | void Bu::Md5::compCap( uint32_t *sumout ) | 103 | void Bu::Md5::compCap( uint32_t *sumout ) |
104 | { | 104 | { |
105 | uint8_t tmpbuf[64]; | 105 | uint8_t tmpbuf[64]; |
106 | memcpy( sumout, sum, 4*4 ); | 106 | memcpy( sumout, sum, 4*4 ); |
107 | memcpy( tmpbuf, inbuf, 64 ); | 107 | memcpy( tmpbuf, inbuf, 64 ); |
108 | 108 | ||
109 | uint32_t count; | 109 | uint32_t count; |
110 | uint8_t *p; | 110 | uint8_t *p; |
111 | 111 | ||
112 | /* Compute number of bytes mod 64 */ | 112 | /* Compute number of bytes mod 64 */ |
113 | count = (uBits[0] >> 3) & 0x3F; | 113 | count = (uBits[0] >> 3) & 0x3F; |
114 | 114 | ||
115 | /* Set the first char of padding to 0x80. This is safe since there is | 115 | /* Set the first char of padding to 0x80. This is safe since there is |
116 | always at least one byte free */ | 116 | always at least one byte free */ |
117 | p = tmpbuf + count; | 117 | p = tmpbuf + count; |
118 | *p++ = 0x80; | 118 | *p++ = 0x80; |
119 | 119 | ||
120 | /* Bytes of padding needed to make 64 bytes */ | 120 | /* Bytes of padding needed to make 64 bytes */ |
121 | count = 64 - 1 - count; | 121 | count = 64 - 1 - count; |
122 | 122 | ||
123 | /* Pad out to 56 mod 64 */ | 123 | /* Pad out to 56 mod 64 */ |
124 | if (count < 8) { | 124 | if (count < 8) { |
125 | /* Two lots of padding: Pad the first block to 64 bytes */ | 125 | /* Two lots of padding: Pad the first block to 64 bytes */ |
126 | memset( p, 0, count ); | 126 | memset( p, 0, count ); |
127 | toLittleEndian( tmpbuf, 16 ); | 127 | toLittleEndian( tmpbuf, 16 ); |
128 | compBlock( sumout, (uint32_t *)tmpbuf ); | 128 | compBlock( sumout, (uint32_t *)tmpbuf ); |
129 | 129 | ||
130 | /* Now fill the next block with 56 bytes */ | 130 | /* Now fill the next block with 56 bytes */ |
131 | memset( tmpbuf, 0, 56); | 131 | memset( tmpbuf, 0, 56); |
132 | } else { | 132 | } else { |
133 | /* Pad block to 56 bytes */ | 133 | /* Pad block to 56 bytes */ |
134 | memset( p, 0, count - 8); | 134 | memset( p, 0, count - 8); |
135 | } | 135 | } |
136 | toLittleEndian( tmpbuf, 14 ); | 136 | toLittleEndian( tmpbuf, 14 ); |
137 | 137 | ||
138 | /* Append length in bits and transform */ | 138 | /* Append length in bits and transform */ |
139 | ((uint32_t *) tmpbuf)[14] = uBits[0]; | 139 | ((uint32_t *) tmpbuf)[14] = uBits[0]; |
140 | ((uint32_t *) tmpbuf)[15] = uBits[1]; | 140 | ((uint32_t *) tmpbuf)[15] = uBits[1]; |
141 | 141 | ||
142 | compBlock( sumout, (uint32_t *)tmpbuf ); | 142 | compBlock( sumout, (uint32_t *)tmpbuf ); |
143 | toLittleEndian((unsigned char *)sumout, 4); | 143 | toLittleEndian((unsigned char *)sumout, 4); |
144 | } | 144 | } |
145 | 145 | ||
146 | #define F1(x, y, z) (z ^ (x & (y ^ z))) | 146 | #define F1(x, y, z) (z ^ (x & (y ^ z))) |
@@ -150,15 +150,15 @@ void Bu::Md5::compCap( uint32_t *sumout ) | |||
150 | 150 | ||
151 | /* This is the central step in the MD5 algorithm. */ | 151 | /* This is the central step in the MD5 algorithm. */ |
152 | #define MD5STEP(f, w, x, y, z, data, s) \ | 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 ) | 153 | ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) |
154 | 154 | ||
155 | void Bu::Md5::compBlock( uint32_t *lsum, uint32_t *x ) | 155 | void Bu::Md5::compBlock( uint32_t *lsum, uint32_t *x ) |
156 | { | 156 | { |
157 | register uint32_t a, b, c, d; | 157 | register uint32_t a, b, c, d; |
158 | a = lsum[0]; | 158 | a = lsum[0]; |
159 | b = lsum[1]; | 159 | b = lsum[1]; |
160 | c = lsum[2]; | 160 | c = lsum[2]; |
161 | d = lsum[3]; | 161 | d = lsum[3]; |
162 | 162 | ||
163 | MD5STEP(F1, a, b, c, d, x[0] + 0xd76aa478, 7); | 163 | MD5STEP(F1, a, b, c, d, x[0] + 0xd76aa478, 7); |
164 | MD5STEP(F1, d, a, b, c, x[1] + 0xe8c7b756, 12); | 164 | MD5STEP(F1, d, a, b, c, x[1] + 0xe8c7b756, 12); |
@@ -228,19 +228,19 @@ void Bu::Md5::compBlock( uint32_t *lsum, uint32_t *x ) | |||
228 | MD5STEP(F4, c, d, a, b, x[2] + 0x2ad7d2bb, 15); | 228 | MD5STEP(F4, c, d, a, b, x[2] + 0x2ad7d2bb, 15); |
229 | MD5STEP(F4, b, c, d, a, x[9] + 0xeb86d391, 21); | 229 | MD5STEP(F4, b, c, d, a, x[9] + 0xeb86d391, 21); |
230 | 230 | ||
231 | lsum[0] += a; | 231 | lsum[0] += a; |
232 | lsum[1] += b; | 232 | lsum[1] += b; |
233 | lsum[2] += c; | 233 | lsum[2] += c; |
234 | lsum[3] += d; | 234 | lsum[3] += d; |
235 | } | 235 | } |
236 | 236 | ||
237 | void Bu::Md5::_toLittleEndian( uint8_t *buf, uint32_t count ) | 237 | void Bu::Md5::_toLittleEndian( uint8_t *buf, uint32_t count ) |
238 | { | 238 | { |
239 | uint32_t t; | 239 | uint32_t t; |
240 | do { | 240 | do { |
241 | t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 | | 241 | t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 | |
242 | ((unsigned) buf[1] << 8 | buf[0]); | 242 | ((unsigned) buf[1] << 8 | buf[0]); |
243 | *(uint32_t *) buf = t; | 243 | *(uint32_t *) buf = t; |
244 | buf += 4; | 244 | buf += 4; |
245 | } while( --count ); | 245 | } while( --count ); |
246 | } | 246 | } |