Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
= ({ 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)
}
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 => {
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);
};
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);
};
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);
},
};
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);
},
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),
}
}
}