MCPcopy Create free account
hub / github.com/ChaiScript/ChaiScript / getHash

Method getHash

src/sha3.cpp:235–271  ·  view source on GitHub ↗

return latest hash as 16 hex characters

Source from the content-addressed store, hash-verified

233
234/// return latest hash as 16 hex characters
235std::string SHA3::getHash()
236{
237 // process remaining bytes
238 processBuffer();
239
240 // convert hash to string
241 static const char dec2hex[16 + 1] = "0123456789abcdef";
242
243 // number of significant elements in hash (uint64_t)
244 unsigned int hashLength = m_bits / 64;
245
246 std::string result;
247 result.reserve(m_bits / 4);
248 for (unsigned int i = 0; i < hashLength; i++)
249 for (unsigned int j = 0; j < 8; j++) // 64 bits => 8 bytes
250 {
251 // convert a byte to hex
252 unsigned char oneByte = (unsigned char) (m_hash[i] >> (8 * j));
253 result += dec2hex[oneByte >> 4];
254 result += dec2hex[oneByte & 15];
255 }
256
257 // SHA3-224's last entry in m_hash provides only 32 bits instead of 64 bits
258 unsigned int remainder = m_bits - hashLength * 64;
259 unsigned int processed = 0;
260 while (processed < remainder)
261 {
262 // convert a byte to hex
263 unsigned char oneByte = (unsigned char) (m_hash[hashLength] >> processed);
264 result += dec2hex[oneByte >> 4];
265 result += dec2hex[oneByte & 15];
266
267 processed += 8;
268 }
269
270 return result;
271}
272
273
274/// compute SHA3 of a memory block

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected