Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
await fs.writeFile(skeyPath, localKeyPair.secretKey)
} catch (e) {}
} else {
try {
localKeyPair = {
publicKey: await fs.readFile(keyPath),
secretKey: await fs.readFile(skeyPath)
}
} catch (e) {
localKeyPair = keyPair()
}
}
// convert old sodium-native keys to use new length compatible with libsodium-wrappers
if (localKeyPair.secretKey.length > sodium.crypto_box_PUBLICKEYBYTES) {
localKeyPair.secretKey = localKeyPair.secretKey.slice(0, sodium.crypto_box_PUBLICKEYBYTES)
}
return localKeyPair
}
await fs.writeFile(keyPath, localKeyPair.publicKey)
await fs.writeFile(skeyPath, localKeyPair.secretKey)
} catch (e) {}
} else {
try {
localKeyPair = {
publicKey: await fs.readFile(keyPath),
secretKey: await fs.readFile(skeyPath)
}
} catch (e) {
localKeyPair = keyPair()
}
}
// convert old sodium-native keys to use new length compatible with libsodium-wrappers
if (localKeyPair.secretKey.length > sodium.crypto_box_PUBLICKEYBYTES) {
localKeyPair.secretKey = localKeyPair.secretKey.slice(0, sodium.crypto_box_PUBLICKEYBYTES)
}
return localKeyPair
}
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) {
this.emit('error', new Error(ex));
}
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) {
this.emit('error', new Error(ex));
}
}
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 {
handshake = Buffer.alloc(this.m_identifier.length + this.m_credentials.length + 2);
handshake.write(this.m_identifier, 0);
handshake.write(this.m_credentials, this.m_identifier.length() + 1);
}
this.InternalSend(ServertalkPacketType.ServertalkClientHandshake, handshake);
}