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, std::string_view str2) noexcept;
18
19/// Performs constant-time string comparison comparator
21 bool operator()(std::string_view x, std::string_view y) const noexcept {
22 return algorithm::AreStringsEqualConstTime(x, y);
23 }
24
25 /// Overload for utils::StrongTypedef that hold string like objects
26 template <typename T>
27 auto operator()(const T& x, const T& y) const noexcept
29 static_assert(
30 noexcept(algorithm::AreStringsEqualConstTime(x.GetUnderlying(), y.GetUnderlying())),
31 "The comparator should not throw as this affects efficiency "
32 "on some platforms"
33 );
34
35 return algorithm::AreStringsEqualConstTime(x.GetUnderlying(), y.GetUnderlying());
36 }
37};
38
39} // namespace crypto::algorithm
40
41USERVER_NAMESPACE_END