Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Improve React Native documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
kevlened committed Mar 19, 2018
1 parent 9755e4b commit 25c9ca9
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ There's currently no native crypto support in React Native, so [the Microsoft Re

## Usage

There's a simple example below, but [there are many more here](https://github.com/diafygi/webcrypto-examples).
There's a simple hashing example below, but [there are many more WebCrypto examples here](https://github.com/diafygi/webcrypto-examples). This example requires you to `npm install hex-lite`.

```javascript
const crypto = require('isomorphic-webcrypto')
const hex = require('hex-lite')
// or
import crypto from 'isomorphic-webcrypto'
import hex from 'hex-lite'

crypto.subtle.digest(
{ name: 'SHA-256' },
new Uint8Array([1,2,3]).buffer
)
.then(hash => {
// do something with the hash buffer
// hashes are usually represented as hex strings
// hex-lite makes this easier
const hashString = hex.fromBuffer(hash);
})
```

Expand All @@ -53,10 +57,17 @@ const crypto = require('isomorphic-webcrypto')
// Only needed for crypto.getRandomValues
// but only wait once, future calls are secure
await crypto.ensureSecure();
const safeValues = crypto.getRandomValues();
const array = new Uint8Array(1);
crypto.getRandomValues(array);
const safeValue = array[0];
})()
```

Working React Native examples:

* Using [create-react-native-app](https://github.com/kevlened/webcrypto-react-native-examples/tree/master/crna) with Expo
* Using an ejected [create-react-native-app](https://github.com/kevlened/webcrypto-react-native-examples/blob/master/crna-ejected)

## I just want to drop in a script tag

You should use [the webcrypto-shim](https://github.com/vibornoff/webcrypto-shim) library directly:
Expand Down

0 comments on commit 25c9ca9

Please sign in to comment.