MCPcopy Create free account
hub / github.com/MITK/MITK / ConstantTimeCompare

Function ConstantTimeCompare

Modules/RESTAPI/src/mitkRestServer.cpp:88–98  ·  view source on GitHub ↗

* @brief Constant-time string comparison to prevent timing attacks. * * Uses OpenSSL's CRYPTO_memcmp for guaranteed constant-time behavior. */

Source from the content-addressed store, hash-verified

86 * Uses OpenSSL's CRYPTO_memcmp for guaranteed constant-time behavior.
87 */
88 bool ConstantTimeCompare(const std::string& a, const std::string& b)
89 {
90 // Compare in constant time to avoid leaking token length through timing.
91 // XOR of sizes is non-zero if they differ; CRYPTO_memcmp compares up to
92 // min(len) bytes. The combined result is zero only when both size and
93 // content match exactly.
94 volatile size_t result = a.size() ^ b.size();
95 const size_t len = std::min(a.size(), b.size());
96 result |= static_cast<size_t>(CRYPTO_memcmp(a.data(), b.data(), len));
97 return result == 0;
98 }
99
100 /**
101 * @brief Check if a request path is exempt from authentication.

Callers 1

CheckAuthenticationMethod · 0.85

Calls 2

sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected