33 for (
size_t i = 0; i < out.size(); i++)
34 out[i] = (
char)std::tolower((
unsigned char)out[i]);
41static bool CheckHex(
const char* algo,
const char* input,
42 const std::string& got,
const char* expected) {
45 algo, input, expected, got.c_str());
60 "d41d8cd98f00b204e9800998ecf8427e"))
return false;
61 if (!
CheckHex(
"MD5",
"abc", md5(
"abc"),
62 "900150983cd24fb0d6963f7d28e17f72"))
return false;
63 if (!
CheckHex(
"MD5",
"message digest", md5(
"message digest"),
64 "f96b697d7cb7938d525a2f31aaf161d0"))
return false;
71 "900150983cd24fb0d6963f7d28e17f72"))
return false;
79 "da39a3ee5e6b4b0d3255bfef95601890afd80709"))
return false;
80 if (!
CheckHex(
"SHA1",
"abc", sha1(
"abc"),
81 "a9993e364706816aba3e25717850c26c9cd0d89d"))
return false;
83 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
84 sha1(
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"),
85 "84983e441c3bd26ebaae4aa1f95129e5e54670f1"))
return false;
104 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"))
108 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"))
117 if (!
CheckHex(
"CRC32",
"123456789", crc(
"123456789"),
118 "cbf43926"))
return false;
122 c2.
add(
"123456789", 9);
125 uint32 val = ((uint32)buf[0] << 24) | ((uint32)buf[1] << 16) |
126 ((uint32)buf[2] << 8) | (uint32)buf[3];
127 if (val != 0xCBF43926u) {
128 unittest::fail(
"CRC32 raw bytes: expected 0xCBF43926, got 0x%08X", val);
139 "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"))
144 "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"))
154 "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"))
159 "4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45"))
166 const size_t bufSize = 64 * 1024;
167 const int iters = 256;
168 std::vector<unsigned char> buf(bufSize);
169 for (
size_t i = 0; i < bufSize; i++)
170 buf[i] = (
unsigned char)(i * 31 + 7);
174 for (
int i = 0; i < iters; i++)
175 sha.
add(&buf[0], bufSize);
176 std::string digest = sha.
getHash();
178 if (us <= 0.0) us = 1.0;
179 double totalBytes = (double)bufSize * (
double)iters;
180 double mbps = (totalBytes / (1024.0 * 1024.0)) / (us / 1e6);
183 totalBytes, us, digest.c_str());
192 "Hash library: MD5/SHA1/SHA256/CRC32/SHA3/Keccak known-answer vectors",
"util");
CMSDK unit test entry point for the vendored hash library (md5, sha1, sha256, crc32,...
CMSDK time: µs-resolution 64-bit timestamps and the Time Mapping Constant (TMC).
Small, dependency-free unit test harness used by all CMSDK object tests.
static UnitTestRunner & instance()
Access the singleton (created on first use).
void registerTest(const char *name, UnitTestFunc func, const char *description="", const char *category="", bool inDefaultRun=true)
Register a test with the runner.
compute CRC32 hash, based on Intel's Slicing-by-8 algorithm
std::string getHash()
return latest hash as 8 hex characters
void add(const void *data, size_t numBytes)
add arbitrary number of bytes
compute Keccak hash (designated SHA3)
void add(const void *data, size_t numBytes)
add arbitrary number of bytes
std::string getHash()
return latest hash as 32 hex characters
std::string getHashRaw()
return latest hash as raw characters
void add(const void *data, size_t numBytes)
add arbitrary number of bytes
std::string getHash()
return latest hash as 64 hex characters
void add(const void *data, size_t numBytes)
add arbitrary number of bytes
Third-party (vendored): CRC32 checksum from Stephan Brumme's portable hashing library (create....
uint64 GetTimeNow()
Return the current absolute time (µs since year 0) according to the TMC.
Third-party (vendored): Keccak (pre-standard SHA-3) hash from Stephan Brumme's portable hashing libra...
Third-party (vendored): MD5 hash from Stephan Brumme's portable hashing library (create....
void fail(const char *fmt,...)
Set an explanatory reason shown on the FAIL line.
void metric(const char *name, double value, const char *unit="", bool higherIsBetter=true)
Record a performance metric.
void detail(const char *fmt,...)
Verbose-only indented diagnostic line (shown only when verbose=1).
void progress(int percent, const char *action)
Report progress with a short description of the current action.
static std::string ToLowerHex(const std::string &s)
static bool CheckHex(const char *algo, const char *input, const std::string &got, const char *expected)
void Register_Hash_Tests()
bool Hash_UnitTest()
Exercise the hash classes against known test vectors.
Third-party (vendored): SHA-1 hash from Stephan Brumme's portable hashing library (create....
Third-party (vendored): SHA-256 hash from Stephan Brumme's portable hashing library (create....
Third-party (vendored): SHA-3 hash from Stephan Brumme's portable hashing library (create....