Payment encryption
const crypto = require('crypto')
const { v4: uuidv4 } = require('uuid')
// Load encryption key from environment (in production)
// In this example we're creating a mock key
const encryptKey = ENCRYPT_KEY
/**
* Encrypts payment information with a unique UUID and IV
* @param {Object} paymentInfo - Object containing payment details
* @returns {Object} - Object containing uuid, encrypted data and IV
*/
function encryptPaymentInfo(paymentInfo) {
// Generate a unique ID for this encryption operation
const uuid = uuidv4()
// Generate a unique IV for this encryption
const iv = crypto.randomBytes(16)
// Convert payment info to JSON string
const paymentInfoString = JSON.stringify(paymentInfo)
// Create cipher with the unique IV
const cipher = crypto.createCipheriv('aes-256-cbc', encryptKey, iv)
// Encrypt the data
let encrypted = cipher.update(paymentInfoString, 'utf8', 'hex')
encrypted += cipher.final('hex')
// Return both the encrypted data and the IV (as hex)
return {
encryptedData: encrypted,
iv: iv.toString('hex'),
}
}
module.exports = { encryptPaymentInfo }Last updated