Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports.encrypt = function (message/* : Uint8Array */,
secretboxKey/* : Uint8Array */, counter/* : number */,
nonceBytes/* : Uint8Array */) {
const nonce = module.exports.getNonce(counter, nonceBytes)
return {
nonce,
ciphertext: nacl.secretbox(message, nonce, secretboxKey)
}
}
function encrypt(key: string, msg: string): string {
const newNonce = () => nacl.randomBytes(nacl.secretbox.nonceLength);
const keyUint8Array = nacl.util.decodeBase64(key);
const nonce = newNonce();
const messageUint8 = nacl.util.decodeUTF8(msg);
const box = nacl.secretbox(messageUint8, nonce, keyUint8Array);
const fullMessage = new Uint8Array(nonce.length + box.length);
fullMessage.set(nonce);
fullMessage.set(box, nonce.length);
// base64 full message;
return nacl.util.encodeBase64(fullMessage);
}
const symEncryptBase = (msg, symKey, nonce) => {
nonce = nonce || randomNonce()
if (typeof msg === 'string') {
msg = nacl.util.decodeUTF8(msg)
}
const ciphertext = nacl.secretbox(msg, nonce, symKey)
return {
nonce: nacl.util.encodeBase64(nonce),
ciphertext: nacl.util.encodeBase64(ciphertext)
}
}
const symEncryptBase = (msg, symKey, nonce) => {
nonce = nonce || randomNonce()
if (typeof msg === 'string') {
msg = nacl.util.decodeUTF8(msg)
}
const ciphertext = nacl.secretbox(msg, nonce, symKey)
return {
nonce: nacl.util.encodeBase64(nonce),
ciphertext: nacl.util.encodeBase64(ciphertext)
}
}
.then(key => {
const nonce = this._randomNonce();
const ciphertext = nacl.secretbox(naclUtil.decodeUTF8(newKeypair.secret()), nonce, key);
return {
keypair: newKeypair,
walletData: {
version,
address: newKeypair.publicKey(),
crypto: {
ciphertext: naclUtil.encodeBase64(ciphertext),
nonce: naclUtil.encodeBase64(nonce),
salt: naclUtil.encodeBase64(salt),
scryptOptions: latestScryptOptions
}
}
};
});
function encrypt(pusher, channel, data) {
if (pusher.config.encryptionMasterKey === undefined) {
throw new Error("Set encryptionMasterKey before triggering events on encrypted channels");
}
var nonceBytes = nacl.randomBytes(24);
var ciphertextBytes = nacl.secretbox(
naclUtil.decodeUTF8(JSON.stringify(data)),
nonceBytes,
pusher.channelSharedSecret(channel));
return JSON.stringify({
nonce: naclUtil.encodeBase64(nonceBytes),
ciphertext: naclUtil.encodeBase64(ciphertextBytes)
});
}