Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const generateSecret = () => sodium.to_base64(sodium.randombytes_buf(32));
const rand = async (length) => {
await sodiumsumo.ready;
return sodiumsumo.randombytes_buf(length);
}
if (sk && typeof sk === 'object' && !(sk instanceof Buffer)) {
nonce = sk.nonce;
sk = sk.key;
}
let payload, isk;
[ payload ] = iparse(message);
[ sk, nonce ] = cparse(sk, nonce);
if (!sk) { sk = sodium.crypto_secretbox_keygen(); }
isk = sk;
let ciphertext;
try {
nonce = nonce || sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES);
ciphertext = sodium.crypto_secretbox_easy(payload, nonce, isk);
} catch(ex) {
return done(new Error('Libsodium error: ' + ex));
}
return done(null, convert({
alg: 'xsalsa20poly1305',
sk: sk,
payload: payload,
nonce: nonce,
ciphertext: ciphertext
}));
}
export async function randombytes_buf(length: number) {
await sodium.ready;
return sodium.randombytes_buf(length);
}
let payload, isk, ipk;
[ payload ] = iparse(message);
[ sk, pk, nonce ] = cparse(sk, pk, nonce);
if (!sk) {
const keys = sodium.crypto_box_keypair();
sk = keys.privateKey;
pk = keys.publicKey;
}
isk = sk;
ipk = pk;
let ciphertext;
try {
nonce = nonce || sodium.randombytes_buf(sodium.crypto_box_NONCEBYTES);
ciphertext = sodium.crypto_box_easy(payload, nonce, ipk, isk);
} catch(ex) {
return done(new Error('Libsodium error: ' + ex));
}
return done(null, convert({
alg: 'x25519-xsalsa20poly1305',
sk: sk,
pk: pk,
payload: payload,
nonce: nonce,
ciphertext: ciphertext
}));
}