A specific key is generated per operation. To obtain the derived key to be used in an operation, a 3DES encryption must be performed between the merchant key, which must be previously decoded in BASE 64, and the value of the order number of the operation (Ds_Merchant_Order).
The SHA256 HMAC is calculated from the value of the Ds_MerchantParameters parameter and the key obtained in the previous step.
The result obtained is encoded in BASE 64, and the result of the encoding will be the value of the Ds_Signature parameter.
How can I do that? Can you help, my codgin it is not very good…
Dynamic imports can works in the WF weweb´s? because when I try to put the line: import { CryptoJS } from “crypto-js”; the errror appears… thanks
// Import the CryptoJS library
import * as CryptoJS from "crypto-js";
// Get the DS_MERCHANT_ORDER value
const DS_MERCHANT_ORDER = variables['62eba781-ed3a-4e8b-a551-945a05c31167'];
// Define a secret key for encryption
const secretKey = "mySecretKey";
// Encrypt the DS_MERCHANT_ORDER value using 3DES
const encrypted = CryptoJS.TripleDES.encrypt(DS_MERCHANT_ORDER, secretKey);
// Convert the encrypted value to a string
const encryptedString = encrypted.toString();
// Return the encrypted string
return encryptedString;
because you are not using dynamic imports. It’s explained in the link of my previous post.
There is also the native crypto library from tge browser, you may want ro check how to use it for the logic you need Crypto - Web APIs | MDN. And again double check if you should do this from a secure server instead
function encrypt(key, message) {
// Crear el objeto de cifrado
cipher = CryptoJS.algo.TripleDES(key);
// Cifrar los datos
encryptedData = cipher.update(message);
// Devolver el mensaje cifrado como una cadena de texto hexadecimal
return encryptedData.toString();
}
// Definir la clave y el mensaje
const key = "62eba781-ed3a-4e8b-a551-945a05c31167";
const message = "Este es un mensaje de ejemplo";
// Cifrar el mensaje
const encryptedData = encrypt(key, message);
// Imprimir el mensaje cifrado
console.log(encryptedData);
but the error appears:
name: "TypeError"
stack: "TypeError: CryptoJS.algo.TripleDES is not a function at encrypt (eval at executeCode (https://editor-cdn.weweb.io/ww_front/public/js/index.784e06f2.js:1:365845), :3:26) at eval (eval at executeCode (https://editor-cdn.weweb.io/ww_front/public/js/index.784e06f2.js:1:365845), :17:23) at eval (eval at executeCode (https://editor-cdn.weweb.io/ww_front/public/js/index.784e06f2.js:1:365845), :21:3) at executeCode (https://editor-cdn.weweb.io/ww_front/public/js/index.784e06f2.js:1:365845) at ye (https://editor-cdn.weweb.io/ww_front/public/js/index.784e06f2.js:1:434801) at fe (https://editor-cdn.weweb.io/ww_front/public/js/index.784e06f2.js:1:433069) at fe (https://editor-cdn.weweb.io/ww_front/public/js/index.784e06f2.js:1:433445) at async fe (https://editor-cdn.weweb.io/ww_front/public/js/index.784e06f2.js:1:433439) at async fe (https://editor-cdn.weweb.io/ww_front/public/js/index.784e06f2.js:1:433439) at async fe (https://editor-cdn.weweb.io/ww_front/public/js/index.784e06f2.js:1:433439)"
message: "CryptoJS.algo.TripleDES is not a function"
First, based on your import code from the State Change Weweb Converter, CryptoJS is available to you from global scope. It’s a commonJS import, which means no need for import statement. Remove that and your first example should work. Note that references to CryptoJS will work only after the import code has run (presumably at page load)
Second, according to the documentation, CryptoJS.algo.TripleDES() is not a thing. Instead, CryptoJS.TripleDES.encrypt(message, passphrase) will do what you want. (Docs here: CryptoJS - CryptoJS)
I’m guessing a little but i suspect you need to replace “console.log(encrypteddata)” with “return encrypteddata.toString()” or something close to it- the crypto is docs will help further.
If this is bugging you after the holiday, consider joining us at State Change! Encryption and low code injection are both very much in the hardest 5% we work on together there.
If you do this operation client-side with js actions in weweb your encryption key will be accessible by anybody. I doubt that the key is public to share, in fact the helper libraries provided by Redsys are all for server-side languages.