Encryption and Decryption

Here goes the Encryption and Decryption procedure for data encrypted within the enc_payload object of your webhook payload.

Encryption

export const encryptor = (decryptedData: string, publicKey: any, encryptionKey: any) => {
    const cipher = 'aes-256-cbc'

    const enc_key = crypto.createHash('sha256').update(String(publicKey)).digest('base64').substr(0, 32);
    const pub_key = crypto.createHash('sha256').update(String(encryptionKey)).digest('base64').substr(0, 16);


    const encrypt = crypto.createCipheriv(cipher, enc_key, pub_key)

    let encryption = encrypt.update(decryptedData, 'utf8', 'base64')
    encryption += encrypt.final('base64')

    return encryption
}

Decryption

export const decryptor = (encryptedData: string, publicKey: any, encryptionKey: any) => {
    const cipher = 'aes-256-cbc'

    const enc_key = crypto.createHash('sha256').update(String(publicKey)).digest('base64').substr(0, 32);
    const pub_key = crypto.createHash('sha256').update(String(encryptionKey)).digest('base64').substr(0, 16);

    const decrypt = crypto.createDecipheriv(cipher, enc_key, pub_key)

    let decryption = decrypt.update(encryptedData, 'base64', 'utf8')
    decryption += decrypt.final('utf8')

    return decryption
}

Last updated