Skip to content

Commit 4a3bf74

Browse files
committed
feat: add support for base16
1 parent 12523f9 commit 4a3bf74

File tree

6 files changed

+105
-41
lines changed

6 files changed

+105
-41
lines changed

Diff for: README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# react-native-jsi-base-coder
22

3-
Base64 and Base32 encoding/decoding for React Native written in C/C++ and JSI.
3+
Base64/32/16 encoding/decoding for React Native written in C/C++ and JSI.
44

55
## Installation
66

@@ -54,6 +54,8 @@ enum Algorithm {
5454
'base32Rfc4648' = 'base32Rfc4648',
5555
'base32Crockford' = 'base32Crockford',
5656
'base32Hex' = 'base32Hex',
57+
'base16Upper' = 'base16Upper',
58+
'base16Lower' = 'base16Lower',
5759
}
5860
5961
encode('string to encode', { algorithm: Algorithm.base32Crockford });

Diff for: cpp/BaseCoderHostObject.cpp

+48-24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include "../cppcodec/cppcodec/base32_crockford.hpp"
99
#include "../cppcodec/cppcodec/base32_hex.hpp"
1010

11+
#include "../cppcodec/cppcodec/hex_upper.hpp"
12+
#include "../cppcodec/cppcodec/hex_lower.hpp"
13+
1114
#include <jsi/jsi.h>
1215
#include <string>
1316
#include <iostream>
@@ -34,6 +37,9 @@ Value BaseCoderHostObject::get(Runtime& runtime, const PropNameID& propNameId) {
3437
using base32Rfc4648 = cppcodec::base32_rfc4648;
3538
using base32Crockford = cppcodec::base32_crockford;
3639
using base32Hex = cppcodec::base32_hex;
40+
41+
using base16Upper = cppcodec::hex_upper;
42+
using base16Lower = cppcodec::hex_lower;
3743

3844
if (propName == "encode") {
3945
auto encode = [this] (Runtime& runtime, const Value& thisValue, const Value* arguments, size_t count) -> Value {
@@ -57,37 +63,49 @@ Value BaseCoderHostObject::get(Runtime& runtime, const PropNameID& propNameId) {
5763
if (algorithm == "base64Rfc4648") {
5864
string stringEncoded = base64Rfc4648::encode(stringToEncode);
5965

60-
result = stringEncoded.c_str();
66+
result = stringEncoded;
6167
}
6268

6369
if (algorithm == "base64Url") {
6470
string stringEncoded = base64Url::encode(stringToEncode);
6571

66-
result = stringEncoded.c_str();
72+
result = stringEncoded;
6773
}
6874

6975
if (algorithm == "base64UrlUnpadded") {
7076
string stringEncoded = base64UrlUnpadded::encode(stringToEncode);
7177

72-
result = stringEncoded.c_str();
78+
result = stringEncoded;
7379
}
7480

7581
if (algorithm == "base32Rfc4648") {
7682
string stringEncoded = base32Rfc4648::encode(stringToEncode);
7783

78-
result = stringEncoded.c_str();
84+
result = stringEncoded;
7985
}
8086

8187
if (algorithm == "base32Crockford") {
8288
string stringEncoded = base32Crockford::encode(stringToEncode);
8389

84-
result = stringEncoded.c_str();
90+
result = stringEncoded;
8591
}
8692

8793
if (algorithm == "base32Hex") {
8894
string stringEncoded = base32Hex::encode(stringToEncode);
8995

90-
result = stringEncoded.c_str();
96+
result = stringEncoded;
97+
}
98+
99+
if (algorithm == "base16Upper") {
100+
string stringEncoded = base16Upper::encode(stringToEncode);
101+
102+
result = stringEncoded;
103+
}
104+
105+
if (algorithm == "base16Lower") {
106+
string stringEncoded = base16Lower::encode(stringToEncode);
107+
108+
result = stringEncoded;
91109
}
92110

93111
return String::createFromUtf8(runtime, result);
@@ -113,51 +131,57 @@ Value BaseCoderHostObject::get(Runtime& runtime, const PropNameID& propNameId) {
113131
throw JSError(runtime, "[react-native-jsi-base-coder] The `bytesToDecode` cannot be an empty string.");
114132
}
115133

116-
vector<unsigned char> result;
134+
string result;
117135

118136
if (algorithm == "base64Rfc4648") {
119-
vector<unsigned char> bytesDecoded = base64Rfc4648::decode(bytesToDecode);
137+
vector<uint8_t> bytesDecoded = base64Rfc4648::decode(bytesToDecode);
120138

121-
result = bytesDecoded;
139+
result.assign(bytesDecoded.begin(), bytesDecoded.end());
122140
}
123141

124142
if (algorithm == "base64Url") {
125-
vector<unsigned char> bytesDecoded = base64Url::decode(bytesToDecode);
143+
vector<uint8_t> bytesDecoded = base64Url::decode(bytesToDecode);
126144

127-
result = bytesDecoded;
145+
result.assign(bytesDecoded.begin(), bytesDecoded.end());
128146
}
129147

130148
if (algorithm == "base64UrlUnpadded") {
131-
vector<unsigned char> bytesDecoded = base64UrlUnpadded::decode(bytesToDecode);
149+
vector<uint8_t> bytesDecoded = base64UrlUnpadded::decode(bytesToDecode);
132150

133-
result = bytesDecoded;
151+
result.assign(bytesDecoded.begin(), bytesDecoded.end());
134152
}
135153

136154
if (algorithm == "base32Rfc4648") {
137-
vector<unsigned char> bytesDecoded = base32Rfc4648::decode(bytesToDecode);
155+
vector<uint8_t> bytesDecoded = base32Rfc4648::decode(bytesToDecode);
138156

139-
result = bytesDecoded;
157+
result.assign(bytesDecoded.begin(), bytesDecoded.end());
140158
}
141159

142160
if (algorithm == "base32Crockford") {
143-
vector<unsigned char> bytesDecoded = base32Crockford::decode(bytesToDecode);
161+
vector<uint8_t> bytesDecoded = base32Crockford::decode(bytesToDecode);
144162

145-
result = bytesDecoded;
163+
result.assign(bytesDecoded.begin(), bytesDecoded.end());
146164
}
147165

148166
if (algorithm == "base32Hex") {
149-
vector<unsigned char> bytesDecoded = base32Hex::decode(bytesToDecode);
167+
vector<uint8_t> bytesDecoded = base32Hex::decode(bytesToDecode);
150168

151-
result = bytesDecoded;
169+
result.assign(bytesDecoded.begin(), bytesDecoded.end());
152170
}
171+
172+
if (algorithm == "base16Upper") {
173+
vector<uint8_t> bytesDecoded = base16Upper::decode(bytesToDecode);
153174

154-
string s;
175+
result.assign(bytesDecoded.begin(), bytesDecoded.end());
176+
}
177+
178+
if (algorithm == "base16Lower") {
179+
vector<uint8_t> bytesDecoded = base16Lower::decode(bytesToDecode);
155180

156-
transform(result.begin(), result.end(), back_inserter(s), [](char c) {
157-
return c;
158-
});
181+
result.assign(bytesDecoded.begin(), bytesDecoded.end());
182+
}
159183

160-
return String::createFromUtf8(runtime, s);
184+
return String::createFromUtf8(runtime, result);
161185
};
162186

163187
return Function::createFromHostFunction(runtime, PropNameID::forUtf8(runtime, "decode"), 0, decode);

Diff for: example/ios/Podfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ PODS:
228228
- React-jsinspector (0.67.1)
229229
- React-logger (0.67.1):
230230
- glog
231-
- react-native-jsi-base-coder (0.0.1):
231+
- react-native-jsi-base-coder (0.0.2):
232232
- React-Core
233233
- React-perflogger (0.67.1)
234234
- React-RCTActionSheet (0.67.1):
@@ -429,7 +429,7 @@ SPEC CHECKSUMS:
429429
React-jsiexecutor: 1af5de75a4c834c05d53a77c1512e5aa6c18412f
430430
React-jsinspector: ab80bcdb02f28cdfc0dbbaea6db1241565d59002
431431
React-logger: b08f354e4c928ff794ca477347fea0922aaf11c3
432-
react-native-jsi-base-coder: 9955793f53110c4cf13d9b061b42b43b4ff5e997
432+
react-native-jsi-base-coder: 3d083c39c3b877b8db591e41ee24d78a03ebedb5
433433
React-perflogger: 9a6172711d9c4c8c7ac0a426717317c3c6ecf85c
434434
React-RCTActionSheet: ed408b54b08278e6af8a75e08679675041da61ae
435435
React-RCTAnimation: 0163b497a423a9576a776685c6e3fe276f934758

Diff for: example/src/App.tsx

+35
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export default () => {
2727
encodedHex: encode(rawString, { algorithm: Algorithm.base32Hex }),
2828
};
2929

30+
const base16 = {
31+
encodedUpper: encode(rawString, { algorithm: Algorithm.base16Upper }),
32+
encodedLower: encode(rawString, { algorithm: Algorithm.base16Lower }),
33+
};
34+
3035
const Base64Examples = () => (
3136
<>
3237
<SubHeading>Base64</SubHeading>
@@ -109,6 +114,35 @@ export default () => {
109114
</>
110115
);
111116

117+
const Base16Examples = () => (
118+
<>
119+
<SubHeading>Base16</SubHeading>
120+
<Copy style={{ marginTop: 20 }}>Upper</Copy>
121+
122+
<Group>
123+
<Copy style={{ marginBottom: 8 }}>{encodeValue(rawString)}</Copy>
124+
<Copy>{base16.encodedUpper}</Copy>
125+
</Group>
126+
127+
<Group>
128+
<Copy style={{ marginBottom: 8 }}>{decodeValue(base16.encodedUpper)}</Copy>
129+
<Copy>{decode(base16.encodedUpper, { algorithm: Algorithm.base16Upper })}</Copy>
130+
</Group>
131+
132+
<Copy style={{ marginTop: 20 }}>Lower</Copy>
133+
134+
<Group>
135+
<Copy style={{ marginBottom: 8 }}>{encodeValue(rawString)}</Copy>
136+
<Copy>{base16.encodedLower}</Copy>
137+
</Group>
138+
139+
<Group>
140+
<Copy style={{ marginBottom: 8 }}>{decodeValue(base16.encodedLower)}</Copy>
141+
<Copy>{decode(base16.encodedLower, { algorithm: Algorithm.base16Lower })}</Copy>
142+
</Group>
143+
</>
144+
);
145+
112146
return (
113147
<ScrollView>
114148
<StatusBar barStyle={isDark ? 'light-content' : 'dark-content'} />
@@ -119,6 +153,7 @@ export default () => {
119153
<View style={styles.group}>
120154
<Base64Examples />
121155
<Base32Examples />
156+
<Base16Examples />
122157
</View>
123158
</View>
124159
</ScrollView>

Diff for: package.json

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-native-jsi-base-coder",
33
"version": "0.0.2",
4-
"description": "Base64 and Base32 encoding/decoding for React Native written in C/C++ and JSI.",
4+
"description": "Base64/32/16 encoding/decoding for React Native written in C/C++ and JSI.",
55
"repository": "https://github.com/jeremybarbet/react-native-jsi-base-coder",
66
"author": "Jérémy Barbet <[email protected]> (https://github.com/jeremybarbet)",
77
"license": "MIT",
@@ -31,6 +31,20 @@
3131
"!**/__fixtures__",
3232
"!**/__mocks__"
3333
],
34+
"keywords": [
35+
"react-native",
36+
"ios",
37+
"android",
38+
"jsi",
39+
"c++",
40+
"base64",
41+
"base32",
42+
"base16",
43+
"encode",
44+
"decode",
45+
"rfc4648",
46+
"crockford"
47+
],
3448
"scripts": {
3549
"typescript": "tsc --noEmit",
3650
"prepare": "yarn build && yarn build:dts",
@@ -45,19 +59,6 @@
4559
"format": "git ls-files -m | xargs yarn prettier --write --ignore-unknown --no-error-on-unmatched-pattern",
4660
"submodule:update": "git submodule update --remote --merge"
4761
},
48-
"keywords": [
49-
"react-native",
50-
"ios",
51-
"android",
52-
"jsi",
53-
"c++",
54-
"base64",
55-
"base32",
56-
"encode",
57-
"decode",
58-
"rfc4648",
59-
"crockford"
60-
],
6162
"devDependencies": {
6263
"@firmnav/eslint-github-actions-formatter": "1.0.1",
6364
"@release-it/conventional-changelog": "4.3.0",

Diff for: src/types/global.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export enum Algorithm {
77
'base32Rfc4648' = 'base32Rfc4648',
88
'base32Crockford' = 'base32Crockford',
99
'base32Hex' = 'base32Hex',
10+
'base16Upper' = 'base16Upper',
11+
'base16Lower' = 'base16Lower',
1012
}
1113

1214
export interface Config {

0 commit comments

Comments
 (0)