How to use the tweetnacl.secretbox.open 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 liamgriffiths / tmpnote / src / lib / crypto.js View on Github external
= ({ cipher, secret }) => {
  // convert b64 secret to nonce + key typed arrays
  const [nonce, key] = split(NONCE_LEN, decodeBase64(secret))

  // convert b64 cipher to typed array "box", then open it
  const box = decodeBase64(cipher)
  const bytes = secretbox.open(box, nonce, key)

  // return the utf8 message
  return encodeUTF8(bytes)
}
github pusher / pusher-js / src / core / channels / encrypted_channel.ts View on Github external
let minLength =
          naclSecretbox.overheadLength + naclSecretbox.nonceLength;
        if (encryptedData.length < minLength) {
          throw new Error(
            'encrypted payload too short',
          );
        }
        let parts = encryptedData.split(':');
        if (parts.length != 3) {
          throw new Error('unexpected data format');
        }

        let nonce = decodeBase64(parts[1]);
        let cipherText = decodeBase64(parts[2]);

        let bytes = naclSecretbox.open(cipherText, nonce, key);
        if (bytes === null) {
          throw new Error('probably invalid key');
        }
        let str = encodeUTF8(bytes);
        let decryptedData;
        try {
          decryptedData = JSON.parse(str);
        } catch (e) {}
        return decryptedData || str;
      })
      .catch(err => {
github olymp / olymp / external / crypt / tweetnacl-v1.es6 View on Github external
export const decrypt = (messageWithNonce, key) => {
  const keyUint8Array = decodeBase64(key);
  const messageWithNonceAsUint8Array = decodeBase64(messageWithNonce);
  const nonce = messageWithNonceAsUint8Array.slice(0, secretbox.nonceLength);
  const message = messageWithNonceAsUint8Array.slice(
    secretbox.nonceLength,
    messageWithNonce.length
  );

  const decrypted = secretbox.open(message, nonce, keyUint8Array);

  if (!decrypted) {
    throw new Error('Could not decrypt message');
  }

  const base64DecryptedMessage = encodeUTF8(decrypted);
  return JSON.parse(base64DecryptedMessage);
};
github opencollective / opencollective-api / server / lib / encryption.js View on Github external
export const decrypt = (buffWithNonce, key) => {
  const keyUint8Array = decodeBase64(key);
  const nonce = buffWithNonce.slice(0, nonceLength);
  const message = buffWithNonce.slice(nonceLength, buffWithNonce.length);

  const decrypted = secretbox.open(message, nonce, keyUint8Array);

  if (!decrypted) {
    throw new Error('Could not decrypt message');
  }

  return encodeUTF8(decrypted);
};
github N3TC4T / artunis-mobile / src / libs / crypto.js View on Github external
decrypt: async (box: string, seed: string): string => {
        const seedMD5Unit8 = SHA256(seed);
        const boxUint8 = Base64.decode(box);
        const nonce = new Uint8Array(Array.prototype.slice.call(boxUint8, 0, secretbox.nonceLength));
        const message = new Uint8Array(Array.prototype.slice.call(boxUint8, secretbox.nonceLength, box.length));


        const plainSecret = secretbox.open(message, nonce, seedMD5Unit8);

        if (!plainSecret) {
            return false;
        }

        return TextEncoding.toString(plainSecret);
    },
};
github N3TC4T / artunis-mobile / src / libs / vault.js View on Github external
open: async (type: string, password: string): string => {
        const encPassword = SHA256(password);
        const EncVault = await Vault.retrieve(type);
        const seed = await secretbox.open(
            Base64.decode(EncVault.password),
            Base64.decode(EncVault.username),
            encPassword,
        );
        if (!seed) {
            return false;
        }
        return TextEncoding.toString(seed);
    },
github fidm / quic / src / internal / crypto.ts View on Github external
decode (buf: Buffer): { ip: string, ts: Date } {
    if (buf.length <= secretbox.nonceLength) {
      throw new Error('Invalid SourceToken buffer to decode')
    }
    const nonce = buf.slice(buf.length - secretbox.nonceLength)
    const data = secretbox.open(buf.slice(0, buf.length - secretbox.nonceLength), nonce, this.key)
    if (data == null) {
      throw new Error('SourceToken verify failured')
    }
    const captures = ASN1.parseDERWithTemplate(
      Buffer.from(data.buffer as ArrayBuffer, data.byteOffset, data.length), tokenValidator)
    return {
      ip: bytesToIP(captures.ip.bytes),
      ts: new Date((captures.ts.value as number) * 1000),
    }
  }
}