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) 66 | (((n) & 0xff00) << 8)
67 | (((n) >> 8) & 0xff00)
72 const short x = 0x0100;
75 if (*(reinterpret_cast<const char*>(&x)))
84 | (((n) & 0xff00) << 8)
85 | (((n) >> 8) & 0xff00)
93 inline void Foam::SHA1::set_uint32(
unsigned char *cp, uint32_t v)
95 memcpy(cp, &v,
sizeof(uint32_t));
101 void Foam::SHA1::processBytes(
const void *data,
size_t len)
112 size_t remaining = bufLen_;
115 sizeof(buffer_) - remaining > len
117 :
sizeof(buffer_) - remaining
120 unsigned char* bufp =
reinterpret_cast<unsigned char*
>(buffer_);
122 memcpy(&bufp[remaining], data, add);
127 processBlock(buffer_, bufLen_ & ~63);
132 memcpy(buffer_, &bufp[(remaining + add) & ~63], bufLen_);
135 data =
reinterpret_cast<const unsigned char*
>(data) + add;
142 processBlock(memcpy(buffer_, data, 64), 64);
143 data =
reinterpret_cast<const unsigned char*
>(data) + 64;
150 unsigned char* bufp =
reinterpret_cast<unsigned char*
>(buffer_);
151 size_t remaining = bufLen_;
153 memcpy (&bufp[remaining], data, len);
157 processBlock(buffer_, 64);
159 memcpy(buffer_, &buffer_[16], remaining);
167 #define K1 0x5a827999 168 #define K2 0x6ed9eba1 169 #define K3 0x8f1bbcdc 170 #define K4 0xca62c1d6 173 #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) ) 174 #define F2(B,C,D) (B ^ C ^ D) 175 #define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) ) 176 #define F4(B,C,D) (B ^ C ^ D) 181 void Foam::SHA1::processBlock(
const void *data,
size_t len)
183 const uint32_t *words =
reinterpret_cast<const uint32_t*
>(data);
184 size_t nwords = len /
sizeof(uint32_t);
185 const uint32_t *endp = words + nwords;
189 uint32_t a = hashsumA_;
190 uint32_t
b = hashsumB_;
191 uint32_t
c = hashsumC_;
192 uint32_t d = hashsumD_;
193 uint32_t
e = hashsumE_;
199 if (bufTotal_[0] < len)
205 #define rol_uint32(x, nbits) (((x) << (nbits)) | ((x) >> (32 - (nbits)))) 207 #define M(I) ( tm = x[I & 0x0F] ^ x[(I-14) & 0x0F] \ 208 ^ x[(I-8) & 0x0F] ^ x[(I-3) & 0x0F] \ 209 , (x[I & 0x0F] = rol_uint32(tm, 1)) ) 211 #define R(A,B,C,D,E,F,K,M) \ 214 E += rol_uint32(A, 5) + F(B, C, D) + K + M; \ 215 B = rol_uint32(B, 30); \ 221 for (
int t = 0; t < 16; ++t)
223 x[t] = swapBytes(*words);
227 R( a, b, c, d, e,
F1,
K1, x[ 0] );
228 R( e, a, b, c, d,
F1,
K1, x[ 1] );
229 R( d, e, a, b, c,
F1,
K1, x[ 2] );
230 R( c, d, e, a, b,
F1,
K1, x[ 3] );
231 R( b, c, d, e, a,
F1,
K1, x[ 4] );
232 R( a, b, c, d, e,
F1,
K1, x[ 5] );
233 R( e, a, b, c, d,
F1,
K1, x[ 6] );
234 R( d, e, a, b, c,
F1,
K1, x[ 7] );
235 R( c, d, e, a, b,
F1,
K1, x[ 8] );
236 R( b, c, d, e, a,
F1,
K1, x[ 9] );
237 R( a, b, c, d, e,
F1,
K1, x[10] );
238 R( e, a, b, c, d,
F1,
K1, x[11] );
239 R( d, e, a, b, c,
F1,
K1, x[12] );
240 R( c, d, e, a, b,
F1,
K1, x[13] );
241 R( b, c, d, e, a,
F1,
K1, x[14] );
242 R( a, b, c, d, e,
F1,
K1, x[15] );
243 R( e, a, b, c, d,
F1,
K1,
M(16) );
244 R( d, e, a, b, c,
F1,
K1,
M(17) );
245 R( c, d, e, a, b,
F1,
K1,
M(18) );
246 R( b, c, d, e, a,
F1,
K1,
M(19) );
247 R( a, b, c, d, e,
F2,
K2,
M(20) );
248 R( e, a, b, c, d,
F2,
K2,
M(21) );
249 R( d, e, a, b, c,
F2,
K2,
M(22) );
250 R( c, d, e, a, b,
F2,
K2,
M(23) );
251 R( b, c, d, e, a,
F2,
K2,
M(24) );
252 R( a, b, c, d, e,
F2,
K2,
M(25) );
253 R( e, a, b, c, d,
F2,
K2,
M(26) );
254 R( d, e, a, b, c,
F2,
K2,
M(27) );
255 R( c, d, e, a, b,
F2,
K2,
M(28) );
256 R( b, c, d, e, a,
F2,
K2,
M(29) );
257 R( a, b, c, d, e,
F2,
K2,
M(30) );
258 R( e, a, b, c, d,
F2,
K2,
M(31) );
259 R( d, e, a, b, c,
F2,
K2,
M(32) );
260 R( c, d, e, a, b,
F2,
K2,
M(33) );
261 R( b, c, d, e, a,
F2,
K2,
M(34) );
262 R( a, b, c, d, e,
F2,
K2,
M(35) );
263 R( e, a, b, c, d,
F2,
K2,
M(36) );
264 R( d, e, a, b, c,
F2,
K2,
M(37) );
265 R( c, d, e, a, b,
F2,
K2,
M(38) );
266 R( b, c, d, e, a,
F2,
K2,
M(39) );
267 R( a, b, c, d, e,
F3,
K3,
M(40) );
268 R( e, a, b, c, d,
F3,
K3,
M(41) );
269 R( d, e, a, b, c,
F3,
K3,
M(42) );
270 R( c, d, e, a, b,
F3,
K3,
M(43) );
271 R( b, c, d, e, a,
F3,
K3,
M(44) );
272 R( a, b, c, d, e,
F3,
K3,
M(45) );
273 R( e, a, b, c, d,
F3,
K3,
M(46) );
274 R( d, e, a, b, c,
F3,
K3,
M(47) );
275 R( c, d, e, a, b,
F3,
K3,
M(48) );
276 R( b, c, d, e, a,
F3,
K3,
M(49) );
277 R( a, b, c, d, e,
F3,
K3,
M(50) );
278 R( e, a, b, c, d,
F3,
K3,
M(51) );
279 R( d, e, a, b, c,
F3,
K3,
M(52) );
280 R( c, d, e, a, b,
F3,
K3,
M(53) );
281 R( b, c, d, e, a,
F3,
K3,
M(54) );
282 R( a, b, c, d, e,
F3,
K3,
M(55) );
283 R( e, a, b, c, d,
F3,
K3,
M(56) );
284 R( d, e, a, b, c,
F3,
K3,
M(57) );
285 R( c, d, e, a, b,
F3,
K3,
M(58) );
286 R( b, c, d, e, a,
F3,
K3,
M(59) );
287 R( a, b, c, d, e,
F4,
K4,
M(60) );
288 R( e, a, b, c, d,
F4,
K4,
M(61) );
289 R( d, e, a, b, c,
F4,
K4,
M(62) );
290 R( c, d, e, a, b,
F4,
K4,
M(63) );
291 R( b, c, d, e, a,
F4,
K4,
M(64) );
292 R( a, b, c, d, e,
F4,
K4,
M(65) );
293 R( e, a, b, c, d,
F4,
K4,
M(66) );
294 R( d, e, a, b, c,
F4,
K4,
M(67) );
295 R( c, d, e, a, b,
F4,
K4,
M(68) );
296 R( b, c, d, e, a,
F4,
K4,
M(69) );
297 R( a, b, c, d, e,
F4,
K4,
M(70) );
298 R( e, a, b, c, d,
F4,
K4,
M(71) );
299 R( d, e, a, b, c,
F4,
K4,
M(72) );
300 R( c, d, e, a, b,
F4,
K4,
M(73) );
301 R( b, c, d, e, a,
F4,
K4,
M(74) );
302 R( a, b, c, d, e,
F4,
K4,
M(75) );
303 R( e, a, b, c, d,
F4,
K4,
M(76) );
304 R( d, e, a, b, c,
F4,
K4,
M(77) );
305 R( c, d, e, a, b,
F4,
K4,
M(78) );
306 R( b, c, d, e, a,
F4,
K4,
M(79) );
317 void Foam::SHA1::calcDigest(SHA1Digest& dig)
const 319 if (bufTotal_[0] || bufTotal_[1])
321 unsigned char *r = dig.v_;
323 set_uint32(r + 0 *
sizeof(uint32_t), swapBytes(hashsumA_));
324 set_uint32(r + 1 *
sizeof(uint32_t), swapBytes(hashsumB_));
325 set_uint32(r + 2 *
sizeof(uint32_t), swapBytes(hashsumC_));
326 set_uint32(r + 3 *
sizeof(uint32_t), swapBytes(hashsumD_));
327 set_uint32(r + 4 *
sizeof(uint32_t), swapBytes(hashsumE_));
341 hashsumA_ = 0x67452301;
342 hashsumB_ = 0xefcdab89;
343 hashsumC_ = 0x98badcfe;
344 hashsumD_ = 0x10325476;
345 hashsumE_ = 0xc3d2e1f0;
347 bufTotal_[0] = bufTotal_[1] = 0;
361 uint32_t bytes = bufLen_;
362 size_t size = (bytes < 56 ? 64 : 128) /
sizeof(uint32_t);
365 bufTotal_[0] += bytes;
366 if (bufTotal_[0] < bytes)
372 if (!bufTotal_[0] && !bufTotal_[1])
378 buffer_[size-2] = swapBytes((bufTotal_[1] << 3) | (bufTotal_[0] >> 29));
379 buffer_[size-1] = swapBytes(bufTotal_[0] << 3);
381 unsigned char* bufp =
reinterpret_cast<unsigned char *
>(buffer_);
383 memcpy(&bufp[bytes], fillbuf, (size-2) *
sizeof(uint32_t) - bytes);
386 processBlock(buffer_, size *
sizeof(uint32_t));
const dimensionedScalar & c
Speed of light in a vacuum.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
void clear()
Reset the hashed data before appending more.
const dimensionedScalar & b
Wien displacement law constant: default SI units: [m K].
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
#define R(A, B, C, D, E, F, K, M)
const doubleScalar e
Elementary charge.
Functions to compute SHA1 message digest according to the NIST specification FIPS-180-1.
SHA1Digest digest() const
Calculate current digest from appended data.
bool finalize()
Finalized the calculations (normally not needed directly).