userver: userver/crypto/random.hpp Source File
Loading...
Searching...
No Matches
random.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/crypto/random.hpp
4/// @brief Cryptographically-secure randomness
5
6#include <cstddef>
7#include <string>
8#include <type_traits>
9
10#include <userver/utils/span.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace crypto {
15
16namespace impl {
17
18void GenerateRandomBlock(utils::span<std::byte> buffer);
19
20} // namespace impl
21
22/// @brief Generate a block of randomness using a cryptographically secure RNG.
23///
24/// Uses a thread-local CryptoPP::AutoSeededRandomPool.
25void GenerateRandomBlock(utils::span<char> buffer);
26
27/// @overload
28template <typename T>
29void GenerateRandomBlock(utils::span<T> buffer) {
30 static_assert(std::is_trivially_copyable_v<T>);
31 impl::GenerateRandomBlock(utils::as_writable_bytes(buffer));
32}
33
34/// @overload
35std::string GenerateRandomBlock(std::size_t size);
36
37} // namespace crypto
38
39USERVER_NAMESPACE_END