49 static const unsigned char fillbuf[64] = { 0x80, 0 };
57 static inline uint32_t swapBytes(uint32_t
n)
59 #ifdef WM_LITTLE_ENDIAN
69 static inline void set_uint32(
unsigned char *dst, uint32_t v)
71 std::memcpy(dst, &v,
sizeof(uint32_t));
78 void Foam::SHA1::processBytes(
const void *data,
size_t len)
89 const size_t remaining = bufLen_;
92 sizeof(buffer_) - remaining > len
94 :
sizeof(buffer_) - remaining
97 unsigned char* bufp =
reinterpret_cast<unsigned char*
>(buffer_);
99 std::memcpy(&bufp[remaining], data,
add);
104 processBlock(buffer_, bufLen_ & ~63);
109 std::memcpy(buffer_, &bufp[(remaining +
add) & ~63], bufLen_);
112 data =
reinterpret_cast<const unsigned char*
>(data) +
add;
119 processBlock(std::memcpy(buffer_, data, 64), 64);
120 data =
reinterpret_cast<const unsigned char*
>(data) + 64;
127 unsigned char* bufp =
reinterpret_cast<unsigned char*
>(buffer_);
128 size_t remaining = bufLen_;
130 std::memcpy(&bufp[remaining], data, len);
134 processBlock(buffer_, 64);
136 std::memcpy(buffer_, &buffer_[16], remaining);
144 #define K1 0x5a827999
145 #define K2 0x6ed9eba1
146 #define K3 0x8f1bbcdc
147 #define K4 0xca62c1d6
150 #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
151 #define F2(B,C,D) (B ^ C ^ D)
152 #define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
153 #define F4(B,C,D) (B ^ C ^ D)
158 void Foam::SHA1::processBlock(
const void *data,
size_t len)
160 const uint32_t *words =
reinterpret_cast<const uint32_t*
>(data);
161 const size_t nwords = len /
sizeof(uint32_t);
162 const uint32_t *endp = words + nwords;
166 uint32_t a = hashsumA_;
167 uint32_t
b = hashsumB_;
168 uint32_t
c = hashsumC_;
169 uint32_t d = hashsumD_;
170 uint32_t
e = hashsumE_;
176 if (bufTotal_[0] < len)
182 #define rol_uint32(x, nbits) (((x) << (nbits)) | ((x) >> (32 - (nbits))))
184 #define M(I) ( tm = x[I & 0x0F] ^ x[(I-14) & 0x0F] \
185 ^ x[(I-8) & 0x0F] ^ x[(I-3) & 0x0F] \
186 , (x[I & 0x0F] = rol_uint32(tm, 1)) )
188 #define R(A,B,C,D,E,F,K,M) \
191 E += rol_uint32(A, 5) + F(B, C, D) + K + M; \
192 B = rol_uint32(B, 30); \
198 for (
int t = 0; t < 16; ++t)
200 x[t] = swapBytes(*words);
294 void Foam::SHA1::calcDigest(SHA1Digest& dig)
const
296 if (bufTotal_[0] || bufTotal_[1])
298 unsigned char *r = dig.data();
300 set_uint32(r + 0 *
sizeof(uint32_t), swapBytes(hashsumA_));
301 set_uint32(r + 1 *
sizeof(uint32_t), swapBytes(hashsumB_));
302 set_uint32(r + 2 *
sizeof(uint32_t), swapBytes(hashsumC_));
303 set_uint32(r + 3 *
sizeof(uint32_t), swapBytes(hashsumD_));
304 set_uint32(r + 4 *
sizeof(uint32_t), swapBytes(hashsumE_));
317 hashsumA_ = 0x67452301;
318 hashsumB_ = 0xefcdab89;
319 hashsumC_ = 0x98badcfe;
320 hashsumD_ = 0x10325476;
321 hashsumE_ = 0xc3d2e1f0;
323 bufTotal_[0] = bufTotal_[1] = 0;
330 bool Foam::SHA1::finalize()
337 const uint32_t bytes = bufLen_;
338 const size_t size = (bytes < 56 ? 64 : 128) /
sizeof(uint32_t);
341 bufTotal_[0] += bytes;
342 if (bufTotal_[0] < bytes)
348 if (!bufTotal_[0] && !bufTotal_[1])
354 buffer_[size-2] = swapBytes((bufTotal_[1] << 3) | (bufTotal_[0] >> 29));
355 buffer_[size-1] = swapBytes(bufTotal_[0] << 3);
357 unsigned char* bufp =
reinterpret_cast<unsigned char *
>(buffer_);
359 std::memcpy(&bufp[bytes], fillbuf, (size-2) *
sizeof(uint32_t) - bytes);
362 processBlock(buffer_, size *
sizeof(uint32_t));