|
| 1 | +#ifndef _HMAC_ENCODING_HPP_INCLUDED |
| 2 | +#define _HMAC_ENCODING_HPP_INCLUDED |
| 3 | + |
| 4 | +#include "api.hpp" |
| 5 | +#include "secure_buffer.hpp" |
| 6 | +#include <cstdint> |
| 7 | +#include <string> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +namespace hmac_cpp { |
| 11 | + |
| 12 | + /// \brief Alphabet variant for Base64. |
| 13 | + enum class Base64Alphabet { Standard, Url }; // Standard: "+/", Url: "-_" |
| 14 | + |
| 15 | + // ------------------------- |
| 16 | + // Base64 — encode / decode |
| 17 | + // ------------------------- |
| 18 | + |
| 19 | + /// \brief Base64-encode a byte buffer (RFC 4648). |
| 20 | + /// \param data Pointer to input bytes. |
| 21 | + /// \param len Number of input bytes. |
| 22 | + /// \param alphabet Standard ("+/") or URL-safe ("-_") alphabet. |
| 23 | + /// \param pad If true, append '=' padding to a multiple of 4 chars. |
| 24 | + /// \return Encoded string. |
| 25 | + HMAC_CPP_API std::string base64_encode(const uint8_t* data, size_t len, |
| 26 | + Base64Alphabet alphabet = Base64Alphabet::Standard, |
| 27 | + bool pad = true); |
| 28 | + |
| 29 | + /// \brief Base64-encode a vector. |
| 30 | + inline std::string base64_encode(const std::vector<uint8_t>& v, |
| 31 | + Base64Alphabet alphabet = Base64Alphabet::Standard, |
| 32 | + bool pad = true) { |
| 33 | + return base64_encode(v.data(), v.size(), alphabet, pad); |
| 34 | + } |
| 35 | + |
| 36 | + /// \brief Base64-encode a secure_buffer. |
| 37 | + inline std::string base64_encode(const secure_buffer<uint8_t>& v, |
| 38 | + Base64Alphabet alphabet = Base64Alphabet::Standard, |
| 39 | + bool pad = true) { |
| 40 | + return base64_encode(v.data(), v.size(), alphabet, pad); |
| 41 | + } |
| 42 | + |
| 43 | + /// \brief Decode a Base64 string (RFC 4648). |
| 44 | + /// \param in Input string (raw Base64 chars). |
| 45 | + /// \param out Output byte vector (overwritten). |
| 46 | + /// \param alphabet Standard ("+/") or URL-safe ("-_") alphabet. |
| 47 | + /// \param require_padding If true, input must have proper '=' padding and length % 4 == 0. |
| 48 | + /// \param strict If true, disallow whitespaces and enforce '=' only in the last quartet. |
| 49 | + /// If false, ignore ASCII spaces and CR/LF/TAB and allow missing padding. |
| 50 | + /// \return true on success, false on invalid input. |
| 51 | + HMAC_CPP_API bool base64_decode(const std::string& in, std::vector<uint8_t>& out, |
| 52 | + Base64Alphabet alphabet = Base64Alphabet::Standard, |
| 53 | + bool require_padding = false, |
| 54 | + bool strict = true) noexcept; |
| 55 | + |
| 56 | + /// \brief Decode Base64 into secure_buffer. |
| 57 | + HMAC_CPP_API bool base64_decode(const std::string& in, secure_buffer<uint8_t>& out, |
| 58 | + Base64Alphabet alphabet = Base64Alphabet::Standard, |
| 59 | + bool require_padding = false, |
| 60 | + bool strict = true) noexcept; |
| 61 | + |
| 62 | + // ------------------------- |
| 63 | + // Base32 — encode / decode |
| 64 | + // ------------------------- |
| 65 | + |
| 66 | + /// \brief Base32-encode a byte buffer (RFC 4648; alphabet A–Z, 2–7). |
| 67 | + /// \param data Pointer to input bytes. |
| 68 | + /// \param len Number of input bytes. |
| 69 | + /// \param pad If true, append '=' padding to a multiple of 8 chars. |
| 70 | + /// \return Encoded string (upper-case). |
| 71 | + HMAC_CPP_API std::string base32_encode(const uint8_t* data, size_t len, |
| 72 | + bool pad = true); |
| 73 | + |
| 74 | + /// \brief Base32-encode a vector. |
| 75 | + inline std::string base32_encode(const std::vector<uint8_t>& v, bool pad = true) { |
| 76 | + return base32_encode(v.data(), v.size(), pad); |
| 77 | + } |
| 78 | + |
| 79 | + /// \brief Base32-encode a secure_buffer. |
| 80 | + inline std::string base32_encode(const secure_buffer<uint8_t>& v, bool pad = true) { |
| 81 | + return base32_encode(v.data(), v.size(), pad); |
| 82 | + } |
| 83 | + |
| 84 | + /// \brief Decode a Base32 string (RFC 4648). |
| 85 | + /// \param in Input string (Base32; upper-case preferred). |
| 86 | + /// \param out Output byte vector (overwritten). |
| 87 | + /// \param require_padding If true, input must have proper '=' padding and length % 8 == 0. |
| 88 | + /// \param strict If true, disallow whitespaces and lower-case; enforce '=' only in the last block. |
| 89 | + /// If false, ignore ASCII spaces and CR/LF/TAB and accept lower-case letters. |
| 90 | + /// \return true on success, false on invalid input. |
| 91 | + HMAC_CPP_API bool base32_decode(const std::string& in, std::vector<uint8_t>& out, |
| 92 | + bool require_padding = false, |
| 93 | + bool strict = true) noexcept; |
| 94 | + |
| 95 | + /// \brief Decode Base32 into secure_buffer. |
| 96 | + HMAC_CPP_API bool base32_decode(const std::string& in, secure_buffer<uint8_t>& out, |
| 97 | + bool require_padding = false, |
| 98 | + bool strict = true) noexcept; |
| 99 | + |
| 100 | +} // namespace hmac_cpp |
| 101 | + |
| 102 | +#endif // _HMAC_ENCODING_HPP_INCLUDED |
0 commit comments