Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const nonce = () => sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES)
encrypt(key, message)
{
var nonce = _sodium.randombytes_buf(_sodium.crypto_secretbox_NONCEBYTES);
try
{
return _sodium.to_hex(_concat(Uint8Array, nonce, _sodium.crypto_secretbox_easy(message, nonce, _sodium.from_hex(key))));
}
catch(e)
{
this._adapter.log.warn(e.message);
return false;
}
}
notifierCallback('Handshake failed - ' + handshakeResponse.error.description +
'(' + handshakeResponse.error.code + ')');
return;
}
var launcherPublicKey = convertStringAsUnit8Array(handshakeResponse.data.launcher_public_key);
var decryptedSymmKey;
try {
decryptedSymmKey = sodium.crypto_box_open_easy(convertStringAsUnit8Array(handshakeResponse.data.encrypted_symm_key), HandshakeKeys.nonce,
launcherPublicKey, HandshakeKeys.secretKey);
} catch(e) {
log.error('SymmetricKey Decryption failed');
return notifierCallback('SymmetricKey Decryption failed');
}
encryptionNonce = decryptedSymmKey.subarray(0, sodium.crypto_secretbox_NONCEBYTES);
encryptionKey = decryptedSymmKey.subarray(sodium.crypto_secretbox_NONCEBYTES);
connectionManager.setOnDataRecievedListener(onDataReceived);
notifierCallback(null, self);
};
decrypt(key, message)
{
try
{
let cypherText = new Buffer(payload, 'base64');
let nonce = cypherText.slice(0, _sodium.crypto_secretbox_NONCEBYTES);
let encryptionKey = new Buffer(32);
encryptionKey.fill(0);
encryptionKey.write(key);
return _sodium.crypto_secretbox_open_easy(cypherText.slice(_sodium.crypto_secretbox_NONCEBYTES), nonce, encryptionKey, 'text');
}
catch(e)
{
this._adapter.log.warn(e.message);
return false;
}
}
}
log.warn(handshakeResponse.error);
notifierCallback('Handshake failed - ' + handshakeResponse.error.description +
'(' + handshakeResponse.error.code + ')');
return;
}
var launcherPublicKey = convertStringAsUnit8Array(handshakeResponse.data.launcher_public_key);
var decryptedSymmKey;
try {
decryptedSymmKey = sodium.crypto_box_open_easy(convertStringAsUnit8Array(handshakeResponse.data.encrypted_symm_key), HandshakeKeys.nonce,
launcherPublicKey, HandshakeKeys.secretKey);
} catch(e) {
log.error('SymmetricKey Decryption failed');
return notifierCallback('SymmetricKey Decryption failed');
}
encryptionNonce = decryptedSymmKey.subarray(0, sodium.crypto_secretbox_NONCEBYTES);
encryptionKey = decryptedSymmKey.subarray(sodium.crypto_secretbox_NONCEBYTES);
connectionManager.setOnDataRecievedListener(onDataReceived);
notifierCallback(null, self);
};