crypto#createDecipheriv JavaScript Examples
The following examples show how to use
crypto#createDecipheriv.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.js From lx-music-mobile with Apache License 2.0 | 6 votes |
aesDecrypt = (text, key, iv) => {
const decipher = createDecipheriv('aes-128-cbc', Buffer.from(key, 'base64'), Buffer.from(iv, 'base64'))
return Buffer.concat([decipher.update(Buffer.from(text, 'base64')), decipher.final()]).toString()
}
Example #2
Source File: nexoCrypto.js From Lynx with MIT License | 6 votes |
NexoCrypto = /** @class */ (function () {
function NexoCrypto() {
}
NexoCrypto.encrypt = function (messageHeader, saleToPoiMessageJson, securityKey) {
var derivedKey = NexoDerivedKeyGenerator.deriveKeyMaterial(securityKey.passphrase);
var saleToPoiMessageByteArray = Buffer.from(saleToPoiMessageJson, "ascii");
var ivNonce = NexoCrypto.generateRandomIvNonce();
var encryptedSaleToPoiMessage = NexoCrypto.crypt(saleToPoiMessageByteArray, derivedKey, ivNonce, Modes.ENCRYPT);
var encryptedSaleToPoiMessageHmac = NexoCrypto.hmac(saleToPoiMessageByteArray, derivedKey);
var securityTrailer = {
adyenCryptoVersion: securityKey.adyenCryptoVersion,
hmac: encryptedSaleToPoiMessageHmac.toString("base64"),
keyIdentifier: securityKey.keyIdentifier,
keyVersion: securityKey.keyVersion,
nonce: ivNonce.toString("base64"),
};
return {
messageHeader: messageHeader,
nexoBlob: encryptedSaleToPoiMessage.toString("base64"),
securityTrailer: securityTrailer,
};
};
NexoCrypto.prototype.decrypt = function (saleToPoiSecureMessage, securityKey) {
NexoCrypto.validateSecurityKey(securityKey);
var encryptedSaleToPoiMessageByteArray = Buffer.from(saleToPoiSecureMessage.nexoBlob, "base64");
var derivedKey = NexoDerivedKeyGenerator.deriveKeyMaterial(securityKey.passphrase);
var ivNonce = Buffer.from(saleToPoiSecureMessage.securityTrailer.nonce, "base64");
var decryptedSaleToPoiMessageByteArray = NexoCrypto.crypt(encryptedSaleToPoiMessageByteArray, derivedKey, ivNonce, Modes.DECRYPT);
var receivedHmac = Buffer.from(saleToPoiSecureMessage.securityTrailer.hmac, "base64");
this.validateHmac(receivedHmac, decryptedSaleToPoiMessageByteArray, derivedKey);
return decryptedSaleToPoiMessageByteArray.toString("ascii");
};
NexoCrypto.validateSecurityKey = function (securityKey) {
var isValid = securityKey
&& securityKey.passphrase
&& securityKey.keyIdentifier
&& securityKey.keyVersion
&& securityKey.adyenCryptoVersion;
if (!isValid) {
throw new InvalidSecurityKeyException("Invalid Security Key");
}
};
NexoCrypto.crypt = function (bytes, dk, ivNonce, mode) {
var actualIV = Buffer.alloc(NEXO_IV_LENGTH);
for (var i = 0; i < NEXO_IV_LENGTH; i++) {
actualIV[i] = dk.iv[i] ^ ivNonce[i];
}
var cipher = mode === Modes.ENCRYPT
? createCipheriv("aes-256-cbc", dk.cipherKey, actualIV)
: createDecipheriv("aes-256-cbc", dk.cipherKey, actualIV);
var encrypted = cipher.update(bytes);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return encrypted;
};
NexoCrypto.hmac = function (bytes, derivedKey) {
var mac = createHmac("sha256", derivedKey.hmacKey);
return mac.update(bytes).digest();
};
NexoCrypto.generateRandomIvNonce = function () {
return randomBytes(NEXO_IV_LENGTH);
};
NexoCrypto.prototype.validateHmac = function (receivedHmac, decryptedMessage, derivedKey) {
var hmac = NexoCrypto.hmac(decryptedMessage, derivedKey);
var isValid = hmac.every(function (item, index) { return item === receivedHmac[index]; });
if (!isValid) {
throw new NexoCryptoException("Hmac validation failed");
}
};
return NexoCrypto;
}())
Example #3
Source File: crypto.js From lx-music-mobile with Apache License 2.0 | 5 votes |
aesDecrypt = function(cipherBuffer, mode, key, iv) {
let decipher = createDecipheriv(mode, key, iv)
return Buffer.concat([decipher.update(cipherBuffer), decipher.final()])
}