react-native-aes
react-native-aes copied to clipboard
_reactNativeAesCrypto.default.pbkdf2 is not a function
I'm getting the following error while trying to run the readme example: [TypeError: _reactNativeAesCrypto.default.pbkdf2 is not a function. (In '_reactNativeAesCrypto.default.pbkdf2(password, salt, cost, length)', '_reactNativeAesCrypto.default.pbkdf2' is undefined)]
Environment "react-native": "0.64.2" "react": "17.0.1" Below is my entire file import React from 'react'; import {Text} from 'react-native'; import Aes from 'react-native-aes-crypto';
const generateKey = (password, salt, cost, length) => Aes.pbkdf2(password, salt, cost, length);
const encryptData = (text, key) => { return Aes.randomKey(16).then(iv => { return Aes.encrypt(text, key, iv).then(cipher => ({ cipher, iv, })); }); };
const decryptData = (encryptedData, key) => Aes.decrypt(encryptedData.cipher, key, encryptedData.iv);
try { generateKey('Arnold', 'salt', 5000, 256).then(key => { console.log('Key:', key); encryptData('These violent delights have violent ends', key) .then(({cipher, iv}) => { console.log('Encrypted:', cipher);
decryptData({cipher, iv}, key)
.then(text => {
console.log('Decrypted:', text);
})
.catch(error => {
console.log(error);
});
Aes.hmac256(cipher, key).then(hash => {
console.log('HMAC', hash);
});
})
.catch(error => {
console.log(error);
});
}); } catch (e) { console.error(e); } const Encrypt = () => { return <Text>Hello</Text>; }; export default Encrypt;
The README seems to be wrong. You're probably trying to import Aes like so:
import Aes from 'react-native-aes-crypto'
The correct way is by using NativeModules.Aes, as shown in the demo, like so: https://github.com/tectiv3/react-native-aes-demo/blob/master/App.js#L25