Open RichardNassar1 opened 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
+1
Ah, the require
is an issue as well. If I have time I'll contribute a PR for this
how to use react-native-crypto
Use ^3.0.0
not ^4.0.0
, it works in RN
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
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.
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.
Can anyone please help me.
@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'}
CryptoJS.HmacSHA256() is not working and gives me TypeError: undefined is not a function error. i'm using react-native-crypto-js package
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)
react-native-crypto is recommended to use? It has not be updated for 5 years ago
is it still can't solved ?
Hello,
I tried to use crypto-js with react native and I got an error. Please advise.