Gathering detailed insights and metrics for @s77rt/react-native-sodium
Gathering detailed insights and metrics for @s77rt/react-native-sodium
Gathering detailed insights and metrics for @s77rt/react-native-sodium
Gathering detailed insights and metrics for @s77rt/react-native-sodium
A fast cryptography module for React Native using libsodium.
npm install @s77rt/react-native-sodium
Typescript
Module System
Node Version
NPM Version
C++ (73.43%)
TypeScript (18.46%)
Kotlin (2.93%)
CMake (1.74%)
Ruby (1.55%)
Objective-C++ (0.99%)
Shell (0.66%)
Objective-C (0.25%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
3 Stars
95 Commits
2 Watchers
1 Branches
1 Contributors
Updated on Jun 10, 2025
Latest Version
0.4.0
Package Id
@s77rt/react-native-sodium@0.4.0
Unpacked Size
18.98 MB
Size
8.81 MB
File Count
724
NPM Version
10.8.2
Node Version
20.18.1
Published on
May 09, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
@s77rt/react-native-sodium is a high-performance cryptography library written in C++ that uses the JavaScript Interface (JSI) to provide access to the Sodium API. Sodium is a modern, easy-to-use library for encryption, decryption, signatures, password hashing, and more.
1npm install @s77rt/react-native-sodium
For detailed documentation checkout libsodium Documentation.
1import sodium from "@s77rt/react-native-sodium"; 2 3if (sodium.sodium_init() < 0) { 4 throw new Error("Failed to initialize sodium!"); 5}
1randombytes_random(): number;
1const rnd = sodium.randombytes_random(); 2console.log("Random number:", rnd);
1randombytes_uniform(upperBound: number): number;
1const upperBound = 100; 2const rnd = sodium.randombytes_uniform(upperBound); 3console.log("Random number:", rnd);
1randombytes_buf(buf: ArrayBuffer, size: number): void;
1const buf = new ArrayBuffer(8);
2sodium.randombytes_buf(buf, buf.byteLength);
3console.log("Random byte sequence:", new Uint8Array(buf));
1randombytes_buf_deterministic(buf: ArrayBuffer, size: number, seed: ArrayBuffer): void;
1const buf = new ArrayBuffer(8);
2const seed = new ArrayBuffer(sodium.randombytes_SEEDBYTES);
3new TextEncoder().encodeInto("Couscous", new Uint8Array(seed));
4sodium.randombytes_buf_deterministic(buf, buf.byteLength, seed);
5console.log("Deterministic random byte sequence:", new Uint8Array(buf));
1randombytes_close(): number;
1randombytes_close();
1randombytes_stir(): void;
1randombytes_stir();
1crypto_box_keypair(pk: ArrayBuffer, sk: ArrayBuffer): number;
2crypto_box_seed_keypair(pk: ArrayBuffer, sk: ArrayBuffer, seed: ArrayBuffer): number;
3crypto_box_easy(c: ArrayBuffer, m: ArrayBuffer, mLen: number, n: ArrayBuffer, pk: ArrayBuffer, sk: ArrayBuffer): number;
4crypto_box_open_easy(m: ArrayBuffer, c: ArrayBuffer, cLen: number, n: ArrayBuffer, pk: ArrayBuffer, sk: ArrayBuffer): number;
1const alicePK = new ArrayBuffer(sodium.crypto_box_PUBLICKEYBYTES);
2const aliceSK = new ArrayBuffer(sodium.crypto_box_SECRETKEYBYTES);
3const bobPK = new ArrayBuffer(sodium.crypto_box_PUBLICKEYBYTES);
4const bobSK = new ArrayBuffer(sodium.crypto_box_SECRETKEYBYTES);
5const nonce = new ArrayBuffer(sodium.crypto_box_NONCEBYTES);
6const message = new TextEncoder().encode("Fennec fox").buffer;
7const messageL = message.byteLength;
8const cipherL = sodium.crypto_box_MACBYTES + messageL;
9const cipher = new ArrayBuffer(cipherL);
10const decrypted = new ArrayBuffer(messageL);
11sodium.crypto_box_keypair(alicePK, aliceSK);
12sodium.crypto_box_keypair(bobPK, bobSK);
13sodium.randombytes_buf(nonce, nonce.byteLength);
14sodium.crypto_box_easy(cipher, message, messageL, nonce, bobPK, aliceSK);
15sodium.crypto_box_open_easy(decrypted, cipher, cipherL, nonce, alicePK, bobSK);
16console.log("Message:", new Uint8Array(message));
17console.log("Cipher:", new Uint8Array(cipher));
18console.log("Decrypted:", new Uint8Array(decrypted));
1crypto_box_seal(c: ArrayBuffer, m: ArrayBuffer, mLen: number, pk: ArrayBuffer): number;
2crypto_box_seal_open(m: ArrayBuffer, c: ArrayBuffer, cLen: number, pk: ArrayBuffer, sk: ArrayBuffer): number;
1const bobPK = new ArrayBuffer(sodium.crypto_box_PUBLICKEYBYTES);
2const bobSK = new ArrayBuffer(sodium.crypto_box_SECRETKEYBYTES);
3const message = new TextEncoder().encode("Fennec fox").buffer;
4const messageL = message.byteLength;
5const cipherL = sodium.crypto_box_SEALBYTES + messageL;
6const cipher = new ArrayBuffer(cipherL);
7const decrypted = new ArrayBuffer(messageL);
8sodium.crypto_box_keypair(bobPK, bobSK);
9sodium.crypto_box_seal(cipher, message, messageL, bobPK);
10sodium.crypto_box_seal_open(decrypted, cipher, cipherL, bobPK, bobSK);
11console.log("Message:", new Uint8Array(message));
12console.log("Cipher:", new Uint8Array(cipher));
13console.log("Decrypted:", new Uint8Array(decrypted));
1crypto_sign_keypair(pk: ArrayBuffer, sk: ArrayBuffer): number;
2crypto_sign_seed_keypair(pk: ArrayBuffer, sk: ArrayBuffer, seed: ArrayBuffer): number;
3crypto_sign(sm: ArrayBuffer, smLenP: ArrayBuffer | null, m: ArrayBuffer, mLen: number, sk: ArrayBuffer): number;
4crypto_sign_open(m: ArrayBuffer, mLenP: ArrayBuffer | null, sm: ArrayBuffer, smLen: number, pk: ArrayBuffer): number;
1const bobPK = new ArrayBuffer(sodium.crypto_sign_PUBLICKEYBYTES);
2const bobSK = new ArrayBuffer(sodium.crypto_sign_SECRETKEYBYTES);
3const message = new TextEncoder().encode("I saw a fennec fox").buffer;
4const messageL = message.byteLength;
5const signedL = sodium.crypto_sign_BYTES + messageL;
6const signed = new ArrayBuffer(signedL);
7sodium.crypto_sign_keypair(bobPK, bobSK);
8if (sodium.crypto_sign(signed, null, message, messageL, bobSK) !== 0) {
9 throw new Error("Failed to sign the message!");
10}
11if (sodium.crypto_sign_open(null, null, signed, signedL, bobPK) !== 0) {
12 throw new Error("Invalid signature!");
13}
14console.log("Message:", new Uint8Array(message));
15console.log("Signed:", new Uint8Array(signed));
1crypto_generichash(output: ArrayBuffer, outputLen: number, input: ArrayBuffer, inputLen: number, key: ArrayBuffer | null, keyLen: number): number;
1const output = new ArrayBuffer(sodium.crypto_generichash_BYTES);
2const input = new TextEncoder().encode("Fennec fox").buffer;
3const key = new ArrayBuffer(sodium.crypto_generichash_KEYBYTES);
4sodium.crypto_generichash_keygen(key);
5sodium.crypto_generichash(
6 output,
7 output.byteLength,
8 input,
9 input.byteLength,
10 key,
11 key.byteLength
12);
13console.log(
14 "Key:",
15 sodium.sodium_bin2hex(
16 new ArrayBuffer(key.byteLength * 2 + 1),
17 key.byteLength * 2 + 1,
18 key,
19 key.byteLength
20 )
21);
22console.log(
23 "Hash:",
24 sodium.sodium_bin2hex(
25 new ArrayBuffer(output.byteLength * 2 + 1),
26 output.byteLength * 2 + 1,
27 output,
28 output.byteLength
29 )
30);
1crypto_generichash_init(state: Record<string, never>, key: ArrayBuffer | null, keyLen: number, outputLen: number): number;
2crypto_generichash_update(state: Record<string, never>, input: ArrayBuffer, inputLen: number): number;
3crypto_generichash_final(state: Record<string, never>, output: ArrayBuffer, outputLen: number): number;
1const output = new ArrayBuffer(sodium.crypto_generichash_BYTES);
2const input1 = new TextEncoder().encode("Fennec ").buffer;
3const input2 = new TextEncoder().encode("fox").buffer;
4const key = new ArrayBuffer(sodium.crypto_generichash_KEYBYTES);
5const state = {};
6sodium.crypto_generichash_keygen(key);
7sodium.crypto_generichash_init(state, key, key.byteLength, output.byteLength);
8sodium.crypto_generichash_update(state, input1, input1.byteLength);
9sodium.crypto_generichash_update(state, input2, input2.byteLength);
10sodium.crypto_generichash_final(state, output, output.byteLength);
11console.log(
12 "Key:",
13 sodium.sodium_bin2hex(
14 new ArrayBuffer(key.byteLength * 2 + 1),
15 key.byteLength * 2 + 1,
16 key,
17 key.byteLength
18 )
19);
20console.log(
21 "Hash:",
22 sodium.sodium_bin2hex(
23 new ArrayBuffer(output.byteLength * 2 + 1),
24 output.byteLength * 2 + 1,
25 output,
26 output.byteLength
27 )
28);
1crypto_generichash_keygen(k: ArrayBuffer): void;
1const k = new ArrayBuffer(sodium.crypto_generichash_KEYBYTES);
2sodium.crypto_generichash_keygen(k);
3console.log(
4 "Key:",
5 sodium.sodium_bin2hex(
6 new ArrayBuffer(k.byteLength * 2 + 1),
7 k.byteLength * 2 + 1,
8 k,
9 k.byteLength
10 )
11);
1crypto_shorthash(output: ArrayBuffer, input: ArrayBuffer, inputLen: number, k: ArrayBuffer): number;
1const output = new ArrayBuffer(sodium.crypto_shorthash_BYTES);
2const input = new TextEncoder().encode("Fennec fox").buffer;
3const k = new ArrayBuffer(sodium.crypto_shorthash_KEYBYTES);
4sodium.crypto_shorthash_keygen(k);
5sodium.crypto_shorthash(output, input, input.byteLength, k);
6console.log(
7 "Key:",
8 sodium.sodium_bin2hex(
9 new ArrayBuffer(k.byteLength * 2 + 1),
10 k.byteLength * 2 + 1,
11 k,
12 k.byteLength
13 )
14);
15console.log(
16 "Hash:",
17 sodium.sodium_bin2hex(
18 new ArrayBuffer(output.byteLength * 2 + 1),
19 output.byteLength * 2 + 1,
20 output,
21 output.byteLength
22 )
23);
1crypto_shorthash_keygen(k: ArrayBuffer): void;
1const k = new ArrayBuffer(sodium.crypto_shorthash_KEYBYTES);
2sodium.crypto_shorthash_keygen(k);
3console.log(
4 "Key:",
5 sodium.sodium_bin2hex(
6 new ArrayBuffer(k.byteLength * 2 + 1),
7 k.byteLength * 2 + 1,
8 k,
9 k.byteLength
10 )
11);
1sodium_pad(paddedBufLenP: ArrayBuffer, buf: ArrayBuffer, unpaddedBufLen: number, blockSize: number, maxBufLen: number): number;
1const paddedBufLenP = new ArrayBuffer(8); // 8 bytes are needed to store a size_t number
2const buf = new ArrayBuffer(64);
3const message = new TextEncoder().encode("Fennec fox");
4new Uint8Array(buf).set(message);
5const unpaddedBufLen = message.byteLength;
6const blockSize = 16;
7sodium.sodium_pad(
8 paddedBufLenP,
9 buf,
10 unpaddedBufLen,
11 blockSize,
12 buf.byteLength
13);
14const paddedBufLen = Number(new DataView(paddedBufLenP).getBigUint64(0, true)); // Safe as long as you are not working with a 9PB data
15console.log("Padded buf:", new Uint8Array(buf.slice(0, paddedBufLen)));
1sodium_unpad(unpaddedBufLenP: ArrayBuffer, buf: ArrayBuffer, paddedBufLen: number, blockSize: number): number;
1const unpaddedBufLenP = new ArrayBuffer(8); // 8 bytes are needed to store a size_t number
2const buf = new Uint8Array([
3 70, 101, 110, 110, 101, 99, 32, 102, 111, 120, 128, 0, 0, 0, 0, 0,
4]).buffer;
5const blockSize = 16;
6sodium.sodium_unpad(unpaddedBufLenP, buf, buf.byteLength, blockSize);
7const unpaddedBufLen = Number(
8 new DataView(unpaddedBufLenP).getBigUint64(0, true) // Safe as long as you are not working with a 9PB data
9);
10console.log("Unpadded buf:", new Uint8Array(buf.slice(0, unpaddedBufLen)));
1sodium_memcmp(b1_: ArrayBuffer, b2_: ArrayBuffer, len: number): number;
1const b1_ = new Uint8Array([7, 7, 1, 2]).buffer; 2const b2_ = new Uint8Array([7, 7, 100, 200]).buffer; 3console.log("isEqual:", sodium.sodium_memcmp(b1_, b2_, 2) === 0);
1sodium_bin2hex(hex: ArrayBuffer, hexMaxLen: number, bin: ArrayBuffer, binLen: number): string;
2sodium_hex2bin(bin: ArrayBuffer, binMaxLen: number, hex: string, hexLen: number, ignore: string | null, binLen: ArrayBuffer, hexEnd: ArrayBuffer | null): number;
1const bin = new Uint8Array([0, 255, 0, 255]).buffer;
2console.log(
3 "Hex:",
4 sodium.sodium_bin2hex(
5 new ArrayBuffer(bin.byteLength * 2 + 1), // Each byte is encoded into two characters, plus one for the null character
6 bin.byteLength * 2 + 1,
7 bin,
8 bin.byteLength
9 )
10);
1const hex = "00ff00ff";
2const bin = new ArrayBuffer(hex.length / 2); // Every two characters fit into a single byte
3sodium.sodium_hex2bin(
4 bin,
5 bin.byteLength,
6 hex,
7 hex.length,
8 null,
9 new ArrayBuffer(8), // 8 bytes are needed to store a size_t number, not used in this example
10 null
11);
12console.log("Binary:", new Uint8Array(bin));
1sodium_base64_encoded_len(binLen: number, variant: number): number;
2sodium_bin2base64(b64: ArrayBuffer, b64MaxLen: number, bin: ArrayBuffer, binLen: number, variant: number): string;
3sodium_base642bin(bin: ArrayBuffer, binMaxLen: number, b64: string, b64Len: number, ignore: string | null, binLen: ArrayBuffer, b64End: ArrayBuffer | null, variant: number): number;
1const variant = sodium.sodium_base64_VARIANT_ORIGINAL; 2const bin = new Uint8Array([0, 255, 0, 255]).buffer; 3const b64Len = sodium.sodium_base64_encoded_len(bin.byteLength, variant); 4console.log( 5 "Base64:", 6 sodium.sodium_bin2base64( 7 new ArrayBuffer(b64Len), 8 b64Len, 9 bin, 10 bin.byteLength, 11 variant 12 ) 13);
1const variant = sodium.sodium_base64_VARIANT_ORIGINAL;
2const b64 = "AP8A/w==";
3const bin = new ArrayBuffer(Math.ceil((b64.length / 4) * 3)); // Bin will take at most (b64.length / 4) * 3 bytes. Use binLen to get the exact length
4const binLen = new ArrayBuffer(8); // 8 bytes are needed to store a size_t number
5sodium.sodium_base642bin(
6 bin,
7 bin.byteLength,
8 b64,
9 b64.length,
10 null,
11 binLen,
12 null,
13 variant
14);
15const binLenAsNumber = Number(new DataView(binLen).getBigUint64(0, true)); // Safe as long as you are not working with a 9PB data
16console.log("Binary:", new Uint8Array(bin.slice(0, binLenAsNumber)));
1sodium_increment(n: ArrayBuffer, nLen: number): void;
2sodium_add(a: ArrayBuffer, b: ArrayBuffer, len: number): void;
3sodium_sub(a: ArrayBuffer, b: ArrayBuffer, len: number): void;
4sodium_compare(b1_: ArrayBuffer, b2_: ArrayBuffer, len: number): number;
1const a = new Uint8Array(16).fill(255, 0, 10).buffer; // a=1208925819614629174706175
2const b = new Uint8Array(16).fill(255, 0, 10).buffer; // b=1208925819614629174706175
3console.log("Comparison", sodium.sodium_compare(a, b, a.byteLength));
4sodium.sodium_increment(a, a.byteLength);
5console.log("Comparison", sodium.sodium_compare(a, b, a.byteLength));
6sodium.sodium_sub(a, b, a.byteLength);
7console.log("Comparison", sodium.sodium_compare(a, b, a.byteLength));
1sodium_is_zero(n: ArrayBuffer, nLen: number): number;
1const n = new ArrayBuffer(8);
2console.log("isZero", sodium.sodium_is_zero(n, n.byteLength) === 1);
1sodium_stackzero(len: number): void;
1sodium.sodium_stackzero(4);
Q: Why functions that take char*
parameters sometimes use ArrayBuffer
and other times use string
?
A: If the input is encoding-dependant an ArrayBuffer
is used and it's the responsibility of the user to choose the desired encoding. You can use TextEncoder to generate an array buffer with UTF-8 encoding. On the other hand if the input is guaranteed to be representable in ASCII then it will be interpreted as ASCII and a string
is used.
If the input is meant to be written into then an ArrayBuffer
must be used since string
s are immutable.
Q: Why are some libsodium functions not implemented?
A: This library aims to provide a 1:1 libsodium compatibility however functions are implemented progressively and per priority and needs. Feel free to submit an issue for prioritization.
PRs are welcome!
No vulnerabilities found.
No security vulnerabilities found.