Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// @ts-ignore
.hmac(hashjs.sha256, derivedKey, 'hex')
.update(
Buffer.concat([
derivedKey.slice(16, 32),
ciphertext,
iv,
Buffer.from(ALGO_IDENTIFIER),
]),
'hex',
)
.digest('hex');
// we need to do a byte-by-byte comparison to avoid non-constant time side
// channel attacks.
if (!bytes.isEqual(mac.toUpperCase(), keystore.crypto.mac.toUpperCase())) {
return Promise.reject('Failed to decrypt.');
}
const cipher = new aes.ModeOfOperation.ctr(
derivedKey.slice(0, 16),
new aes.Counter(iv),
);
return Buffer.from(cipher.decrypt(ciphertext)).toString('hex');
};