I’m in need of a fairly speedy SHA-256 file hasher. JUCE’s implementation is rather naive, reading from a file 64 bytes at a time - which is totally reasonable for small data sets.
I’m working with a variety of large files so I had a serious crack at improving this situation.
Here are some benchmarks when testing with a 1 Gigabyte file, in a 64-bit release build on Windows 10, VS2019 (Laptop specs: Intel i7 6700HQ, 24GB RAM, NVMe drives)
JUCE
1 min 8 secs
1 min 11 secs
1 min 14 secs
1 min 10 secs
1 min 8 secs
My Changes
11 secs
10 secs
9 secs
9 secs
9 secs
Here’s a test site to compare the resulting SHA of a file:
If you don’t have any known large data files handy to test with, you can go here to generate a file of whatever size, filled with random bytes: Online Random file generator
The changes are here. I left JUCE’s code intact if you want to toggle between my changes and the original code.
I’d love for people to give it a run and see if it’s reasonable/totally shit or whatever.
juce_SHA256.cpp (10.8 KB)
Some test code:
void testSHA256 (const juce::File& f)
{
const juce::File logFile ("YourLogFileLocation");
if (! logFile.existsAsFile())
{
logFile.deleteRecursively (true);
logFile.create();
}
juce::FileOutputStream fos (logFile);
jassert (fos.openedOk());
jassert (f.existsAsFile());
juce::FileInputStream fis (f);
jassert (fis.openedOk());
const auto ct = juce::Time::getCurrentTime();
juce::SHA256 hash (fis);
fos << hash.toHexString() << juce::newLine;
const auto et = juce::Time::getCurrentTime();
fos << (et - ct).getDescription() << juce::newLine;
}