const crypto = require('crypto'); // Function to create a hash (MD5) function createHash(value) { return crypto.createHash('sha256').update(value).digest('hex'); } // Function to make Base64 URL-safe function base64UrlEncode(value) { return Buffer.from(value, 'hex') // Convert hex to buffer .toString('base64') // Base64 encode .replace(/\+/g, '-') // Replace '+' with '-' .replace(/\//g, '_') // Replace '/' with '_' .replace(/=+$/, ''); // Remove trailing '=' } // Function to decode Base64 URL-safe encoding function base64UrlDecode(value) { value = value .replace(/-/g, '+') // Replace '-' with '+' .replace(/_/g, '/'); // Replace '_' with '/' // Add padding if necessary const padLength = (4 - (value.length % 4)) % 4; value += '='.repeat(padLength); return Buffer.from(value, 'base64').toString('hex'); // Decode Base64 to hex } // Function to isolate the three hashes (MD5 hashes are 32 characters long) function isolateHashes(decodedHash) { const hashLength = 64; // Each MD5 hash in hex is 32 characters long const hashA = decodedHash.slice(0, hashLength); const hashB = decodedHash.slice(hashLength, hashLength * 2); const hashC = decodedHash.slice(hashLength * 2, hashLength * 3); const extraDataHex = decodedHash.slice(hashLength * 3); // Remaining part as extraData return { hashA, hashB, hashC, extraDataHex }; } var tenant_id = '1-NMVM'; var customerId = '1-NXHTP'; var extraData = {'saved_search_name': createHash('Default Search111')}; // Create unsubscribe hash and combine it with Base64-encoded extraData var unsubscribeHash = createHash(tenant_id) + createHash(customerId) + createHash('saved_search'); var encodedExtraData = Buffer.from(JSON.stringify(extraData)).toString('hex'); var finalToken = base64UrlEncode(unsubscribeHash + encodedExtraData); console.log('Final Token:', finalToken); // Decode the Base64 URL-safe encoded token back to original parts const decodedHash = base64UrlDecode(finalToken); // Isolate individual hashes and extraData const { hashA: isolatedHashA, hashB: isolatedHashB, hashC: isolatedHashC, extraDataHex } = isolateHashes(decodedHash); // Decode and parse extraData const decodedExtraData = JSON.parse(Buffer.from(extraDataHex, 'hex').toString('utf8')); // Output isolated hashes and decoded extraData console.log('Isolated Hash A:', isolatedHashA); console.log('Isolated Hash B:', isolatedHashB); console.log('Isolated Hash C:', isolatedHashC); console.log('Decoded ExtraData:', decodedExtraData); // Verify that isolated hashes match the original ones console.log('Verification:'); console.log('Hash A matches:', isolatedHashA === createHash(tenant_id)); console.log('Hash B matches:', isolatedHashB === createHash(customerId)); console.log('Hash C matches:', isolatedHashC === createHash('saved_search'));
Write, Run & Share NodeJS code online using OneCompiler's NodeJS online compiler for free. It's one of the robust, feature-rich online compilers for NodeJS language,running on the latest LTS version NodeJS 16.14.2. Getting started with the OneCompiler's NodeJS editor is easy and fast. The editor shows sample boilerplate code when you choose language as NodeJS and start coding. You can provide the dependencies in package.json
.
Node.js is a free and open-source server environment. Node.js is very popular in recent times and a large number of companies like Microsoft, Paypal, Uber, Yahoo, General Electric and many others are using Node.js.
Google chrome's javascript engine V8
and is pretty fast.Asynchronous
, event-driven
and works on single-thread model
thus eliminating the dis-advantages of multi-thread model.Express is one of the most popular web application framework in the NodeJS echosystem.
let moment = require('moment');
console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));
const _ = require("lodash");
let colors = ['blue', 'green', 'yellow', 'red'];
let firstElement = _.first(colors);
let lastElement = _.last(colors);
console.log(`First element: ${firstElement}`);
console.log(`Last element: ${lastElement}`);
Following are the libraries supported by OneCompiler's NodeJS compiler.