41 #if defined (__GLIBC__)
51 static const unsigned char fillbuf[64] = { 0x80, 0 };
57 inline uint32_t Foam::SHA1::swapBytes(uint32_t n)
60 # if (__BYTE_ORDER == __BIG_ENDIAN)
63 return (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24));
68 const short x = 0x0100;
71 if (*(reinterpret_cast<const char *>(&x)))
77 return (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24));
84 Foam::SHA1::set_uint32(
unsigned char *
cp, uint32_t v)
86 memcpy(cp, &v,
sizeof(uint32_t));
93 void Foam::SHA1::processBytes(
const void *data,
size_t len)
104 size_t remaining = bufLen_;
107 sizeof(buffer_) - remaining > len
109 :
sizeof(buffer_) - remaining
112 unsigned char* bufp =
reinterpret_cast<unsigned char*
>(buffer_);
114 memcpy(&bufp[remaining], data, add);
119 processBlock(buffer_, bufLen_ & ~63);
123 memcpy(buffer_, &bufp[(remaining + add) & ~63], bufLen_);
126 data =
reinterpret_cast<const unsigned char*
>(data) + add;
140 processBlock(memcpy (buffer_, data, 64), 64);
141 data =
reinterpret_cast<const unsigned char*
>(data) + 64;
157 unsigned char* bufp =
reinterpret_cast<unsigned char*
>(buffer_);
158 size_t remaining = bufLen_;
160 memcpy (&bufp[remaining], data, len);
164 processBlock(buffer_, 64);
166 memcpy (buffer_, &buffer_[16], remaining);
174 #define K1 0x5a827999
175 #define K2 0x6ed9eba1
176 #define K3 0x8f1bbcdc
177 #define K4 0xca62c1d6
180 #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
181 #define F2(B,C,D) (B ^ C ^ D)
182 #define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
183 #define F4(B,C,D) (B ^ C ^ D)
189 Foam::SHA1::processBlock(
const void *data,
size_t len)
191 const uint32_t *words =
reinterpret_cast<const uint32_t*
>(data);
192 size_t nwords = len /
sizeof(uint32_t);
193 const uint32_t *endp = words + nwords;
197 uint32_t a = hashsumA_;
198 uint32_t
b = hashsumB_;
199 uint32_t c = hashsumC_;
200 uint32_t
d = hashsumD_;
201 uint32_t
e = hashsumE_;
207 if (bufTotal_[0] < len)
213 #define rol_uint32(x, nbits) (((x) << (nbits)) | ((x) >> (32 - (nbits))))
215 #define M(I) ( tm = x[I & 0x0F] ^ x[(I-14) & 0x0F] \
216 ^ x[(I-8) & 0x0F] ^ x[(I-3) & 0x0F] \
217 , (x[I & 0x0F] = rol_uint32(tm, 1)) )
220 #define R(A,B,C,D,E,F,K,M) \
223 E += rol_uint32(A, 5) + F(B, C, D) + K + M; \
224 B = rol_uint32(B, 30); \
230 for (
int t = 0; t < 16; t++)
232 x[t] = swapBytes (*words);
236 R( a, b, c, d, e,
F1,
K1, x[ 0] );
237 R( e, a, b, c, d,
F1,
K1, x[ 1] );
238 R( d, e, a, b, c,
F1,
K1, x[ 2] );
239 R( c, d, e, a, b,
F1,
K1, x[ 3] );
240 R( b, c, d, e, a,
F1,
K1, x[ 4] );
241 R( a, b, c, d, e,
F1,
K1, x[ 5] );
242 R( e, a, b, c, d,
F1,
K1, x[ 6] );
243 R( d, e, a, b, c,
F1,
K1, x[ 7] );
244 R( c, d, e, a, b,
F1,
K1, x[ 8] );
245 R( b, c, d, e, a,
F1,
K1, x[ 9] );
246 R( a, b, c, d, e,
F1,
K1, x[10] );
247 R( e, a, b, c, d,
F1,
K1, x[11] );
248 R( d, e, a, b, c,
F1,
K1, x[12] );
249 R( c, d, e, a, b,
F1,
K1, x[13] );
250 R( b, c, d, e, a,
F1,
K1, x[14] );
251 R( a, b, c, d, e,
F1,
K1, x[15] );
252 R( e, a, b, c, d,
F1,
K1,
M(16) );
253 R( d, e, a, b, c,
F1,
K1,
M(17) );
254 R( c, d, e, a, b,
F1,
K1,
M(18) );
255 R( b, c, d, e, a,
F1,
K1,
M(19) );
256 R( a, b, c, d, e,
F2,
K2,
M(20) );
257 R( e, a, b, c, d,
F2,
K2,
M(21) );
258 R( d, e, a, b, c,
F2,
K2,
M(22) );
259 R( c, d, e, a, b,
F2,
K2,
M(23) );
260 R( b, c, d, e, a,
F2,
K2,
M(24) );
261 R( a, b, c, d, e,
F2,
K2,
M(25) );
262 R( e, a, b, c, d,
F2,
K2,
M(26) );
263 R( d, e, a, b, c,
F2,
K2,
M(27) );
264 R( c, d, e, a, b,
F2,
K2,
M(28) );
265 R( b, c, d, e, a,
F2,
K2,
M(29) );
266 R( a, b, c, d, e,
F2,
K2,
M(30) );
267 R( e, a, b, c, d,
F2,
K2,
M(31) );
268 R( d, e, a, b, c,
F2,
K2,
M(32) );
269 R( c, d, e, a, b,
F2,
K2,
M(33) );
270 R( b, c, d, e, a,
F2,
K2,
M(34) );
271 R( a, b, c, d, e,
F2,
K2,
M(35) );
272 R( e, a, b, c, d,
F2,
K2,
M(36) );
273 R( d, e, a, b, c,
F2,
K2,
M(37) );
274 R( c, d, e, a, b,
F2,
K2,
M(38) );
275 R( b, c, d, e, a,
F2,
K2,
M(39) );
276 R( a, b, c, d, e,
F3,
K3,
M(40) );
277 R( e, a, b, c, d,
F3,
K3,
M(41) );
278 R( d, e, a, b, c,
F3,
K3,
M(42) );
279 R( c, d, e, a, b,
F3,
K3,
M(43) );
280 R( b, c, d, e, a,
F3,
K3,
M(44) );
281 R( a, b, c, d, e,
F3,
K3,
M(45) );
282 R( e, a, b, c, d,
F3,
K3,
M(46) );
283 R( d, e, a, b, c,
F3,
K3,
M(47) );
284 R( c, d, e, a, b,
F3,
K3,
M(48) );
285 R( b, c, d, e, a,
F3,
K3,
M(49) );
286 R( a, b, c, d, e,
F3,
K3,
M(50) );
287 R( e, a, b, c, d,
F3,
K3,
M(51) );
288 R( d, e, a, b, c,
F3,
K3,
M(52) );
289 R( c, d, e, a, b,
F3,
K3,
M(53) );
290 R( b, c, d, e, a,
F3,
K3,
M(54) );
291 R( a, b, c, d, e,
F3,
K3,
M(55) );
292 R( e, a, b, c, d,
F3,
K3,
M(56) );
293 R( d, e, a, b, c,
F3,
K3,
M(57) );
294 R( c, d, e, a, b,
F3,
K3,
M(58) );
295 R( b, c, d, e, a,
F3,
K3,
M(59) );
296 R( a, b, c, d, e,
F4,
K4,
M(60) );
297 R( e, a, b, c, d,
F4,
K4,
M(61) );
298 R( d, e, a, b, c,
F4,
K4,
M(62) );
299 R( c, d, e, a, b,
F4,
K4,
M(63) );
300 R( b, c, d, e, a,
F4,
K4,
M(64) );
301 R( a, b, c, d, e,
F4,
K4,
M(65) );
302 R( e, a, b, c, d,
F4,
K4,
M(66) );
303 R( d, e, a, b, c,
F4,
K4,
M(67) );
304 R( c, d, e, a, b,
F4,
K4,
M(68) );
305 R( b, c, d, e, a,
F4,
K4,
M(69) );
306 R( a, b, c, d, e,
F4,
K4,
M(70) );
307 R( e, a, b, c, d,
F4,
K4,
M(71) );
308 R( d, e, a, b, c,
F4,
K4,
M(72) );
309 R( c, d, e, a, b,
F4,
K4,
M(73) );
310 R( b, c, d, e, a,
F4,
K4,
M(74) );
311 R( a, b, c, d, e,
F4,
K4,
M(75) );
312 R( e, a, b, c, d,
F4,
K4,
M(76) );
313 R( d, e, a, b, c,
F4,
K4,
M(77) );
314 R( c, d, e, a, b,
F4,
K4,
M(78) );
315 R( b, c, d, e, a,
F4,
K4,
M(79) );
326 void Foam::SHA1::calcDigest(SHA1Digest& dig)
const
328 if (bufTotal_[0] || bufTotal_[1])
330 unsigned char *r = dig.v_;
332 set_uint32 (r + 0 *
sizeof(uint32_t), swapBytes(hashsumA_));
333 set_uint32 (r + 1 *
sizeof(uint32_t), swapBytes(hashsumB_));
334 set_uint32 (r + 2 *
sizeof(uint32_t), swapBytes(hashsumC_));
335 set_uint32 (r + 3 *
sizeof(uint32_t), swapBytes(hashsumD_));
336 set_uint32 (r + 4 *
sizeof(uint32_t), swapBytes(hashsumE_));
361 hashsumA_ = 0x67452301;
362 hashsumB_ = 0xefcdab89;
363 hashsumC_ = 0x98badcfe;
364 hashsumD_ = 0x10325476;
365 hashsumE_ = 0xc3d2e1f0;
367 bufTotal_[0] = bufTotal_[1] = 0;
381 uint32_t bytes = bufLen_;
382 size_t size = (bytes < 56 ? 64 : 128) /
sizeof(uint32_t);
385 bufTotal_[0] += bytes;
386 if (bufTotal_[0] < bytes)
392 if (!bufTotal_[0] && !bufTotal_[1])
398 buffer_[size-2] = swapBytes((bufTotal_[1] << 3) | (bufTotal_[0] >> 29));
399 buffer_[size-1] = swapBytes(bufTotal_[0] << 3);
401 unsigned char* bufp =
reinterpret_cast<unsigned char *
>(buffer_);
403 memcpy(&bufp[bytes], fillbuf, (size-2) *
sizeof(uint32_t) - bytes);
406 processBlock(buffer_, size *
sizeof(uint32_t));