You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 21, 2023. It is now read-only.
Changes `webcrypto.js` to check for native web crypto availability and falls
back to using `window.__crypto` if not available.
If the user wants to bring their own Web Crypto API compatible implementation
then they simply need to assign it to `window.__crypto` before they start using
IPFS.
Checks are done in the functions that require web crypto to give the user the
flexibility to assign to `window.__crypto` before OR after they import
`libp2p-crypto`. It also means that users have the ability to use other exported
functions that do not require web crypto without having to worry about sorting
their own implementation.
We use `window.__crypto` because `window.crypto` is a readonly property in
secure context and always readonly in workers.
If `window.crypto` and `window.__cypto` are unavailable then an appropriate
error message is reported to the user with a `ERR_MISSING_WEB_CRYPTO` code.
I've also added documentation to the README.
This is a backwards compatible change.
closes#149resolves#105resolvesipfs/js-ipfs#2017resolvesipfs/js-ipfs#2153
License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
Copy file name to clipboardexpand all lines: README.md
+30-1
Original file line number
Diff line number
Diff line change
@@ -48,11 +48,40 @@ This repo contains the JavaScript implementation of the crypto primitives needed
48
48
npm install --save libp2p-crypto
49
49
```
50
50
51
+
## Usage
52
+
53
+
```js
54
+
constcrypto=require('libp2p-crypto')
55
+
56
+
// Now available to you:
57
+
//
58
+
// crypto.aes
59
+
// crypto.hmac
60
+
// crypto.keys
61
+
// etc.
62
+
//
63
+
// See full API details below...
64
+
```
65
+
66
+
### Web Crypto API
67
+
68
+
The `libp2p-crypto` library depends on the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) in the browser. Web Crypto is available in all modern browsers, however browsers restrict its usage to [Secure Contexts](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts).
69
+
70
+
**This means you will not be able to use `libp2p-crypto` in the browser when the page is served over HTTP.**
71
+
72
+
You can either move your page to be served over HTTPS or bring your own Web Crypto API implementation. If `libp2p-crypto` does not find the official Web Crypto API at `window.crypto` (or `self.crypto`) it will use `window.__crypto`.
73
+
74
+
```js
75
+
if (!window.isSecureContext) {
76
+
window.__crypto=// ... your Web Crypto API compatible implementation
77
+
}
78
+
```
79
+
51
80
## API
52
81
53
82
### `crypto.aes`
54
83
55
-
Expoes an interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197.
84
+
Exposes an interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197.
0 commit comments