1 | # expo-random
|
2 |
|
3 | Provides 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 in managed Expo projects
|
6 |
|
7 | For managed [managed](https://docs.expo.io/versions/latest/introduction/managed-vs-bare/) Expo projects, please follow the installation instructions in the [API documentation for the latest stable release](https://docs.expo.io/versions/latest/sdk/random/).
|
8 |
|
9 | # Installation in bare React Native projects
|
10 |
|
11 | For 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.
|
12 |
|
13 | ### Add the package to your npm dependencies
|
14 |
|
15 | ```
|
16 | expo install expo-random
|
17 | ```
|
18 |
|
19 | ### Configure for iOS
|
20 |
|
21 | Run `npx pod-install` after installing the npm package.
|
22 |
|
23 | ### Configure for Android
|
24 |
|
25 | No additional set up necessary.
|
26 |
|
27 | # Documentation
|
28 |
|
29 | ```js
|
30 | import * as Random from 'expo-random';
|
31 | ```
|
32 |
|
33 | ## Methods
|
34 |
|
35 | ### `getRandomBytesAsync`
|
36 |
|
37 | ```js
|
38 | getRandomBytesAsync(byteCount: number): Promise<Uint8Array>
|
39 | ```
|
40 |
|
41 | Generates 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`.
|
42 |
|
43 | **Parameters**
|
44 |
|
45 | | Name | Type | Description |
|
46 | | --------- | -------- | ------------------------------------------------------------------------------- |
|
47 | | byteCount | `number` | A number within the range: **0...1024**. Anything else will throw a `TypeError` |
|
48 |
|
49 | **Returns**
|
50 |
|
51 | | Name | Type | Description |
|
52 | | ----------- | --------------------- | ---------------------------------------------------------------- |
|
53 | | randomBytes | `Promise<Uint8Array>` | An array of random bytes with the same length as the `byteCount` |
|
54 |
|
55 | **Example**
|
56 |
|
57 | ```js
|
58 | const randomBytes = await Random.getRandomBytesAsync(3);
|
59 | ```
|
60 |
|
61 | # Usage
|
62 |
|
63 | ```javascript
|
64 | import React from 'react';
|
65 | import { View } from 'react-native';
|
66 | import * as Random from 'expo-random';
|
67 |
|
68 | export default class DemoView extends React.Component {
|
69 | async componentDidMount() {
|
70 | const randomBytes = await Random.getRandomBytesAsync(16);
|
71 |
|
72 | /* Some crypto operation... */
|
73 | }
|
74 | render() {
|
75 | return <View />;
|
76 | }
|
77 | }
|
78 | ```
|