brix / crypto-js

JavaScript library of crypto standards.
Other
15.67k stars 2.38k forks source link

Does crypto-js work with react-native? #303

Open RichardNassar1 opened 4 years ago

RichardNassar1 commented 4 years ago

Hello,

I tried to use crypto-js with react native and I got an error. Please advise.

rajivshah3 commented 4 years ago

I think the issue is that crypto-js requires crypto, which is a built-in Node.js module. It's not supplied with React Native. We used react-native-crypto with rn-nodeify to get around this. It should allow crypto-js to work properly

showtan001 commented 4 years ago

+1

rajivshah3 commented 4 years ago

Ah, the require is an issue as well. If I have time I'll contribute a PR for this

rohithnair1996 commented 4 years ago

how to use react-native-crypto

ortonomy commented 4 years ago

Use ^3.0.0 not ^4.0.0, it works in RN

csandman commented 4 years ago

In the patch notes for version 4.0.0 they state that this library will no longer be compatible with React Native. I tried the route mentioned by @rajivshah3, however this added a lot of bloat, so I think using version 3.3.0 is a better option. to install it, enter:

npm install crypto-js@3.3.0 --save
entronad commented 3 years ago

If the issues caused by secure radom of crypto-js 4.0 bother you, such as too large bundle, unavalible in React Native... Maybe you can try crypto-es .

We merged all updates of crypto-js 4.0 except the secure radom.

LogiMateenSA commented 2 years ago

Somehow version 3.0.0 does not work for me

Below is code i am using for setting cookie from node server

var cookieValue="Mateen Syed"
cookieValue ? res.cookie(cookieName, CryptoJS.AES.encrypt(cookieValue, "Dummy").toString()) : "" 
console.log("Settnig cookie decoded value ", CryptoJS.AES.encrypt(cookieValue, "Dummy").toString())

Below is result from console Settnig cookie decoded value U2FsdGVkX1+ddRjYsooHJS8z1f6qGzELqsHDYAEikAE=

Below is code i am using to get decrypted value of cookie in react client

var decryptvalTemp = CryptoJS.AES.decrypt(poppedValue, "Dummy");
    console.log("decryptvalTemp ", decryptvalTemp)
    console.log("React decoded value ", decryptvalTemp.toString(CryptoJS.enc.Utf8))

Below is output from console. image

Can anyone please help me.

gantispam commented 2 years ago

@LogiMateenSA

    const myObjectToEncrypt = {foo: 'bar'};
    const myObjectToEncryptAsString = JSON.stringify(myObjectToEncrypt);
    const myObjectEncryptedAsString = CryptoJS.AES.encrypt(myObjectToEncryptAsString, 'my-private-key').toString();
    const myObjectDecryptedAsString = CryptoJS.AES.decrypt(myObjectEncryptedAsString, 'my-private-key').toString(CryptoJS.enc.Utf8);
    const myObjectDecrypted = JSON.parse(myObjectDecryptedAsString); // return {foo: 'bar'}
Sahil6458 commented 1 year ago

CryptoJS.HmacSHA256() is not working and gives me TypeError: undefined is not a function error. i'm using react-native-crypto-js package

DavidAPears commented 1 year ago

Same here. I've lost the best part of a day on this. Any help greatly appreciated.

import CryptoJS from "react-native-crypto-js";

 const signature = CryptoJS.HmacSHA256(
    `${encodedHeader}.${encodedPayload}`,
     secret
  );

Throws the error:

 WARN  Possible Unhandled Promise Rejection (id: 0):
TypeError: _reactNativeCryptoJs.default.HmacSHA256 is not a function. (In '_reactNativeCryptoJs.default.HmacSHA256(encodedHeader + "." + encodedPayload, secret)', '_reactNativeCryptoJs.default.HmacSHA256' is undefined)
gasparnd commented 10 months ago

react-native-crypto is recommended to use? It has not be updated for 5 years ago

afifputra commented 2 months ago

is it still can't solved ?