How to use the libsodium-wrappers-sumo.randombytes_buf function in libsodium-wrappers-sumo

To help you get started, we’ve selected a few libsodium-wrappers-sumo 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 TankerHQ / quickstart-examples / server / src / auth.js View on Github external
const generateSecret = () => sodium.to_base64(sodium.randombytes_buf(32));
github Cryptonomic / ConseilJS / src / utils / WrapperWrapper.js View on Github external
const rand = async (length) => {
    await sodiumsumo.ready;
    return sodiumsumo.randombytes_buf(length);
}
github auth0 / magic / magic.js View on Github external
if (sk && typeof sk === 'object' && !(sk instanceof Buffer)) {
    nonce = sk.nonce;
    sk    = sk.key;
  }

  let payload, isk;
  [ payload ]   = iparse(message);
  [ sk, nonce ] = cparse(sk, nonce);

  if (!sk) { sk = sodium.crypto_secretbox_keygen(); }

  isk = sk;

  let ciphertext;
  try {
    nonce      = nonce || sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES);
    ciphertext = sodium.crypto_secretbox_easy(payload, nonce, isk);
  } catch(ex) {
    return done(new Error('Libsodium error: ' + ex));
  }

  return done(null, convert({
    alg:        'xsalsa20poly1305',
    sk:         sk,
    payload:    payload,
    nonce:      nonce,
    ciphertext: ciphertext
  }));
}
github kryptco / kr-u2f / src / crypto.ts View on Github external
export async function randombytes_buf(length: number) {
    await sodium.ready;
    return sodium.randombytes_buf(length);
}
github auth0 / magic / magic.js View on Github external
let payload, isk, ipk;
  [ payload ]       = iparse(message);
  [ sk, pk, nonce ] = cparse(sk, pk, nonce);

  if (!sk) {
    const keys = sodium.crypto_box_keypair();
    sk = keys.privateKey;
    pk = keys.publicKey;
  }

  isk = sk;
  ipk = pk;

  let ciphertext;
  try {
    nonce      = nonce || sodium.randombytes_buf(sodium.crypto_box_NONCEBYTES);
    ciphertext = sodium.crypto_box_easy(payload, nonce, ipk, isk);
  } catch(ex) {
    return done(new Error('Libsodium error: ' + ex));
  }

  return done(null, convert({
    alg:        'x25519-xsalsa20poly1305',
    sk:         sk,
    pk:         pk,
    payload:    payload,
    nonce:      nonce,
    ciphertext: ciphertext
  }));
}