-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathencoding.hpp
More file actions
134 lines (111 loc) · 6.13 KB
/
Copy pathencoding.hpp
File metadata and controls
134 lines (111 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef _HMAC_ENCODING_HPP_INCLUDED
#define _HMAC_ENCODING_HPP_INCLUDED
#include "api.hpp"
#include "secure_buffer.hpp"
#include <cstdint>
#include <string>
#include <vector>
namespace hmac_cpp {
/// \brief Alphabet variant for Base64.
enum class Base64Alphabet { Standard, Url }; // Standard: "+/", Url: "-_"
// -------------------------
// Base64 — encode / decode
// -------------------------
/// \brief Base64-encode a byte buffer (RFC 4648).
/// \param data Pointer to input bytes.
/// \param len Number of input bytes.
/// \param alphabet Standard ("+/") or URL-safe ("-_") alphabet.
/// \param pad If true, append '=' padding to a multiple of 4 chars.
/// \return Encoded string.
HMAC_CPP_API std::string base64_encode(const uint8_t* data, size_t len,
Base64Alphabet alphabet = Base64Alphabet::Standard,
bool pad = true);
/// \brief Base64-encode a vector.
inline HMAC_CPP_API std::string base64_encode(const std::vector<uint8_t>& v,
Base64Alphabet alphabet = Base64Alphabet::Standard,
bool pad = true) {
return base64_encode(v.data(), v.size(), alphabet, pad);
}
/// \brief Base64-encode a secure_buffer.
inline HMAC_CPP_API std::string base64_encode(const secure_buffer<uint8_t>& v,
Base64Alphabet alphabet = Base64Alphabet::Standard,
bool pad = true) {
return base64_encode(v.data(), v.size(), alphabet, pad);
}
/// \brief Decode a Base64 string (RFC 4648).
/// \param in Input string (raw Base64 chars).
/// \param out Output byte vector (overwritten).
/// \param alphabet Standard ("+/") or URL-safe ("-_") alphabet.
/// \param require_padding If true, input must have proper '=' padding and length % 4 == 0.
/// \param strict If true, disallow whitespace and require '=' only in the last quartet.
/// If false, ignore ASCII spaces/CR/LF/TAB, allow missing padding, and
/// accept mixed "+/" and "-_" when \p alphabet == Url.
/// \return true on success, false on invalid input.
HMAC_CPP_API bool base64_decode(const std::string& in, std::vector<uint8_t>& out,
Base64Alphabet alphabet = Base64Alphabet::Standard,
bool require_padding = false,
bool strict = true) noexcept;
/// \brief Decode Base64 into secure_buffer.
HMAC_CPP_API bool base64_decode(const std::string& in, secure_buffer<uint8_t>& out,
Base64Alphabet alphabet = Base64Alphabet::Standard,
bool require_padding = false,
bool strict = true) noexcept;
// -------------------------
// Base32 — encode / decode
// -------------------------
/// \brief Base32-encode a byte buffer (RFC 4648; alphabet A–Z, 2–7).
/// \param data Pointer to input bytes.
/// \param len Number of input bytes.
/// \param pad If true, append '=' padding to a multiple of 8 chars.
/// \return Encoded string (upper-case alphabet).
HMAC_CPP_API std::string base32_encode(const uint8_t* data, size_t len,
bool pad = true);
/// \brief Base32-encode a vector.
inline HMAC_CPP_API std::string base32_encode(const std::vector<uint8_t>& v, bool pad = true) {
return base32_encode(v.data(), v.size(), pad);
}
/// \brief Base32-encode a secure_buffer.
inline HMAC_CPP_API std::string base32_encode(const secure_buffer<uint8_t>& v, bool pad = true) {
return base32_encode(v.data(), v.size(), pad);
}
/// \brief Decode a Base32 string (RFC 4648).
/// \param in Input string (Base32; upper-case preferred).
/// \param out Output byte vector (overwritten).
/// \param require_padding If true, input must have proper '=' padding and length % 8 == 0.
/// \param strict If true, disallow whitespace and lower-case; enforce '=' only in the last block.
/// If false, ignore ASCII spaces/CR/LF/TAB and accept lower-case letters.
/// \return true on success, false on invalid input.
HMAC_CPP_API bool base32_decode(const std::string& in, std::vector<uint8_t>& out,
bool require_padding = false,
bool strict = true) noexcept;
/// \brief Decode Base32 into secure_buffer.
HMAC_CPP_API bool base32_decode(const std::string& in, secure_buffer<uint8_t>& out,
bool require_padding = false,
bool strict = true) noexcept;
// -------------------------
// Base36 — encode / decode
// -------------------------
/// \brief Base36-encode a byte buffer (digits 0–9, letters A–Z).
/// Leading zero bytes are preserved by prefixing '0'.
/// Input of a single \x00 returns "0".
/// \param data Pointer to input bytes (big-endian).
/// \param len Number of input bytes.
/// \return Encoded string.
HMAC_CPP_API std::string base36_encode(const uint8_t* data, size_t len);
/// \brief Base36-encode a vector.
inline HMAC_CPP_API std::string base36_encode(const std::vector<uint8_t>& v) {
return base36_encode(v.data(), v.size());
}
/// \brief Base36-encode a secure_buffer.
inline HMAC_CPP_API std::string base36_encode(const secure_buffer<uint8_t>& v) {
return base36_encode(v.data(), v.size());
}
/// \brief Decode a Base36 string.
/// \param in Input string (case-insensitive).
/// \param out Output byte vector (overwritten).
/// \return true on success, false on invalid input.
HMAC_CPP_API bool base36_decode(const std::string& in, std::vector<uint8_t>& out) noexcept;
/// \brief Decode Base36 into secure_buffer.
HMAC_CPP_API bool base36_decode(const std::string& in, secure_buffer<uint8_t>& out) noexcept;
} // namespace hmac_cpp
#endif // _HMAC_ENCODING_HPP_INCLUDED