Synchronous randombytes function that works in node, the browser & react-native!
npm install get-random-values-polypony!Standard & TypeScript definitions
!Device Tests
get-random-values-polypony is a thorougly tested, normalizing ponyfill for crypto.getRandomValues with a polyfill) option.
- It is tested to work with react-native(ios, android), expo, node 6+, IE 11, Metro, Firefox, Safari
- No dependencies.
npm i get-random-values-polypony --save
You can use it like an other randomBytes function by calling:
``javascript`
const getRandomValues = require('get-random-values-polypony')
const randomUint8Array = getRandomValues(new Uint8Array(16))
Or setup the polyfill like:
`javascript
require('get-random-values-polypony').polyfill()
crypto.getRandomValues(new Uint8Array(16))
`
The polyfill respects _(unlike other implementations)_ that the native crypto.getRandomValues do not support Float32Array, Float64Array, DataView, BigInt64Array or BigUint64Array and behaves likewise on node/react-native.crypto.getRandomValues
Similarly the polyfill does not accept a ArrayBuffer that has more than 65536 bytes.
The ponyfill on the other hand will accept all native ArrayBufferView implementations at any size, on all platforms.
This library works out-of-the-box with expo - no additional setup needed.
Other react-native implementations use base64 to process the native data in JavaScript, this implementation works around this by using hex encoded data which can be easily processed without a library (also: it uses less data).
Some other implementations use like this a random-seed implementations. Those dependencies are often part of a bigger crypto suite that comes with dependencies and dependencies of dependencies, bloating the package tree while not necessarily improving the safety.
To implement it for react-native, this library ported google chrome's random implementation to javascript. The advantage of google's implementation is the small initial random value (64bit) which is easy to supply - even in expo.
It should also come with the same level of security as chrome does.
Other implementations such as react-native-get-random-values use blocking native calls in react-native that may create locks between the native and JS thread.
get-random-values-polypony` only requires a secure initial seed, like browsers and the rest is executed on the javascript thread.