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

Commit

Permalink
Add react-native support
Browse files Browse the repository at this point in the history
  • Loading branch information
kevlened committed Jan 13, 2018
1 parent a174a1e commit 4094bf5
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
webcrypto-shim.mjs

# Logs
logs
Expand Down
Empty file added .npmignore
Empty file.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,25 @@ crypto.subtle.digest(

## Compatibility

See [webcrypto-shim's supported browsers](https://github.com/vibornoff/webcrypto-shim#supported-browsers)
See [webcrypto-shim's supported browsers](https://github.com/vibornoff/webcrypto-shim#supported-browsers) for standard browser support.

If you need a js-only implementation for older browsers or environments that don't have any crypto support, the [Microsoft Research library](https://www.microsoft.com/en-us/download/details.aspx?id=52439) is exposed as the `extended` version.

```javascript
const crypto = require('isomorphic-crypto/extended')

/**
* IMPORTANT: On platforms without crypto, the
* js-only implementation needs another source
* of entropy for operations that require
* random numbers (creating keys, encrypting,
* wrapping keys) This should NOT be Math.random()
*/

crypto.initPrng(randomArrayOf48Bytes)
```

>
## I just want to drop in a script tag

Expand Down
1 change: 1 addition & 0 deletions browser.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./react-native.js')
1 change: 1 addition & 0 deletions browser.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./react-native.js')
1 change: 1 addition & 0 deletions index.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./react-native.js')
1 change: 1 addition & 0 deletions index.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./react-native.js')
1 change: 1 addition & 0 deletions main.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./browser')
1 change: 1 addition & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./index')
25 changes: 24 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 22 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "isomorphic-webcrypto",
"version": "1.1.3",
"description": "isomorphic webcrypto for IE11+ in 3kB",
"main": "index.js",
"main": "main.js",
"module": "index.mjs",
"browser": {
"./index.js": "browser.js",
"./main.js": "main.browser.js",
"./index.mjs": "browser.mjs"
},
"types": "index.d.ts",
Expand All @@ -18,6 +18,21 @@
"type": "git",
"url": "git+https://github.com/kevlened/isomorphic-webcrypto.git"
},
"files": [
"browser.android.js",
"browser.ios.js",
"browser.js",
"browser.mjs",
"index.android.js",
"index.d.ts",
"index.ios.js",
"index.js",
"index.mjs",
"main.browser.js",
"main.js",
"react-native.js",
"webcrypto-shim.mjs"
],
"keywords": [
"isomorphic",
"webcrypto",
Expand All @@ -30,6 +45,11 @@
"homepage": "https://github.com/kevlened/isomorphic-webcrypto#readme",
"dependencies": {
"@trust/webcrypto": "^0.8.2",
"mitt": "^1.1.3",
"msrcrypto": "^1.4.0",
"webcrypto-shim": "^0.1.2"
},
"peerDependencies": {
"react-native-securerandom": "^0.1.1"
}
}
57 changes: 57 additions & 0 deletions react-native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const generateSecureRandom = require('react-native-securerandom').generateSecureRandom
const EventEmitter = require('mitt')
const crypto = require('msrcrypto')
crypto.subtle.forceSync = true

const secureWatch = new EventEmitter()

let secured = !crypto.initPrng
let secureRandomError
if (!secured) {
generateSecureRandom(48)
.then(byteArray => {
crypto.initPrng(Array.from(byteArray))
secured = true
secureWatch.emit('secure')
})
.catch(err => {
secureRandomError = err
secureWatch.emit('secureRandomError')
})
}

crypto.ensureSecure = function(cb) {
if (secured) return cb()
if (secureRandomError) return cb(secureRandomError)
secureWatch.on('secure', () => cb())
secureWatch.on('secureRandomError', () => cb(secureRandomError))
}

// wrap all methods to ensure they're secure
const methods = [
'decrypt',
'digest',
'deriveKey',
'encrypt',
'exportKey',
'generateKey',
'importKey',
'sign',
'unwrapKey',
'verify',
'wrapKey'
]
methods.map(key => {
const original = crypto.subtle[key]
crypto.subtle[key] = function() {
const args = Array.from(arguments)
return new Promise((resolve, reject) => {
crypto.ensureSecure(err => {
if (err) return reject(err)
resolve(original.apply(crypto.subtle, args))
})
})
}
})

module.exports = crypto

0 comments on commit 4094bf5

Please sign in to comment.