How to use the libsodium-wrappers.randombytes_buf 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 sjudson / paseto.js / lib / protocol / V2.js View on Github external
function aeadEncrypt(key, header, plaintext, footer, nonce) {

  // build nonce

  const nlen   = sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES;
  const nkey   = nonce || sodium.randombytes_buf(nlen);
  const _nonce = sodium.crypto_generichash(nlen, plaintext, nkey);

  nonce = Buffer.from(_nonce);

  // encrypt

  let ad;
  try {
    ad = utils.pae(header, nonce, footer);
  } catch (ex) {
    return done(ex);
  }

  const _ciphertext = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(plaintext, ad, null, nonce, key.raw());
  const ciphertext  = Buffer.from(_ciphertext);
github Zefau / ioBroker.roomba / lib / encryption.js View on Github external
encrypt(key, message)
	{
		var nonce = _sodium.randombytes_buf(_sodium.crypto_secretbox_NONCEBYTES);
		try
		{
			return '';
			//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;
		}
	}
github ZeusWPI / MOZAIC / client / src / networking / Handshaker.ts View on Github external
constructor(transport: Transport, connection: Connection) {
        this.transport = transport;
        this.connection = connection;

        this.clientNonce = sodium.randombytes_buf(NONCE_NUM_BYTES);
        this.kxKeypair = sodium.crypto_kx_keypair();
    }
github Picolab / pico-engine / packages / pico-engine-core / src / modules / indy.js View on Github external
header: {
          kid: bs58.encode(targetVKey),
          sender: encSender ? b64url(encSender) : null,
          iv: nonce ? b64url(nonce) : null
        }
      }
    })

    const recipsB64 = b64url(JSON.stringify({
      enc: 'xchacha20poly1305_ietf',
      typ: 'JWM/1.0',
      alg: sender ? 'Authcrypt' : 'Anoncrypt',
      recipients
    }))

    const iv = sodium.randombytes_buf(sodium.crypto_aead_chacha20poly1305_ietf_NPUBBYTES)
    const out = sodium.crypto_aead_chacha20poly1305_ietf_encrypt_detached(message, recipsB64, null, iv, cek)

    return JSON.stringify({
      protected: recipsB64,
      iv: b64url(iv),
      ciphertext: b64url(out.ciphertext),
      tag: b64url(out.mac)
    })
  })