MCPcopy Create free account
hub / github.com/ElementsProject/elements / Strengthen

Function Strengthen

src/random.cpp:224–249  ·  view source on GitHub ↗

Use repeated SHA512 to strengthen the randomness in seed32, and feed into hasher. */

Source from the content-addressed store, hash-verified

222
223/** Use repeated SHA512 to strengthen the randomness in seed32, and feed into hasher. */
224static void Strengthen(const unsigned char (&seed)[32], int microseconds, CSHA512& hasher) noexcept
225{
226 CSHA512 inner_hasher;
227 inner_hasher.Write(seed, sizeof(seed));
228
229 // Hash loop
230 unsigned char buffer[64];
231 int64_t stop = GetTimeMicros() + microseconds;
232 do {
233 for (int i = 0; i < 1000; ++i) {
234 inner_hasher.Finalize(buffer);
235 inner_hasher.Reset();
236 inner_hasher.Write(buffer, sizeof(buffer));
237 }
238 // Benchmark operation and feed it into outer hasher.
239 int64_t perf = GetPerformanceCounter();
240 hasher.Write((const unsigned char*)&perf, sizeof(perf));
241 } while (GetTimeMicros() < stop);
242
243 // Produce output from inner state and feed it to outer hasher.
244 inner_hasher.Finalize(buffer);
245 hasher.Write(buffer, sizeof(buffer));
246 // Try to clean up.
247 inner_hasher.Reset();
248 memory_cleanse(buffer, sizeof(buffer));
249}
250
251#ifndef WIN32
252/** Fallback: get 32 bytes of system entropy from /dev/urandom. The most

Callers 1

SeedStrengthenFunction · 0.85

Calls 6

GetTimeMicrosFunction · 0.85
GetPerformanceCounterFunction · 0.85
memory_cleanseFunction · 0.85
WriteMethod · 0.45
FinalizeMethod · 0.45
ResetMethod · 0.45

Tested by

no test coverage detected