1 | # expo-random
|
2 |
|
3 | **`expo-random`** 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 | - [x] **TypeScript**
|
6 | - [x] **Unit-Tests**
|
7 | - [x] **Universal Module**
|
8 | - [x] **Physical Tests Suite Tests**
|
9 |
|
10 | | 🍎 iOS | 💚 Android | 💻 Web |
|
11 | | ------ | ---------- | ------ |
|
12 | | ✅ | ✅ | ✅ |
|
13 |
|
14 | # Installation
|
15 |
|
16 | <details>
|
17 | <summary>📚 Expand Installation</summary>
|
18 |
|
19 | First, you need to install the package from `npm` registry.
|
20 |
|
21 | ```sh
|
22 | npm install expo-random
|
23 |
|
24 | or
|
25 |
|
26 | yarn add expo-random
|
27 | ```
|
28 |
|
29 | ## iOS
|
30 |
|
31 | **`Podfile`**: Include the local CocoaPod
|
32 |
|
33 | <details>
|
34 | <summary>👉 Expand Code</summary>
|
35 |
|
36 | ```ruby
|
37 | pod 'EXRandom', path: '../node_modules/expo-random/ios'
|
38 | ```
|
39 |
|
40 | </details>
|
41 |
|
42 | Run: `$ pod install` to sync the pods with XCode.
|
43 |
|
44 | ## Android
|
45 |
|
46 | **`android/settings.gradle`**: Make the library accessible to Android
|
47 |
|
48 | <details>
|
49 | <summary>👉 Expand Code</summary>
|
50 |
|
51 | ```gradle
|
52 | include ':expo-random'
|
53 | project(':expo-random').projectDir = new File(rootProject.projectDir, '../node_modules/expo-random/android')
|
54 | ```
|
55 |
|
56 | and if not already included
|
57 |
|
58 | ```gradle
|
59 | include ':expo-core'
|
60 | project(':expo-core').projectDir = new File(rootProject.projectDir, '../node_modules/expo-core/android')
|
61 | ```
|
62 |
|
63 | </details>
|
64 |
|
65 | **`android/app/build.gradle`**: Insert the following lines inside the _`dependencies`_ block.
|
66 |
|
67 | <details>
|
68 | <summary>👉 Expand Code</summary>
|
69 |
|
70 | ```gradle
|
71 | api project(':expo-random')
|
72 | ```
|
73 |
|
74 | and if not already included
|
75 |
|
76 | ```gradle
|
77 | api project(':expo-core')
|
78 | ```
|
79 |
|
80 | </details>
|
81 |
|
82 | **`./android/app/src/main/java/host/exp/exponent/MainActivity.java`**: Import, then export the module from your _`expoPackages`_:
|
83 |
|
84 | <details>
|
85 | <summary>👉 Expand Code</summary>
|
86 |
|
87 | ```java
|
88 | /**
|
89 | * At the top of the file.
|
90 | * This is automatically imported with Android Studio, but if you are in any other editor you will need to manually import the module.
|
91 | */
|
92 | import expo.modules.random.RandomPackage;
|
93 |
|
94 | // Later in the file...
|
95 |
|
96 | @Override
|
97 | public List<Package> expoPackages() {
|
98 | /* Here you can add your own packages. */
|
99 | return Arrays.<Package>asList(
|
100 | /* Include this. */
|
101 | new RandomPackage()
|
102 | );
|
103 | }
|
104 | ```
|
105 |
|
106 | </details>
|
107 |
|
108 | </details>
|
109 |
|
110 | > **Notice** 🛠 The native installation flow is under maintenance.
|
111 |
|
112 | # Docs
|
113 |
|
114 | Once installed natively, the module can be accessed from the **`expo-random`** package.
|
115 |
|
116 | **Bare React Native**
|
117 |
|
118 | ```js
|
119 | import * as Random from 'expo-random';
|
120 | ```
|
121 |
|
122 | **Expo**
|
123 |
|
124 | ```js
|
125 | import { Random } from 'expo';
|
126 | ```
|
127 |
|
128 | ## Methods
|
129 |
|
130 | ### `getRandomBytesAsync`
|
131 |
|
132 | ```js
|
133 | getRandomBytesAsync(byteCount: number): Promise<Uint8Array>
|
134 | ```
|
135 |
|
136 | 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`.
|
137 |
|
138 | **Parameters**
|
139 |
|
140 | | Name | Type | Description |
|
141 | | --------- | -------- | ------------------------------------------------------------------------------- |
|
142 | | byteCount | `number` | A number within the range: **0...1024**. Anything else will throw a `TypeError` |
|
143 |
|
144 | **Returns**
|
145 |
|
146 | | Name | Type | Description |
|
147 | | ----------- | --------------------- | ---------------------------------------------------------------- |
|
148 | | randomBytes | `Promise<Uint8Array>` | An array of random bytes with the same length as the `byteCount` |
|
149 |
|
150 | **Example**
|
151 |
|
152 | ```js
|
153 | const randomBytes = await Random.getRandomBytesAsync(3);
|
154 | ```
|
155 |
|
156 | # Usage
|
157 |
|
158 | ```javascript
|
159 | import React from 'react';
|
160 | import { View } from 'react-native';
|
161 | import * as Random from 'expo-random';
|
162 |
|
163 | export default class DemoView extends React.Component {
|
164 | async componentDidMount() {
|
165 | const randomBytes = await Random.getRandomBytesAsync(16);
|
166 |
|
167 | /* Some crypto operation... */
|
168 | }
|
169 | render() {
|
170 | return <View />;
|
171 | }
|
172 | }
|
173 | ```
|