How to use the tweetnacl.box function in tweetnacl

To help you get started, we’ve selected a few tweetnacl 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 hillstreetlabs / kimono / js / src / crypto.ts View on Github external
export function encryptSecretForRevealer(
  messageSecret: Uint8Array,
  nonce: Uint8Array,
  revealerPublicKey: Uint8Array,
  secretKey: Uint8Array
) {
  return nacl.box(messageSecret, nonce, revealerPublicKey, secretKey);
}
github ninjadotorg / handshake-app / src / pages / Chat / Firechat.jsx View on Github external
encryptMessage(message, nonce, publicKey) {
    try {
      if (!(publicKey instanceof Uint8Array)) {
        publicKey = naclUtil.decodeBase64(publicKey);
      }

      if (!(nonce instanceof Uint8Array)) {
        nonce = naclUtil.decodeUTF8(nonce);
      }

      if (!(message instanceof Uint8Array)) {
        message = naclUtil.decodeUTF8(message);
      }

      return naclUtil.encodeBase64(nacl.box(message, nonce, publicKey, this.encryptionKeyPair.secretKey));
    } catch (e) {
      return '';
    }
  }
github jo / pouch-box / lib / box.js View on Github external
.reduce(function(memo, publicKey) {
        var nonce = nacl.randomBytes(nacl.box.nonceLength)
        
        memo[nacl.util.encodeBase64(publicKey)] = {
          nonce: nacl.util.encodeBase64(nonce),
          encryptedKey: nacl.util.encodeBase64(nacl.box(
            key,
            nonce,
            publicKey,
            ephemeralKey.secretKey
          ))
        }

      return memo
    }, {})
github maxogden / emojilock / minilock.js View on Github external
ids.forEach(function (id, index) {
    debug('Adding recipient ' + id)

    var nonce = nacl.randomBytes(24)
    var publicKey = publicKeyFromId(id)

    debug('Using nonce ' + hex(nonce))

    var decryptInfo = {
      senderID: senderInfo.id,
      recipientID: id,
      fileInfo: fileInfo
    }

    decryptInfo.fileInfo = nacl.util.encodeBase64(nacl.box(
      nacl.util.decodeUTF8(JSON.stringify(decryptInfo.fileInfo)),
      nonce,
      publicKey,
      senderInfo.secretKey
    ))

    decryptInfo = nacl.util.encodeBase64(nacl.box(
      nacl.util.decodeUTF8(JSON.stringify(decryptInfo)),
      nonce,
      publicKey,
      ephemeral.secretKey
    ))

    header.decryptInfo[nacl.util.encodeBase64(nonce)] = decryptInfo
  })
github 3box / identity-wallet-js / src / keyring.js View on Github external
asymEncrypt (msg, toPublic, { nonce } = {}) {
    nonce = nonce || Keyring.randomNonce()
    toPublic = nacl.util.decodeBase64(toPublic)
    if (typeof msg === 'string') {
      msg = nacl.util.decodeUTF8(msg)
    }
    const ephemneralKeypair = nacl.box.keyPair()
    const ciphertext = nacl.box(msg, nonce, toPublic, ephemneralKeypair.secretKey)
    return {
      nonce: nacl.util.encodeBase64(nonce),
      ephemeralFrom: nacl.util.encodeBase64(ephemneralKeypair.publicKey),
      ciphertext: nacl.util.encodeBase64(ciphertext)
    }
  }
github chr15m / bugout / index.js View on Github external
function encryptPacket(bugout, pk, packet) {
  if (bugout.peers[bugout.address(pk)]) {
    var nonce = nacl.randomBytes(nacl.box.nonceLength);
    packet = bencode.encode({
      "n": nonce,
      "ek": bs58.encode(Buffer.from(bugout.keyPairEncrypt.publicKey)),
      "e": nacl.box(packet, nonce, bs58.decode(bugout.peers[bugout.address(pk)].ek), bugout.keyPairEncrypt.secretKey),
    });
  } else {
    throw bugout.address(pk) + " not seen - no encryption key.";
  }
  return packet;
}
github queicherius / asymmetric-crypto / src / index.js View on Github external
function encrypt (data, theirPublicKey, mySecretKey) {
  data = naclUtil.decodeUTF8(data)
  theirPublicKey = convert.publicKey(naclUtil.decodeBase64(theirPublicKey))
  mySecretKey = convert.secretKey(naclUtil.decodeBase64(mySecretKey))

  const nonce = nacl.randomBytes(nacl.box.nonceLength)

  data = nacl.box(data, nonce, theirPublicKey, mySecretKey)

  return {
    data: naclUtil.encodeBase64(data),
    nonce: naclUtil.encodeBase64(nonce)
  }
}
github NullVoxPopuli / tanqueReact / js / utility / nacl.js View on Github external
export function encryptFor(messageWithoutNonce, theirPublicKey, mySecretKey) {
  const theirPublicKeyUint8Array = convertBase64StringToUint8Array(theirPublicKey);
  const mySecretKeyUint8Array = convertBase64StringToUint8Array(mySecretKey);

  const nonce = newNonce();
  const message = convertStringToUint8Array(messageWithoutNonce);
  const box = NaCl.box(
    message,
    nonce,
    theirPublicKeyUint8Array,
    mySecretKeyUint8Array);

  const fullMessage = new Uint8Array(nonce.length + box.length);
  fullMessage.set(nonce);
  fullMessage.set(message, nonce.length);

  const base64FullMessage = convertUint8ArrayToBase64String(fullMessage);
  return base64FullMessage;
}
github LiskHQ / lisk-sdk / elements / lisk-cryptography / src / nacl / slow.ts View on Github external
export const box: NaclInterface['box'] = (
	messageInBytes,
	nonceInBytes,
	convertedPublicKey,
	convertedPrivateKey,
) =>
	Buffer.from(
		tweetnacl.box(
			messageInBytes,
			nonceInBytes,
			convertedPublicKey,
			convertedPrivateKey,
		),
	);