22 #include "runtime.hpp"
31 HASHwithTransform::HASHwithTransform(word32 digSz, word32 buffSz)
36 void HASHwithTransform::AddLength(word32 len)
38 HashLengthType tmp = loLen_;
39 if ( (loLen_ += len) < tmp)
41 hiLen_ += SafeRightShift<8*sizeof(HashLengthType)>(len);
46 void HASHwithTransform::Update(
const byte* data, word32 len)
49 word32 blockSz = getBlockSize();
50 byte* local =
reinterpret_cast<byte*
>(buffer_);
53 word32 add = min(len, blockSz - buffLen_);
54 memcpy(&local[buffLen_], data, add);
60 if (buffLen_ == blockSz) {
61 ByteReverseIf(local, local, blockSz, getByteOrder());
71 void HASHwithTransform::Final(byte* hash)
73 word32 blockSz = getBlockSize();
74 word32 digestSz = getDigestSize();
75 word32 padSz = getPadSize();
76 ByteOrder order = getByteOrder();
79 HashLengthType preLoLen = GetBitCountLo();
80 HashLengthType preHiLen = GetBitCountHi();
81 byte* local =
reinterpret_cast<byte*
>(buffer_);
83 local[buffLen_++] = 0x80;
86 if (buffLen_ > padSz) {
87 memset(&local[buffLen_], 0, blockSz - buffLen_);
88 buffLen_ += blockSz - buffLen_;
90 ByteReverseIf(local, local, blockSz, order);
94 memset(&local[buffLen_], 0, padSz - buffLen_);
96 ByteReverseIf(local, local, blockSz, order);
98 memcpy(&local[padSz], order ? &preHiLen : &preLoLen,
sizeof(preLoLen));
99 memcpy(&local[padSz+4], order ? &preLoLen : &preHiLen,
sizeof(preLoLen));
102 ByteReverseIf(digest_, digest_, digestSz, order);
103 memcpy(hash, digest_, digestSz);
109 #ifdef WORD64_AVAILABLE
111 HASH64withTransform::HASH64withTransform(word32 digSz, word32 buffSz)
116 void HASH64withTransform::AddLength(word32 len)
118 HashLengthType tmp = loLen_;
119 if ( (loLen_ += len) < tmp)
121 hiLen_ += SafeRightShift<8*sizeof(HashLengthType)>(len);
126 void HASH64withTransform::Update(
const byte* data, word32 len)
129 word32 blockSz = getBlockSize();
130 byte* local =
reinterpret_cast<byte*
>(buffer_);
133 word32 add = min(len, blockSz - buffLen_);
134 memcpy(&local[buffLen_], data, add);
140 if (buffLen_ == blockSz) {
141 ByteReverseIf(buffer_, buffer_, blockSz, getByteOrder());
151 void HASH64withTransform::Final(byte* hash)
153 word32 blockSz = getBlockSize();
154 word32 digestSz = getDigestSize();
155 word32 padSz = getPadSize();
156 ByteOrder order = getByteOrder();
159 HashLengthType preLoLen = GetBitCountLo();
160 HashLengthType preHiLen = GetBitCountHi();
161 byte* local =
reinterpret_cast<byte*
>(buffer_);
163 local[buffLen_++] = 0x80;
166 if (buffLen_ > padSz) {
167 memset(&local[buffLen_], 0, blockSz - buffLen_);
168 buffLen_ += blockSz - buffLen_;
170 ByteReverseIf(buffer_, buffer_, blockSz, order);
174 memset(&local[buffLen_], 0, padSz - buffLen_);
176 ByteReverseIf(buffer_, buffer_, padSz, order);
178 buffer_[blockSz /
sizeof(word64) - 2] = order ? preHiLen : preLoLen;
179 buffer_[blockSz /
sizeof(word64) - 1] = order ? preLoLen : preHiLen;
182 ByteReverseIf(digest_, digest_, digestSz, order);
183 memcpy(hash, digest_, digestSz);
188 #endif // WORD64_AVAILABLE