How to use the libsodium-wrappers.crypto_box_NONCEBYTES function in libsodium-wrappers

To help you get started, we’ve selected a few libsodium-wrappers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github EQEmu / Server / wi / network / servertalk_client.js View on Github external
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) {
github samuelmaddock / metastream / packages / metastream-signal-server / src / index.ts View on Github external
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
  }
github maidsafe / safe_examples / dns_example / src / scripts / safe_api / request_manager.js View on Github external
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'));
      }
    }
  };
github EQEmu / Server / wi / network / servertalk_client.js View on Github external
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 {
github TankerHQ / sdk-js / packages / crypto / src / tcrypto.js View on Github external
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);
}
github samuelmaddock / metastream / packages / metastream-app / src / platform / web / authenticate.ts View on Github external
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
  }
github samuelmaddock / metastream / packages / metastream-app / src / platform / web / authenticate.ts View on Github external
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
  }
github multiparty / jiff / lib / jiff-client.js View on Github external
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()+']'};
  }