Skip to content

Commit

Permalink
feat: add support for base16
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremybarbet committed May 9, 2022
1 parent 12523f9 commit 4a3bf74
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 41 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# react-native-jsi-base-coder

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

## Installation

Expand Down Expand Up @@ -54,6 +54,8 @@ enum Algorithm {
'base32Rfc4648' = 'base32Rfc4648',
'base32Crockford' = 'base32Crockford',
'base32Hex' = 'base32Hex',
'base16Upper' = 'base16Upper',
'base16Lower' = 'base16Lower',
}
encode('string to encode', { algorithm: Algorithm.base32Crockford });
Expand Down
72 changes: 48 additions & 24 deletions cpp/BaseCoderHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include "../cppcodec/cppcodec/base32_crockford.hpp"
#include "../cppcodec/cppcodec/base32_hex.hpp"

#include "../cppcodec/cppcodec/hex_upper.hpp"
#include "../cppcodec/cppcodec/hex_lower.hpp"

#include <jsi/jsi.h>
#include <string>
#include <iostream>
Expand All @@ -34,6 +37,9 @@ Value BaseCoderHostObject::get(Runtime& runtime, const PropNameID& propNameId) {
using base32Rfc4648 = cppcodec::base32_rfc4648;
using base32Crockford = cppcodec::base32_crockford;
using base32Hex = cppcodec::base32_hex;

using base16Upper = cppcodec::hex_upper;
using base16Lower = cppcodec::hex_lower;

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

result = stringEncoded.c_str();
result = stringEncoded;
}

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

result = stringEncoded.c_str();
result = stringEncoded;
}

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

result = stringEncoded.c_str();
result = stringEncoded;
}

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

result = stringEncoded.c_str();
result = stringEncoded;
}

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

result = stringEncoded.c_str();
result = stringEncoded;
}

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

result = stringEncoded.c_str();
result = stringEncoded;
}

if (algorithm == "base16Upper") {
string stringEncoded = base16Upper::encode(stringToEncode);

result = stringEncoded;
}

if (algorithm == "base16Lower") {
string stringEncoded = base16Lower::encode(stringToEncode);

result = stringEncoded;
}

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

vector<unsigned char> result;
string result;

if (algorithm == "base64Rfc4648") {
vector<unsigned char> bytesDecoded = base64Rfc4648::decode(bytesToDecode);
vector<uint8_t> bytesDecoded = base64Rfc4648::decode(bytesToDecode);

result = bytesDecoded;
result.assign(bytesDecoded.begin(), bytesDecoded.end());
}

if (algorithm == "base64Url") {
vector<unsigned char> bytesDecoded = base64Url::decode(bytesToDecode);
vector<uint8_t> bytesDecoded = base64Url::decode(bytesToDecode);

result = bytesDecoded;
result.assign(bytesDecoded.begin(), bytesDecoded.end());
}

if (algorithm == "base64UrlUnpadded") {
vector<unsigned char> bytesDecoded = base64UrlUnpadded::decode(bytesToDecode);
vector<uint8_t> bytesDecoded = base64UrlUnpadded::decode(bytesToDecode);

result = bytesDecoded;
result.assign(bytesDecoded.begin(), bytesDecoded.end());
}

if (algorithm == "base32Rfc4648") {
vector<unsigned char> bytesDecoded = base32Rfc4648::decode(bytesToDecode);
vector<uint8_t> bytesDecoded = base32Rfc4648::decode(bytesToDecode);

result = bytesDecoded;
result.assign(bytesDecoded.begin(), bytesDecoded.end());
}

if (algorithm == "base32Crockford") {
vector<unsigned char> bytesDecoded = base32Crockford::decode(bytesToDecode);
vector<uint8_t> bytesDecoded = base32Crockford::decode(bytesToDecode);

result = bytesDecoded;
result.assign(bytesDecoded.begin(), bytesDecoded.end());
}

if (algorithm == "base32Hex") {
vector<unsigned char> bytesDecoded = base32Hex::decode(bytesToDecode);
vector<uint8_t> bytesDecoded = base32Hex::decode(bytesToDecode);

result = bytesDecoded;
result.assign(bytesDecoded.begin(), bytesDecoded.end());
}

if (algorithm == "base16Upper") {
vector<uint8_t> bytesDecoded = base16Upper::decode(bytesToDecode);

string s;
result.assign(bytesDecoded.begin(), bytesDecoded.end());
}

if (algorithm == "base16Lower") {
vector<uint8_t> bytesDecoded = base16Lower::decode(bytesToDecode);

transform(result.begin(), result.end(), back_inserter(s), [](char c) {
return c;
});
result.assign(bytesDecoded.begin(), bytesDecoded.end());
}

return String::createFromUtf8(runtime, s);
return String::createFromUtf8(runtime, result);
};

