Skip to content

Commit 96815ae

Browse files
author
David
committed
feat: adding failsafe checks to exposed functions
1 parent 131d291 commit 96815ae

2 files changed

Lines changed: 45 additions & 15 deletions

File tree

README.md

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,103 @@
11
# react-native-csrng
2+
23
High-performance, cross-platform library that provides cryptographically secure random bytes for React Native applications.
34
Uses `SecRandomCopyBytes` on iOS and `SecureRandom` on Android.
45

56
## Table of Contents
6-
- [Usage](#usage)
7-
- [Installation](#installation)
8-
- [Example](#example)
9-
- [Contributing](#contributing)
10-
- [License](#license)
7+
8+
- [Usage](#usage)
9+
- [Installation](#installation)
10+
- [Example](#example)
11+
- [Contributing](#contributing)
12+
- [License](#license)
1113

1214
## Usage
15+
1316
The library exports 3 functions:
17+
1418
### getRandomBytes(length: number) => ArrayBuffer
19+
1520
Takes a length, the number of bytes to generate, and returns an ArrayBuffer with that length of random bytes.
21+
1622
```javascript
17-
import { getRandomBytes } from 'react-native-csrng';
23+
import { getRandomBytes } from 'react-native-csrng'
1824

19-
const randomArrayBuffer = getRandomBytes(12);
25+
const randomArrayBuffer = getRandomBytes(12)
2026
```
2127

2228
### getRandomInt(min: number, max:number) => number
29+
2330
Takes a min and a max. Returns a random value between those 2 values
31+
2432
```javascript
25-
import { getRandomInt } from 'react-native-csrng';
33+
import { getRandomInt } from 'react-native-csrng'
2634

27-
const randomNumberBetween1And10 = getRandomInt(1, 10);
35+
const randomNumberBetween1And10 = getRandomInt(1, 10)
2836
```
2937

3038
### generateUUID() => string
39+
3140
Returns a random UUID
41+
3242
```javascript
33-
import { getRandomInt } from 'react-native-csrng';
43+
import { getRandomInt } from 'react-native-csrng'
3444

35-
const randomUUID = generateUUID();
45+
const randomUUID = generateUUID()
3646
```
3747

3848
## Installation
49+
3950
```sh
4051
// Yarn
4152
yarn add react-native-csrng
4253

4354
// NPM
4455
npm install react-native-csrng
4556
```
57+
4658
## Example
59+
4760
To run the example, where you can see the library in action you have to run:
61+
4862
1. Install dependencies:
63+
4964
```sh
5065
// Yarn
5166
yarn example install
5267

5368
//NPM
5469
npm run example install
5570
```
71+
5672
1. Install Pods for iOS (only needed for iOS):
5773
```sh
5874
cd example/ios && pod install && cd ../..
5975
```
6076

6177
2. Start Metro Bundler:
78+
6279
```sh
6380
// Yarn
6481
yarn example start
6582
6683
// NPM
6784
npm run example start
6885
```
86+
6987
3. Install the app:
88+
7089
1. Android:
90+
7191
```sh
7292
// Yarn
7393
yarn example android
7494
7595
// NPM
7696
npm run example android
7797
```
98+
7899
2. iOS:
100+
79101
```sh
80102
// Yarn
81103
yarn example ios
@@ -91,9 +113,10 @@ Feel free to dive in! [Open an issue](https://github.com/daload/react-native-csr
91113
react-native-csrng follows the [Contributor Covenant](http://contributor-covenant.org/version/1/3/0/) Code of Conduct.
92114

93115
### Contributors
94-
This project exists thanks to all the people who contribute.
116+
117+
This project exists thanks to all the people who contribute.
95118
[@Daload](https://github.com/daload)
96119

97120
## License
98121

99-
[MIT](LICENSE) © Daload
122+
[MIT](LICENSE) © Daload

src/index.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@ import type { Csrng } from './Csrng.nitro'
33

44
const CsrngHybridObject = NitroModules.createHybridObject<Csrng>('Csrng')
55

6-
export function getRandomBytes(n: number): ArrayBuffer {
7-
return CsrngHybridObject.getRandomBytes(n)
6+
export function getRandomBytes(n: number): Uint8Array {
7+
if (n < 0) {
8+
throw new Error('Number of bytes must be non-negative')
9+
}
10+
const buffer = CsrngHybridObject.getRandomBytes(n)
11+
return new Uint8Array(buffer)
812
}
913

1014
export function getRandomInt(min: number, max: number): number {
15+
if (min >= max) {
16+
throw new Error('min has to be less than max')
17+
}
1118
return CsrngHybridObject.getRandomInt(min, max)
1219
}
1320

0 commit comments

Comments
 (0)