UNPKG

2.37 kBMarkdownView Raw
1# expo-random
2
3Provides a native interface for creating strong random bytes. With `Random` you can create values equivalent to `Node.js` core `crypto.randomBytes` API.
4
5# Installation
6
7This package is pre-installed in [managed](https://docs.expo.io/versions/latest/introduction/managed-vs-bare/) Expo projects as of SDK 33. You may skip the rest of the installation guide if this applies to you.
8
9For bare React Native projects, you must ensure that you have [installed and configured the `react-native-unimodules` package](https://github.com/unimodules/react-native-unimodules) before continuing.
10
11### Add the package to your npm dependencies
12
13```
14expo install expo-random
15```
16
17### Configure for iOS
18
19Run `pod install` in the ios directory after installing the npm package.
20
21### Configure for Android
22
23No additional set up necessary.
24
25# Documentation
26
27```js
28// in managed apps:
29import { Random } from 'expo';
30
31// in bare apps:
32import * as Random from 'expo-random';
33```
34
35## Methods
36
37### `getRandomBytesAsync`
38
39```js
40getRandomBytesAsync(byteCount: number): Promise<Uint8Array>
41```
42
43Generates completely random bytes using native implementations. The `byteCount` property is a `number` indicating the number of bytes to generate in the form of a `Uint8Array`.
44
45**Parameters**
46
47| Name | Type | Description |
48| --------- | -------- | ------------------------------------------------------------------------------- |
49| byteCount | `number` | A number within the range: **0...1024**. Anything else will throw a `TypeError` |
50
51**Returns**
52
53| Name | Type | Description |
54| ----------- | --------------------- | ---------------------------------------------------------------- |
55| randomBytes | `Promise<Uint8Array>` | An array of random bytes with the same length as the `byteCount` |
56
57**Example**
58
59```js
60const randomBytes = await Random.getRandomBytesAsync(3);
61```
62
63# Usage
64
65```javascript
66import React from 'react';
67import { View } from 'react-native';
68import * as Random from 'expo-random';
69
70export default class DemoView extends React.Component {
71 async componentDidMount() {
72 const randomBytes = await Random.getRandomBytesAsync(16);
73
74 /* Some crypto operation... */
75 }
76 render() {
77 return <View />;
78 }
79}
80```