|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | +/*! |
| 7 | + @file base64.hpp |
| 8 | + @brief base64 encoding |
| 9 | +*/ |
| 10 | +#ifndef M5_UNIT_CRYPTO_UTILITY_BASE64_HPP |
| 11 | +#define M5_UNIT_CRYPTO_UTILITY_BASE64_HPP |
| 12 | + |
| 13 | +#include <cstdint> |
| 14 | + |
| 15 | +namespace m5 { |
| 16 | +namespace utility { |
| 17 | + |
| 18 | +/*! |
| 19 | + @brief encode Base64 |
| 20 | + @param[out] out Output buffer |
| 21 | + @param olen Output buffer length |
| 22 | + @param buf Input buffer |
| 23 | + @param blen Input buffer length |
| 24 | + @param line_len ==0 No line breaks ! =0: Line break at that number of characters |
| 25 | + @param urlEncode base64url encoding if true |
| 26 | + @param padding Enable padding if true |
| 27 | + @return Encoded length |
| 28 | + */ |
| 29 | +uint32_t encode_base64(char* out, const uint32_t olen, const uint8_t* buf, const uint32_t blen, const uint8_t line_len, |
| 30 | + const bool urlEncode, const bool padding); |
| 31 | + |
| 32 | +//! @brief Encode Base64(PEM) |
| 33 | +inline bool encodeBase64(char* out, const uint32_t olen, const uint8_t* buf, const uint32_t blen) |
| 34 | +{ |
| 35 | + return encode_base64(out, olen, buf, blen, 64, false, true); |
| 36 | +} |
| 37 | +//! @brief Encode Base64URL |
| 38 | +inline bool encodeBase64URL(char* out, const uint32_t olen, const uint8_t* buf, const uint32_t blen) |
| 39 | +{ |
| 40 | + return encode_base64(out, olen, buf, blen, 0, true, false); |
| 41 | +} |
| 42 | + |
| 43 | +} // namespace utility |
| 44 | +} // namespace m5 |
| 45 | +#endif |
0 commit comments