Cloud Defense Logo

Products

Solutions

Company

Book A Live Demo

Top 10 Examples of "libsodium-wrappers in functional component" in JavaScript

Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'libsodium-wrappers' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.

sodium_promise.then(function() {
        msg = JSON.parse(msg);
        if(jiff.id == null)
          jiff.id = msg.party_id;

        if(jiff.party_count == null)
          jiff.party_count = msg.party_count;

        if(jiff.secret_key == null || jiff.public_key == null) {
          // this party's public and secret key
          var genkey = sodium.crypto_box_keypair();
          jiff.secret_key = genkey.privateKey;
          jiff.public_key = genkey.publicKey;
        }

        jiff.socket.emit("public_key", '['+jiff.public_key.toString()+']');
        // Now: (1) this party is connect (2) server (and other parties) know this public key
        // Resend all pending messages
        jiff.socket.resend_mailbox();

        jiff.execute_wait_callbacks();
      });
    });
return Promise.all([PromiseFileReader.readAsArrayBuffer(file), sodium.ready]).then((res) => {
		let buffer = res[0];
		res = null;

		let key = sodium.crypto_secretstream_xchacha20poly1305_keygen();
		let { state, header } = sodium.crypto_secretstream_xchacha20poly1305_init_push(key);

		// encrypt the metadata
		let piecesCount = Math.ceil(buffer.byteLength / FILE_CHUNK_SIZE);
		let encryptedMetadata = {
			filename: file.name,
			size: file.size,
			pieces: piecesCount,
		};
		let metadataBuffer = sodium.from_string(JSON.stringify(encryptedMetadata));
		let metadataEncrypted = sodium.crypto_secretstream_xchacha20poly1305_push(
			state,
			metadataBuffer,
			null,
			sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL
		);
		metadataBuffer = null;

		// then split the files into pieces
		let promise = new Promise((resolve, reject) => {
			let pieces = [];
			let offset = 0;
			let chunk = () => {
				console.log(`Piece ${offset + 1}/${piecesCount}`);
				let isFinal = offset === piecesCount - 1;
				let length = FILE_CHUNK_SIZE;
function sign(data, key, footer, cb) {
  footer = footer || '';

  const self = this;
  const done = utils.ret(cb);

  if (!(key.protocol() instanceof V2)) {
    return done(new InvalidVersionError('The given key is not intended for this version of PASETO.'));
  }

  return sodium.ready.then(() => {

    const header = utils.public(self);

    [ data, footer ] = (utils.parse('utf-8'))(data, footer);

    // sign

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

    const _signature = sodium.crypto_sign_detached(payload, key.raw());
    const signature  = Buffer.from(_signature);
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);
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);

  // format
return sodium.ready.then(() => {

    const header = utils.public(self);

    [ data, footer ] = (utils.parse('utf-8'))(data, footer);

    // sign

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

    const _signature = sodium.crypto_sign_detached(payload, key.raw());
    const signature  = Buffer.from(_signature);

    // format

    const token = header.toString('utf-8') + utils.toB64URLSafe(Buffer.concat([ data, signature ]));

    return (!Buffer.byteLength(footer))
      ? done(null, token)
      : done(null, token + '.' + utils.toB64URLSafe(footer));
  });
}
case 'Authcrypt':
        if (!senderVK) {
          throw new Error('Sender public key not provided in Authcrypt message')
        }
        break
      case 'Anoncrypt':
        break
      default:
        throw new Error(`Unsupported pack algorithm: ${recipsOuter.alg}`)
    }

    const ciphertext = b64dec(wrapper.ciphertext)
    nonce = b64dec(wrapper.iv)
    const mac = b64dec(wrapper.tag)

    const message = sodium.to_string(
      sodium.crypto_aead_chacha20poly1305_ietf_decrypt_detached(
        null, // nsec
        ciphertext,
        mac,
        wrapper.protected, // ad
        nonce, // npub
        cek
      )
    )

    return {
      message,
      sender_key: senderVK,
      recipient_key: recipient.header.kid
    }
  })
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
}
const nlen  = sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES;
  const nonce = Buffer.from(payload).slice(0, nlen);

  // decrypt and verify

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

  const ciphertext = Buffer.from(payload).slice(nlen, plen);

  const _plaintext = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(null, ciphertext, ad, nonce, key.raw());
  const plaintext  = Buffer.from(_plaintext);

  // format

  return plaintext.toString('utf-8');
}

Is your System Free of Underlying Vulnerabilities?
Find Out Now