Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using crypto-js in React

I generated an app using create-react-app and I want to use crypto-js I am getting the following error

crypto.sha256() is not a function

like image 311
Ryan Avatar asked Dec 12 '17 06:12

Ryan


People also ask

How do you use crypto-JS in react?

To encrypt and decrypt data, simply use encrypt() and decrypt() function from an instance of crypto-js. var bytes = CryptoJS. AES. decrypt(ciphertext, 'my-secret-key@123');

Why crypto-JS is used?

CryptoJS is a growing collection of standard and secure cryptographic algorithms implemented in JavaScript using best practices and patterns. They are fast, and they have a consistent and simple interface.

Can I use JavaScript in react?

It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript. JSX produces React “elements”.


1 Answers

You gotta install crypto-js using

npm install crypto-js

In your js files, you have to import module you wanna use

import sha256 from 'crypto-js/sha256';

Now you can use those functions

sha256(nonce + message);

You have do define message and nonce before this

like image 56
sudo bangbang Avatar answered Oct 08 '22 23:10

sudo bangbang