ethers.js/docs.wrm/cookbook/react-native.wrm
2023-09-12 03:27:29 -04:00

37 lines
1004 B
Plaintext

_section: React Native @<cookbook-react-native>
When using React Native, many of the built-in cryptographic primitives
can be replaced by native, substantially faster implementations.
This should be available in its own package in the future, but for now
this is highly recommended, and requires installing the
[[link-npm-react-native-quick-crypto]] package.
_code:
import { ethers } from "ethers";
import crypto from "react-native-quick-crypto";
ethers.randomBytes.register((length) => {
return new Uint8Array(crypto.randomBytes(length));
});
ethers.computeHmac.register((algo, key, data) => {
return crypto.createHmac(algo, key).update(data).digest();
});
ethers.pbkdf2.register((passwd, salt, iter, keylen, algo) => {
return crypto.pbkdf2Sync(passwd, salt, iter, keylen, algo);
});
ethers.sha256.register((data) => {
return crypto.createHash('sha256').update(data).digest();
});
ethers.sha512.register((data) => {
return crypto.createHash('sha512').update(data).digest();
});