userver: userver/crypto/algorithm.hpp Source File
Loading...
Searching...
No Matches
algorithm.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/crypto/algorithm.hpp
4/// @brief @copybrief crypto::algorithm
5/// @ingroup userver_universal
6
7#include <string_view>
8
9USERVER_NAMESPACE_BEGIN
10
11/// Miscellaneous cryptographic routines
12namespace crypto::algorithm {
13
14/// Performs constant-time string comparison if the strings are of equal size
15///
16/// @snippet storages/secdist/secdist_test.cpp UserPasswords
17bool AreStringsEqualConstTime(std::string_view str1,
18 std::string_view str2) noexcept;
19
20/// Performs constant-time string comparison comparator
22 bool operator()(std::string_view x, std::string_view y) const noexcept {
23 return algorithm::AreStringsEqualConstTime(x, y);
24 }
25
26 /// Overload for utils::StrongTypedef that hold string like objects
27 template <typename T>
28 auto operator()(const T& x, const T& y) const noexcept
30 y.GetUnderlying())) {
31 static_assert(noexcept(algorithm::AreStringsEqualConstTime(
32 x.GetUnderlying(), y.GetUnderlying())),
33 "The comparator should not throw as this affects efficiency "
34 "on some platforms");
35
36 return algorithm::AreStringsEqualConstTime(x.GetUnderlying(),
37 y.GetUnderlying());
38 }
39};
40
41} // namespace crypto::algorithm
42
43USERVER_NAMESPACE_END