return Function::createFromHostFunction(runtime, PropNameID::forUtf8(runtime, "decode"), 0, decode);
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ PODS:
- React-jsinspector (0.67.1)
- React-logger (0.67.1):
- glog
- react-native-jsi-base-coder (0.0.1):
- react-native-jsi-base-coder (0.0.2):
- React-Core
- React-perflogger (0.67.1)
- React-RCTActionSheet (0.67.1):
Expand Down Expand Up @@ -429,7 +429,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: 1af5de75a4c834c05d53a77c1512e5aa6c18412f
React-jsinspector: ab80bcdb02f28cdfc0dbbaea6db1241565d59002
React-logger: b08f354e4c928ff794ca477347fea0922aaf11c3
react-native-jsi-base-coder: 9955793f53110c4cf13d9b061b42b43b4ff5e997
react-native-jsi-base-coder: 3d083c39c3b877b8db591e41ee24d78a03ebedb5
React-perflogger: 9a6172711d9c4c8c7ac0a426717317c3c6ecf85c
React-RCTActionSheet: ed408b54b08278e6af8a75e08679675041da61ae
React-RCTAnimation: 0163b497a423a9576a776685c6e3fe276f934758
Expand Down
35 changes: 35 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export default () => {
encodedHex: encode(rawString, { algorithm: Algorithm.base32Hex }),
};

const base16 = {
encodedUpper: encode(rawString, { algorithm: Algorithm.base16Upper }),
encodedLower: encode(rawString, { algorithm: Algorithm.base16Lower }),
};

const Base64Examples = () => (
<>
<SubHeading>Base64</SubHeading>
Expand Down Expand Up @@ -109,6 +114,35 @@ export default () => {
</>
);

const Base16Examples = () => (
<>
<SubHeading>Base16</SubHeading>
<Copy style={{ marginTop: 20 }}>Upper</Copy>

<Group>
<Copy style={{ marginBottom: 8 }}>{encodeValue(rawString)}</Copy>
<Copy>{base16.encodedUpper}</Copy>
</Group>

<Group>
<Copy style={{ marginBottom: 8 }}>{decodeValue(base16.encodedUpper)}</Copy>
<Copy>{decode(base16.encodedUpper, { algorithm: Algorithm.base16Upper })}</Copy>
</Group>

<Copy style={{ marginTop: 20 }}>Lower</Copy>

<Group>
<Copy style={{ marginBottom: 8 }}>{encodeValue(rawString)}</Copy>
<Copy>{base16.encodedLower}</Copy>
</Group>

<Group>
<Copy style={{ marginBottom: 8 }}>{decodeValue(base16.encodedLower)}</Copy>
<Copy>{decode(base16.encodedLower, { algorithm: Algorithm.base16Lower })}</Copy>
</Group>
</>
);

return (
<ScrollView>
<StatusBar barStyle={isDark ? 'light-content' : 'dark-content'} />
Expand All @@ -119,6 +153,7 @@ export default () => {
<View style={styles.group}>
<Base64Examples />
<Base32Examples />
<Base16Examples />
</View>
</View>
</ScrollView>
Expand Down
29 changes: 15 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-native-jsi-base-coder",
"version": "0.0.2",
"description": "Base64 and Base32 encoding/decoding for React Native written in C/C++ and JSI.",
"description": "Base64/32/16 encoding/decoding for React Native written in C/C++ and JSI.",
"repository": "https://github.com/jeremybarbet/react-native-jsi-base-coder",
"author": "Jérémy Barbet <[email protected]> (https://github.com/jeremybarbet)",
"license": "MIT",
Expand Down Expand Up @@ -31,6 +31,20 @@
"!**/__fixtures__",
"!**/__mocks__"
],
"keywords": [
"react-native",
"ios",
"android",
"jsi",
"c++",
"base64",
"base32",
"base16",
"encode",
"decode",
"rfc4648",
"crockford"
],
"scripts": {
"typescript": "tsc --noEmit",
"prepare": "yarn build && yarn build:dts",
Expand All @@ -45,19 +59,6 @@
"format": "git ls-files -m | xargs yarn prettier --write --ignore-unknown --no-error-on-unmatched-pattern",
"submodule:update": "git submodule update --remote --merge"
},
"keywords": [
"react-native",
"ios",
"android",
"jsi",
"c++",
"base64",
"base32",
"encode",
"decode",
"rfc4648",
"crockford"
],
"devDependencies": {
"@firmnav/eslint-github-actions-formatter": "1.0.1",
"@release-it/conventional-changelog": "4.3.0",
Expand Down
2 changes: 2 additions & 0 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export enum Algorithm {
'base32Rfc4648' = 'base32Rfc4648',
'base32Crockford' = 'base32Crockford',
'base32Hex' = 'base32Hex',
'base16Upper' = 'base16Upper',
'base16Lower' = 'base16Lower',
}

export interface Config {
Expand Down

0 comments on commit 4a3bf74

Please sign in to comment.