I would like to hash and encrypt my request to the external API. For this purpose, I would like to use the famous “crypto-js” javascript library, however unable to do so.
Hence I need your help in achieving the same. Your help is highly appreciated.
Does Wix provides any library for this purpose or I have use the external javascript files? Please explain in detail.
I don’t believe they have support for external libraries. Can I ask you how would you use this library externally? I have a problem to solve that involves 3rd party libraries as well.
Hi,
Currently there’s no option to import external libraries. You can copy the library’s code, add this as a JS file and import it as you import a regular JS code.
I am able to use the library now by following your suggestion. Thank you.
The steps I followed are…
Copied the JS library code and pasted it in a new JS file in public folder.
though it showed error, it worked perfectly. Earlier I was skeptical about the errors.
Hello Daniel,
If you have a bit javascript experience, it would be easy for you to understand.
Here, my requirement was to use hashing algorithm HMACSHA256 to hash a string and then encode the result using Base64 algorithm.
If you see the js code of hmac-sha256.js, in first few lines you may notice something like this
this js file requires core.js.
Hope it’s clear now.
Hence I included all the dependency files altogether and used the following syntax to use the CryptoJS library. Make sure you put all the JS files in public directory.
import * as CryptoJS from 'public/crypto-js.js'
Now you may select the required files based on your requirement and use it in your website.
Step 2
In your main service module file(ex. service.jsw), I mean, from where you would call the CryptoJS function,
import the cryptojs and use it as follows
import * as CryptoJS from 'public/crypto-js.js';
export function conversion(){
var secret = "Pasw0rd@123";
var string2Sign = "Welcome to the world of programming";
var hash = CryptoJS.HmacSHA256(string2Sign, secret);
var signature = CryptoJS.enc.Base64.stringify(hash);
}
So I did what you told me, copy the entire code, BUT is it normal that it gives several erros and warnings specially at the beginning of the code? That’s why I asked if I have to paste all the code.
Errors: “module” , “define” and “exports” not defined.
Warnings: Unnecessary semicolon.
@pptgames you can import crypto-js from NPM repository in the editor. In the editor, click on the backend folder + button, select install NPM package and select crypto-js from the list.