Skip to content

Commit e2f6b15

Browse files
committed
Add base64,des,sha1
1 parent c147bc2 commit e2f6b15

9 files changed

Lines changed: 1899 additions & 0 deletions

File tree

src/M5Utility.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
#include "m5_utility/container/circular_buffer.hpp"
2525

26+
#include "m5_utility/crypto/des.hpp"
27+
#include "m5_utility/crypto/sha1.hpp"
28+
2629
#include "m5_utility/bit_segment.hpp"
2730
#include "m5_utility/compatibility_feature.hpp"
2831
#include "m5_utility/murmurhash3.hpp"
@@ -34,6 +37,7 @@
3437
#include "m5_utility/button_status.hpp"
3538
#include "m5_utility/misc.hpp"
3639
#include "m5_utility/lfsr.hpp"
40+
#include "m5_utility/base64.hpp"
3741

3842
/*!
3943
@namespace m5

src/m5_utility/base64.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
/*!
7+
@file base64.cpp
8+
@brief base64 encoding
9+
*/
10+
#include "base64.hpp"
11+
#include <M5Utility.h>
12+
13+
namespace {
14+
constexpr char b64_table_std[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
15+
constexpr char b64_table_url[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
16+
} // namespace
17+
18+
namespace m5 {
19+
namespace utility {
20+
21+
uint32_t encode_base64(char* out, const uint32_t olen, const uint8_t* buf, const uint32_t blen, const uint8_t line_len,
22+
const bool urlEncode, const bool padding)
23+
{
24+
const char* b64_table = urlEncode ? b64_table_url : b64_table_std;
25+
26+
if (!out || !olen || !buf || !blen) return 0;
27+
28+
uint32_t written = 0;
29+
uint32_t line_pos = 0;
30+
31+
for (uint32_t i = 0; i < blen; i += 3) {
32+
uint32_t val = (buf[i] << 16);
33+
bool has_second = (i + 1 < blen);
34+
bool has_third = (i + 2 < blen);
35+
if (has_second) {
36+
val |= (buf[i + 1] << 8);
37+
}
38+
if (has_third) {
39+
val |= buf[i + 2];
40+
}
41+
42+
auto emit = [&](char c) {
43+
if (written + 1 >= olen) {
44+
return false;
45+
}
46+
out[written++] = c;
47+
if (line_len && ++line_pos == line_len) {
48+
if (written + 1 >= olen) {
49+
return false;
50+
}
51+
out[written++] = '\n';
52+
line_pos = 0;
53+
}
54+
return true;
55+
};
56+
57+
if (!emit(b64_table[(val >> 18) & 0x3F]) || !emit(b64_table[(val >> 12) & 0x3F])) {
58+
return 0;
59+
}
60+
if (has_second) {
61+
if (!emit(b64_table[(val >> 6) & 0x3F])) {
62+
return 0;
63+
}
64+
} else if (padding) {
65+
if (!emit('=')) {
66+
return 0;
67+
}
68+
}
69+
if (has_third) {
70+
if (!emit(b64_table[val & 0x3F])) {
71+
return 0;
72+
}
73+
} else if (padding) {
74+
if (!emit('=')) {
75+
return 0;
76+
}
77+
}
78+
}
79+
80+
if (written > 0 && out[written - 1] == '\n') {
81+
--written;
82+
}
83+
if (written < olen) {
84+
out[written] = '\0';
85+
}
86+
87+
return written;
88+
}
89+
90+
} // namespace utility
91+
} // namespace m5

src/m5_utility/base64.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)