Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ProcessHello(p) {
this.m_encrypted = false;
this.m_public_key_ours = null;
this.m_public_key_theirs = null;
this.m_private_key_ours = null;
this.m_nonce_ours = null;
this.m_nonce_theirs = null;
this.m_shared_key = null;
try {
var enc = p.readUInt8(0) == 1 ? true : false;
if (enc) {
if (p.length == (1 + sodium.crypto_box_PUBLICKEYBYTES + sodium.crypto_box_NONCEBYTES)) {
this.m_public_key_theirs = p.slice(1, 1 + sodium.crypto_box_PUBLICKEYBYTES);
this.m_nonce_theirs = p.slice(1 + sodium.crypto_box_PUBLICKEYBYTES, 1 + sodium.crypto_box_PUBLICKEYBYTES + sodium.crypto_box_NONCEBYTES);
this.m_encrypted = true;
this.SendHandshake(false);
this.emit('connect');
}
else {
this.emit('error', new Error('Could not process hello, size !=', 1 + sodium.crypto_box_PUBLICKEYBYTES + sodium.crypto_box_NONCEBYTES));
}
} else {
this.SendHandshake(false);
this.emit('connect');
}
} catch(ex) {
private authCheck(client: Client, publicKey: string) {
if (client.status === ClientStatus.Authed) return true
else if (client.status === ClientStatus.PendingAuth) return false
else if (!isValidRoom(publicKey)) return false
const nonce = sodium.randombytes_buf(sodium.crypto_box_NONCEBYTES)
const box = sodium.crypto_box_seal(nonce, sodium.from_hex(publicKey))
const challenge = sodium.to_base64(box, sodium.base64_variants.URLSAFE_NO_PADDING)
client.status = ClientStatus.PendingAuth
client.authSecret = nonce
this.sendTo(client, {
t: MessageType.AuthChallenge,
c: challenge
})
return false
}
init: function() {
try {
var keyPair = sodium.crypto_box_keypair();
this.publicKey = keyPair.publicKey;
this.secretKey = keyPair.privateKey;
this.nonce = sodium.randombytes_buf(sodium.crypto_box_NONCEBYTES);
} catch(e) {
notifierCallback(new Error('Failed to initialise HandshakeKeys - libsodium error'));
}
}
};
SendHandshake() {
var handshake;
if(this.m_encrypted) {
var keypair = sodium.crypto_box_keypair();
this.m_public_key_ours = keypair.publicKey;
this.m_private_key_ours = keypair.privateKey;
this.m_nonce_ours = Buffer.from(sodium.randombytes_buf(sodium.crypto_box_NONCEBYTES));
this.m_shared_key = sodium.crypto_box_beforenm(this.m_public_key_theirs, this.m_private_key_ours);
this.m_public_key_theirs = null;
this.m_private_key_ours = null;
var message = Buffer.alloc(this.m_identifier.length + this.m_credentials.length + 2);
message.write(this.m_identifier, 0);
message.write(this.m_credentials, this.m_identifier.length + 1);
var ciphertext = sodium.crypto_box_easy_afternm(message, this.m_nonce_ours, this.m_shared_key);
handshake = Buffer.concat([Buffer.from(this.m_public_key_ours), Buffer.from(this.m_nonce_ours), Buffer.from(ciphertext)], sodium.crypto_box_PUBLICKEYBYTES + sodium.crypto_box_NONCEBYTES + ciphertext.length);
this.IncrementUint64(this.m_nonce_ours);
this.m_public_key_ours = null;
} else {
export function asymDecrypt(cipherText: Uint8Array, publicKey: Uint8Array, privKey: Uint8Array): Uint8Array {
const nonceStart = cipherText.length - sodium.crypto_box_NONCEBYTES;
const nonce = cipherText.subarray(nonceStart);
const cipherSliced = cipherText.subarray(0, nonceStart);
return sodium.crypto_box_open_easy(cipherSliced, nonce, publicKey, privKey);
}
function decrypt(data: Uint8Array) {
if (!sharedKey) return null
const nonce = data.slice(0, sodium.crypto_box_NONCEBYTES)
const box = data.slice(sodium.crypto_box_NONCEBYTES, data.length)
const msg = crypto.decrypt(box, nonce, sharedKey)
return msg
}
function decrypt(data: Uint8Array) {
if (!sharedKey) return null
const nonce = data.slice(0, sodium.crypto_box_NONCEBYTES)
const box = data.slice(sodium.crypto_box_NONCEBYTES, data.length)
const msg = crypto.decrypt(box, nonce, sharedKey)
return msg
}
function encrypt_and_sign(message, encryption_public_key, signing_private_key, operation_type) {
if(operation_type == 'share' || operation_type == 'open') message = message.toString(10);
var nonce = sodium.randombytes_buf(sodium.crypto_box_NONCEBYTES);
var cipher = sodium.crypto_box_easy(message, nonce, encryption_public_key, signing_private_key);
return { "nonce": '['+nonce.toString()+']', "cipher": '['+cipher.toString()+']'};
}