UNPKG

@adyen/api-library

Version:

The Adyen API Library for NodeJS enables you to work with Adyen APIs.

62 lines 3.09 kB
"use strict"; /* * ###### * ###### * ############ ####( ###### #####. ###### ############ ############ * ############# #####( ###### #####. ###### ############# ############# * ###### #####( ###### #####. ###### ##### ###### ##### ###### * ###### ###### #####( ###### #####. ###### ##### ##### ##### ###### * ###### ###### #####( ###### #####. ###### ##### ##### ###### * ############# ############# ############# ############# ##### ###### * ############ ############ ############# ############ ##### ###### * ###### * ############# * ############ * Adyen NodeJS API Library * Copyright (c) 2026 Adyen B.V. * This file is open source and available under the MIT license. * See the LICENSE file for more info. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.generateRandomIvNonce = exports.validateHmac = exports.hmac = exports.crypt = void 0; const crypto_1 = require("crypto"); const nexoDerivedKey_1 = require("../../typings/clouddevice/security/nexoDerivedKey"); const nexoSecurityException_1 = require("./nexoSecurityException"); /** Performs AES-256-CBC encryption or decryption using the derived key and provided IV nonce. */ function crypt(bytes, dk, ivNonce, mode) { if (ivNonce.length !== nexoDerivedKey_1.NEXO_IV_LENGTH) { throw new nexoSecurityException_1.NexoSecurityException("Invalid IV nonce length: expected " + nexoDerivedKey_1.NEXO_IV_LENGTH + ", got " + ivNonce.length); } const actualIV = Buffer.alloc(nexoDerivedKey_1.NEXO_IV_LENGTH); for (let i = 0; i < nexoDerivedKey_1.NEXO_IV_LENGTH; i++) { actualIV[i] = dk.iv[i] ^ ivNonce[i]; } if (mode === "encrypt") { const cipher = (0, crypto_1.createCipheriv)("aes-256-cbc", dk.cipherKey, actualIV); return Buffer.concat([cipher.update(bytes), cipher.final()]); } else { const decipher = (0, crypto_1.createDecipheriv)("aes-256-cbc", dk.cipherKey, actualIV); return Buffer.concat([decipher.update(bytes), decipher.final()]); } } exports.crypt = crypt; /** Generates an HMAC-SHA256 for message authentication. */ function hmac(bytes, dk) { return (0, crypto_1.createHmac)("sha256", dk.hmacKey).update(bytes).digest(); } exports.hmac = hmac; /** Validates the HMAC of a decrypted message to ensure data integrity. */ function validateHmac(receivedHmac, decryptedMessage, dk) { const computed = hmac(decryptedMessage, dk); if (!(0, crypto_1.timingSafeEqual)(computed, receivedHmac)) { throw new nexoSecurityException_1.NexoSecurityException("HMAC validation failed"); } } exports.validateHmac = validateHmac; /** Generates a cryptographically random IV nonce. */ function generateRandomIvNonce() { return (0, crypto_1.randomBytes)(nexoDerivedKey_1.NEXO_IV_LENGTH); } exports.generateRandomIvNonce = generateRandomIvNonce; //# sourceMappingURL=nexoCryptoPrimitives.js.map