cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
constant_time.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
5#include <cstddef>
6#include <string_view>
7
10
11namespace mcp::auth {
12
17inline bool constant_time_string_equal(std::string_view lhs,
18 std::string_view rhs) noexcept {
19 const auto max_size = lhs.size() > rhs.size() ? lhs.size() : rhs.size();
20 std::size_t diff = lhs.size() ^ rhs.size();
21 for (std::size_t index = 0; index < max_size; ++index) {
22 const auto lhs_byte =
23 index < lhs.size() ? static_cast<unsigned char>(lhs[index]) : 0U;
24 const auto rhs_byte =
25 index < rhs.size() ? static_cast<unsigned char>(rhs[index]) : 0U;
26 diff |= static_cast<std::size_t>(lhs_byte ^ rhs_byte);
27 }
28 return diff == 0;
29}
30
31} // namespace mcp::auth
bool constant_time_string_equal(std::string_view lhs, std::string_view rhs) noexcept
Compare two strings without data-dependent early exit.
Definition constant_time.hpp:17