Hello ,
I am trying to build a SMA256 signature using toolbox , I saw the following code posted by @miguel :
var crypto = require(‘crypto’)
, text = ‘I love cupcakes’
, key = ‘abcdeghi’
, hash;
var hash = crypto.createHmac(‘sha256’, key).update(text).digest(‘hex’);
var base64 = Buffer.from(hash).toString(‘base64’);
I have parameters to add , how do i add them in this code and where do i add my secret key please ? I tried to ask chat gpt and it gave me the following code:
const crypto = require(‘crypto’);
// Input data as an object
const inputData = {
vads_action_mode: “INTERACTIVE”,
vads_amount: “5124”,
vads_ctx_mode: “TEST”,
vads_currency: “953”,
vads_page_action: “PAYMENT”,
vads_payment_config: “SINGLE”,
vads_site_id: “12345678”,
vads_trans_date: “20170129130025”,
vads_trans_id: “123456”,
vads_version: “V2”
};
// Function to generate the SHA-256 Base64 key
function generateSHA256Base64Key(dataObject, secretKey) {
// Sort the keys alphabetically and concatenate “key=value” pairs
const sortedData = Object.keys(dataObject)
.sort() // Sort keys alphabetically
.map(key => ${key}=${dataObject[key]}
) // Create “key=value” pairs
.join(“&”); // Join pairs with ‘&’
console.log('Concatenated string:', sortedData); // Debug: see the concatenated string
// Append the secret key at the end (if required by the algorithm)
const finalData = `${sortedData}&${secretKey}`;
// Create the SHA-256 hash and encode it in Base64
const hash = crypto.createHash('sha256').update(finalData).digest('base64');
return hash;
}
// Example secret key (replace with your actual secret key)
const secretKey = “your_secret_key”;
// Generate the key
const base64Key = generateSHA256Base64Key(inputData, secretKey);
console.log(‘Generated SHA-256 Base64 Key:’, base64Key);
However when i try to add the result to an item , nothing is happening… Can someone help me please?