38 lines
1.7 KiB
TypeScript
38 lines
1.7 KiB
TypeScript
import type { CryptoBackend } from '@chat-app/shared/crypto';
|
|
import _sodium from 'libsodium-wrappers-sumo';
|
|
|
|
// Build the libsodium-backed CryptoBackend. Awaits the WASM ready-gate once,
|
|
// then returns a synchronous implementation of the CryptoBackend contract.
|
|
export async function createLibsodiumBackend(): Promise<CryptoBackend> {
|
|
await _sodium.ready;
|
|
const s = _sodium;
|
|
|
|
return {
|
|
name: 'libsodium-wrappers',
|
|
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: number) => 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),
|
|
pwhashConsts: {
|
|
OPSLIMIT_MODERATE: s.crypto_pwhash_OPSLIMIT_MODERATE,
|
|
MEMLIMIT_MODERATE: s.crypto_pwhash_MEMLIMIT_MODERATE,
|
|
ALG_ARGON2ID13: s.crypto_pwhash_ALG_ARGON2ID13,
|
|
},
|
|
pwhash: (outLen, password, salt, opslimit, memlimit, alg) =>
|
|
s.crypto_pwhash(outLen, password, salt, opslimit, memlimit, alg),
|
|
scalarMultBase: (priv) => s.crypto_scalarmult_base(priv),
|
|
};
|
|
}
|