userver: userver/crypto/base64.hpp Source File
Loading...
Searching...
No Matches
base64.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/crypto/base64.hpp
4/// @brief @copybrief crypto::base64
5/// @ingroup userver_universal
6
7#include <string>
8#include <string_view>
9
10USERVER_NAMESPACE_BEGIN
11
12/// @brief Cryptographic hashing, base64 encoding and decoding helpers.
13namespace crypto::base64 {
14
15enum class Pad { kWith, kWithout };
16
17/// @brief Encodes data to Base64, add padding by default
18/// @param data binary data to encode
19/// @param pad controls if pad should be added or not
20/// @throws CryptoException internal library exception
21std::string Base64Encode(std::string_view data, Pad pad = Pad::kWith);
22
23/// @brief Decodes data from Base64, padding is optional, line breaks are skipped, unused bits of the last character
24/// are ignored
25/// @throws CryptoException if @a data contains other characters outside of the alphabet (including spaces and
26/// the URL alphabet), '=' not at the end, more than two '=' or if the number of characters before '=' is 1 modulo 4
27std::string Base64Decode(std::string_view data);
28
29/// @brief Encodes data to Base64 (using URL alphabet), add padding by default
30/// @param data binary data to encode
31/// @param pad controls if pad should be added or not
32/// @throws CryptoException internal library exception
33std::string Base64UrlEncode(std::string_view data, Pad pad = Pad::kWith);
34
35/// @brief Decodes data from Base64 (using URL alphabet), padding is optional, line breaks are skipped, unused bits
36/// of the last character are ignored
37/// @throws CryptoException if @a data contains other characters outside of the URL alphabet (including spaces and
38/// the standard alphabet), '=' not at the end, more than two '=' or if the number of characters before '=' is
39/// 1 modulo 4
40std::string Base64UrlDecode(std::string_view data);
41
42/// @brief Decodes @a data in-place and returns `true` on success.
43/// @note Supports both traditional and "web-safe" base64 encodings.
44[[nodiscard]] bool Base64UniversalDecodeInPlace(std::string& data) noexcept;
45
46} // namespace crypto::base64
47
48USERVER_NAMESPACE_END