UNPKG

2.37 kBMarkdownView Raw
1# react-native-randombytes
2
3## Usage
4
5Note: only the asynchronous API is supported, due to the limitations of having to go over the RCTBridge, and there being no secure RNG in JavaScriptCore (Math.random is a bit of a joke in the industry)
6
7```js
8var randomBytes = require('react-native-randombytes')
9
10// synchronous API
11// uses SJCL
12var rand = randomBytes(4)
13
14// asynchronous API
15// uses iOS-side SecRandomCopyBytes
16randomBytes(4, (err, bytes) => {
17 console.log(bytes.toString('hex'))
18})
19```
20
21## Installation
22
23### `iOS`
24
25* Drag RNRandomBytes.xcodeproj from node_modules/react-native-randombytes into your XCode project.
26
27* Click on the project in XCode, go to Build Phases, then Link Binary With Libraries and add `libRNRandomBytes.a`
28
29Confused? See an example with screenshots [here](http://facebook.github.io/react-native/docs/linking-libraries-ios.html#content)
30
31
32### `Android`
33
34* Update Gradle Settings
35
36```gradle
37// file: android/settings.gradle
38...
39
40include ':randombytes', ':app'
41project(':randombytes').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-randombytes/app')
42```
43
44* Update Gradle Build
45
46```gradle
47// file: android/app/build.gradle
48...
49
50dependencies {
51 ...
52 compile project(':randombytes')
53}
54```
55
56* Register React Package
57
58```java
59...
60import com.bitgo.randombytes.RandomBytesPackage // import
61
62public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
63
64 private ReactInstanceManager mReactInstanceManager;
65 private ReactRootView mReactRootView;
66
67 @Override
68 protected void onCreate(Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
70 mReactRootView = new ReactRootView(this);
71 mReactInstanceManager = ReactInstanceManager.builder()
72 .setApplication(getApplication())
73 .setBundleAssetName("index.android.bundle")
74 .setJSMainModuleName("index.android")
75 .addPackage(new MainReactPackage())
76 .addPackage(new RandomBytesPackage()) // register react iconify package here
77 .setUseDeveloperSupport(BuildConfig.DEBUG)
78 .setInitialLifecycleState(LifecycleState.RESUMED)
79 .build();
80 mReactRootView.startReactApplication(mReactInstanceManager, "AwesomeProject", null);
81 setContentView(mReactRootView);
82 }
83...
84
85```