sdi_toolBox
Public Member Functions | List of all members
Base64 Class Reference

#include <base64.h>

Detailed Description

Utility class providing Base64 encoding and decoding functionality.

This class implements the Base64 encoding scheme as defined in RFC 4648. All methods are static and the class is not meant to be instantiated. Padding is handled via the '=' character.

Note
The standard Base64 alphabet is used ('A-Z', 'a-z', '0-9', '+', '/').
Example - Encoding binary data:
std::vector<std::uint8_t> data = { 0x48, 0x65, 0x6C, 0x6C, 0x6F };
std::string encoded = Base64::encode(data);
// encoded == "SGVsbG8="
static std::string encode(std::span< const std::uint8_t > data)
Encodes binary data into a Base64 string.
Definition base64.h:134
Example - Encoding a string:
std::string encoded = Base64::encode("Hello, World!");
// encoded == "SGVsbG8sIFdvcmxkIQ=="
Example - Decoding:
auto result = Base64::decode_to_string("SGVsbG8sIFdvcmxkIQ==");
if (result)
std::cout << *result; // prints: Hello, World!
static std::optional< std::string > decode_to_string(std::string_view input)
Decodes a Base64 string into a text string.
Definition base64.h:216

Definition at line 52 of file base64.h.

Public Member Functions

 Base64 ()=delete
 Deleted default constructor - this class is not meant to be instantiated.
 

Static Public Member Functions

Encoding
static std::string encode (std::span< const std::uint8_t > data)
 Encodes binary data into a Base64 string.
 
static std::string encode (std::string_view text)
 Encodes a text string into a Base64 string.
 
Decoding
static std::optional< std::vector< std::uint8_t > > decode (std::string_view input)
 Decodes a Base64 string into raw binary data.
 
static std::optional< std::string > decode_to_string (std::string_view input)
 Decodes a Base64 string into a text string.
 

Constructor & Destructor Documentation

◆ Base64()

Base64 ( )
delete

Deleted default constructor - this class is not meant to be instantiated.

Member Function Documentation

◆ encode() [1/2]

std::string encode ( std::span< const std::uint8_t >  data)
inlinestatic

Encodes binary data into a Base64 string.

Parameters
dataA span of bytes to encode.
Returns
A Base64-encoded string, padded with ‘’='` characters if necessary.

Definition at line 134 of file base64.h.

◆ encode() [2/2]

std::string encode ( std::string_view  text)
inlinestatic

Encodes a text string into a Base64 string.

Parameters
textThe input string to encode.
Returns
A Base64-encoded string, padded with ‘’='` characters if necessary.

Definition at line 157 of file base64.h.

◆ decode()

std::optional< std::vector< std::uint8_t > > decode ( std::string_view  input)
inlinestatic

Decodes a Base64 string into raw binary data.

Parameters
inputThe Base64-encoded string to decode. Must have a length that is a multiple of 4.
Returns
A vector of decoded bytes, or std::nullopt if the input is not a valid Base64 string.

Definition at line 163 of file base64.h.

◆ decode_to_string()

std::optional< std::string > decode_to_string ( std::string_view  input)
inlinestatic

Decodes a Base64 string into a text string.

Parameters
inputThe Base64-encoded string to decode. Must have a length that is a multiple of 4.
Returns
The decoded string, or std::nullopt if the input is not a valid Base64 string.

Definition at line 216 of file base64.h.