29 lines
1.4 KiB
TypeScript
29 lines
1.4 KiB
TypeScript
import * as s from 'react-native-libsodium';
|
|
import type { CryptoBackend } from '@chat-app/shared/crypto';
|
|
|
|
// react-native-libsodium re-exports libsodium-wrappers' API shape, so
|
|
// this adapter is the synchronous twin of the desktop one
|
|
// (`apps/desktop/src/lib/cryptoBackend.ts`). No WASM warm-up gate is
|
|
// needed — the native module is ready as soon as the module loads.
|
|
export function createLibsodiumBackend(): CryptoBackend {
|
|
return {
|
|
name: 'react-native-libsodium',
|
|
nonceLength: s.crypto_box_NONCEBYTES,
|
|
publicKeyLength: s.crypto_box_PUBLICKEYBYTES,
|
|
privateKeyLength: s.crypto_box_SECRETKEYBYTES,
|
|
secretboxKeyLength: s.crypto_secretbox_KEYBYTES,
|
|
secretboxNonceLength: s.crypto_secretbox_NONCEBYTES,
|
|
randomBytes: (n) => s.randombytes_buf(n),
|
|
generateKeyPair: () => {
|
|
const kp = s.crypto_box_keypair();
|
|
return { publicKey: kp.publicKey, privateKey: kp.privateKey };
|
|
},
|
|
box: (plaintext, nonce, recipientPublicKey, senderPrivateKey) =>
|
|
s.crypto_box_easy(plaintext, nonce, recipientPublicKey, senderPrivateKey),
|
|
boxOpen: (ciphertext, nonce, senderPublicKey, recipientPrivateKey) =>
|
|
s.crypto_box_open_easy(ciphertext, nonce, senderPublicKey, recipientPrivateKey),
|
|
secretbox: (plaintext, nonce, key) => s.crypto_secretbox_easy(plaintext, nonce, key),
|
|
secretboxOpen: (ciphertext, nonce, key) => s.crypto_secretbox_open_easy(ciphertext, nonce, key),
|
|
};
|
|
}
|