How to use the tweetnacl.secretbox 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 brave / sync / lib / crypto.js View on Github external
module.exports.encrypt = function (message/* : Uint8Array */,
  secretboxKey/* : Uint8Array */, counter/* : number */,
  nonceBytes/* : Uint8Array */) {
  const nonce = module.exports.getNonce(counter, nonceBytes)
  return {
    nonce,
    ciphertext: nacl.secretbox(message, nonce, secretboxKey)
  }
}
github iden3 / iden3js / src / key-container / kc-utils.js View on Github external
function encrypt(key: string, msg: string): string {
  const newNonce = () => nacl.randomBytes(nacl.secretbox.nonceLength);
  const keyUint8Array = nacl.util.decodeBase64(key);
  const nonce = newNonce();
  const messageUint8 = nacl.util.decodeUTF8(msg);
  const box = nacl.secretbox(messageUint8, nonce, keyUint8Array);
  const fullMessage = new Uint8Array(nonce.length + box.length);

  fullMessage.set(nonce);
  fullMessage.set(box, nonce.length);

  // base64 full message;
  return nacl.util.encodeBase64(fullMessage);
}
github 3box / muport-core-js / src / keyring.js View on Github external
const symEncryptBase = (msg, symKey, nonce) => {
  nonce = nonce || randomNonce()
  if (typeof msg === 'string') {
    msg = nacl.util.decodeUTF8(msg)
  }

  const ciphertext = nacl.secretbox(msg, nonce, symKey)

  return {
    nonce: nacl.util.encodeBase64(nonce),
    ciphertext: nacl.util.encodeBase64(ciphertext)
  }
}
github 3box / 3box-js / src / 3id / keyring.js View on Github external
const symEncryptBase = (msg, symKey, nonce) => {
  nonce = nonce || randomNonce()
  if (typeof msg === 'string') {
    msg = nacl.util.decodeUTF8(msg)
  }
  const ciphertext = nacl.secretbox(msg, nonce, symKey)
  return {
    nonce: nacl.util.encodeBase64(nonce),
    ciphertext: nacl.util.encodeBase64(ciphertext)
  }
}
github stellarport / stellar-keystore / src / index.js View on Github external
.then(key => {
                const nonce = this._randomNonce();
                const ciphertext = nacl.secretbox(naclUtil.decodeUTF8(newKeypair.secret()), nonce, key);
                return {
                    keypair: newKeypair,
                    walletData: {
                        version,
                        address: newKeypair.publicKey(),
                        crypto: {
                            ciphertext: naclUtil.encodeBase64(ciphertext),
                            nonce: naclUtil.encodeBase64(nonce),
                            salt: naclUtil.encodeBase64(salt),
                            scryptOptions: latestScryptOptions
                        }
                    }
                };
            });
github pusher / pusher-http-node / lib / events.js View on Github external
function encrypt(pusher, channel, data) {
  if (pusher.config.encryptionMasterKey === undefined) {
    throw new Error("Set encryptionMasterKey before triggering events on encrypted channels");
  }

  var nonceBytes = nacl.randomBytes(24);

  var ciphertextBytes = nacl.secretbox(
    naclUtil.decodeUTF8(JSON.stringify(data)),
    nonceBytes,
    pusher.channelSharedSecret(channel));

  return JSON.stringify({
    nonce: naclUtil.encodeBase64(nonceBytes),
    ciphertext: naclUtil.encodeBase64(ciphertextBytes)
  });
}