@protontech/openpgp 4.10.6 → 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +311 -239
- package/dist/lightweight/bn.interface.min.mjs +3 -0
- package/dist/lightweight/bn.interface.min.mjs.map +1 -0
- package/dist/lightweight/bn.interface.mjs +340 -0
- package/dist/lightweight/bn.min.mjs +3 -0
- package/dist/lightweight/bn.min.mjs.map +1 -0
- package/dist/lightweight/bn.mjs +3434 -0
- package/dist/lightweight/elliptic.min.mjs +3 -0
- package/dist/lightweight/elliptic.min.mjs.map +1 -0
- package/dist/lightweight/elliptic.mjs +4313 -0
- package/dist/lightweight/openpgp.min.mjs +3 -0
- package/dist/lightweight/openpgp.min.mjs.map +1 -0
- package/dist/lightweight/openpgp.mjs +31379 -0
- package/dist/lightweight/ponyfill.es6.min.mjs +3 -0
- package/dist/lightweight/ponyfill.es6.min.mjs.map +1 -0
- package/dist/lightweight/ponyfill.es6.mjs +3831 -0
- package/dist/lightweight/web-streams-adapter.min.mjs +17 -0
- package/dist/lightweight/web-streams-adapter.min.mjs.map +1 -0
- package/dist/lightweight/web-streams-adapter.mjs +561 -0
- package/dist/node/openpgp.js +43947 -0
- package/dist/node/openpgp.min.js +17 -0
- package/dist/node/openpgp.min.js.map +1 -0
- package/dist/node/openpgp.min.mjs +17 -0
- package/dist/node/openpgp.min.mjs.map +1 -0
- package/dist/node/openpgp.mjs +43884 -0
- package/dist/openpgp.js +41082 -41563
- package/dist/openpgp.min.js +17 -2
- package/dist/openpgp.min.js.map +1 -0
- package/dist/openpgp.min.mjs +17 -0
- package/dist/openpgp.min.mjs.map +1 -0
- package/dist/openpgp.mjs +43872 -0
- package/lightweight/package.json +5 -0
- package/openpgp.d.ts +890 -0
- package/package.json +63 -57
- package/dist/compat/openpgp.js +0 -61067
- package/dist/compat/openpgp.min.js +0 -2
- package/dist/compat/openpgp.worker.js +0 -173
- package/dist/compat/openpgp.worker.min.js +0 -2
- package/dist/lightweight/elliptic.min.js +0 -5
- package/dist/lightweight/openpgp.js +0 -40024
- package/dist/lightweight/openpgp.min.js +0 -2
- package/dist/lightweight/openpgp.worker.js +0 -173
- package/dist/lightweight/openpgp.worker.min.js +0 -2
- package/dist/openpgp.worker.js +0 -173
- package/dist/openpgp.worker.min.js +0 -2
- package/src/cleartext.js +0 -220
- package/src/config/config.js +0 -224
- package/src/config/index.js +0 -7
- package/src/config/localStorage.js +0 -35
- package/src/crypto/aes_kw.js +0 -153
- package/src/crypto/cfb.js +0 -169
- package/src/crypto/cipher/aes.js +0 -27
- package/src/crypto/cipher/blowfish.js +0 -398
- package/src/crypto/cipher/cast5.js +0 -610
- package/src/crypto/cipher/des.js +0 -476
- package/src/crypto/cipher/index.js +0 -91
- package/src/crypto/cipher/twofish.js +0 -346
- package/src/crypto/cmac.js +0 -98
- package/src/crypto/crypto.js +0 -394
- package/src/crypto/eax.js +0 -172
- package/src/crypto/gcm.js +0 -141
- package/src/crypto/hash/index.js +0 -163
- package/src/crypto/hash/md5.js +0 -205
- package/src/crypto/index.js +0 -57
- package/src/crypto/ocb.js +0 -274
- package/src/crypto/pkcs1.js +0 -170
- package/src/crypto/pkcs5.js +0 -55
- package/src/crypto/public_key/dsa.js +0 -188
- package/src/crypto/public_key/elgamal.js +0 -137
- package/src/crypto/public_key/elliptic/curves.js +0 -385
- package/src/crypto/public_key/elliptic/ecdh.js +0 -414
- package/src/crypto/public_key/elliptic/ecdsa.js +0 -348
- package/src/crypto/public_key/elliptic/eddsa.js +0 -119
- package/src/crypto/public_key/elliptic/index.js +0 -34
- package/src/crypto/public_key/elliptic/indutnyKey.js +0 -85
- package/src/crypto/public_key/index.js +0 -28
- package/src/crypto/public_key/prime.js +0 -275
- package/src/crypto/public_key/rsa.js +0 -597
- package/src/crypto/random.js +0 -145
- package/src/crypto/signature.js +0 -137
- package/src/encoding/armor.js +0 -433
- package/src/encoding/base64.js +0 -96
- package/src/enums.js +0 -493
- package/src/hkp.js +0 -89
- package/src/index.js +0 -161
- package/src/key/factory.js +0 -326
- package/src/key/helper.js +0 -363
- package/src/key/index.js +0 -32
- package/src/key/key.js +0 -890
- package/src/key/subkey.js +0 -187
- package/src/key/user.js +0 -230
- package/src/keyring/index.js +0 -12
- package/src/keyring/keyring.js +0 -229
- package/src/keyring/localstore.js +0 -119
- package/src/lightweight_helper.js +0 -26
- package/src/message.js +0 -825
- package/src/openpgp.js +0 -717
- package/src/packet/all_packets.js +0 -116
- package/src/packet/clone.js +0 -189
- package/src/packet/compressed.js +0 -194
- package/src/packet/index.js +0 -20
- package/src/packet/literal.js +0 -168
- package/src/packet/marker.js +0 -62
- package/src/packet/one_pass_signature.js +0 -156
- package/src/packet/packet.js +0 -300
- package/src/packet/packetlist.js +0 -232
- package/src/packet/public_key.js +0 -280
- package/src/packet/public_key_encrypted_session_key.js +0 -156
- package/src/packet/public_subkey.js +0 -44
- package/src/packet/secret_key.js +0 -448
- package/src/packet/secret_subkey.js +0 -41
- package/src/packet/signature.js +0 -782
- package/src/packet/sym_encrypted_aead_protected.js +0 -189
- package/src/packet/sym_encrypted_integrity_protected.js +0 -139
- package/src/packet/sym_encrypted_session_key.js +0 -204
- package/src/packet/symmetrically_encrypted.js +0 -118
- package/src/packet/trust.js +0 -35
- package/src/packet/user_attribute.js +0 -94
- package/src/packet/userid.js +0 -87
- package/src/polyfills.js +0 -64
- package/src/signature.js +0 -73
- package/src/type/ecdh_symkey.js +0 -69
- package/src/type/kdf_params.js +0 -114
- package/src/type/keyid.js +0 -110
- package/src/type/mpi.js +0 -138
- package/src/type/oid.js +0 -110
- package/src/type/s2k.js +0 -203
- package/src/util.js +0 -836
- package/src/wkd.js +0 -88
- package/src/worker/async_proxy.js +0 -190
- package/src/worker/worker.js +0 -167
- package/test/crypto/aes_kw.js +0 -57
- package/test/crypto/cipher/aes.js +0 -86
- package/test/crypto/cipher/blowfish.js +0 -58
- package/test/crypto/cipher/cast5.js +0 -25
- package/test/crypto/cipher/des.js +0 -143
- package/test/crypto/cipher/index.js +0 -7
- package/test/crypto/cipher/twofish.js +0 -71
- package/test/crypto/crypto.js +0 -383
- package/test/crypto/eax.js +0 -150
- package/test/crypto/ecdh.js +0 -359
- package/test/crypto/elliptic.js +0 -251
- package/test/crypto/elliptic_data.js +0 -102
- package/test/crypto/hash/index.js +0 -5
- package/test/crypto/hash/md5.js +0 -16
- package/test/crypto/hash/ripemd.js +0 -14
- package/test/crypto/hash/sha.js +0 -20
- package/test/crypto/index.js +0 -14
- package/test/crypto/ocb.js +0 -183
- package/test/crypto/pkcs5.js +0 -39
- package/test/crypto/random.js +0 -79
- package/test/crypto/rsa.js +0 -180
- package/test/crypto/validate.js +0 -387
- package/test/general/armor.js +0 -408
- package/test/general/brainpool.js +0 -360
- package/test/general/decompression.js +0 -60
- package/test/general/ecc_nist.js +0 -115
- package/test/general/ecc_secp256k1.js +0 -242
- package/test/general/forwarding.js +0 -43
- package/test/general/hkp.js +0 -165
- package/test/general/index.js +0 -20
- package/test/general/key.js +0 -3402
- package/test/general/keyring.js +0 -336
- package/test/general/oid.js +0 -39
- package/test/general/openpgp.js +0 -2542
- package/test/general/packet.js +0 -937
- package/test/general/signature.js +0 -1665
- package/test/general/streaming.js +0 -944
- package/test/general/testInputs.js +0 -18
- package/test/general/util.js +0 -183
- package/test/general/wkd.js +0 -48
- package/test/general/x25519.js +0 -556
- package/test/unittests.js +0 -64
package/README.md
CHANGED
|
@@ -1,28 +1,26 @@
|
|
|
1
|
-
OpenPGP.js [](https://automate.browserstack.com/public-build/N1l2eHFOanVBMU9wYWxJM3ZnWERnc1lidkt5UkRqa3BralV3SWVhOGpGTT0tLVljSjE4Z3dzVmdiQjl6RWgxb2c3T2c9PQ==--5864052cd523f751b6b907d547ac9c4c5f88c8a3) [](https://gitter.im/openpgpjs/openpgpjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|
2
2
|
==========
|
|
3
3
|
|
|
4
|
-
[OpenPGP.js](https://openpgpjs.org/) is a JavaScript implementation of the OpenPGP protocol.
|
|
4
|
+
[OpenPGP.js](https://openpgpjs.org/) is a JavaScript implementation of the OpenPGP protocol. It implements [RFC4880](https://tools.ietf.org/html/rfc4880) and parts of [RFC4880bis](https://tools.ietf.org/html/draft-ietf-openpgp-rfc4880bis-10).
|
|
5
5
|
|
|
6
|
-
<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-generate-toc again -->
|
|
7
6
|
**Table of Contents**
|
|
8
7
|
|
|
9
8
|
- [OpenPGP.js](#openpgpjs)
|
|
10
9
|
- [Platform Support](#platform-support)
|
|
11
10
|
- [Performance](#performance)
|
|
12
11
|
- [Getting started](#getting-started)
|
|
13
|
-
- [
|
|
14
|
-
- [
|
|
12
|
+
- [Node.js](#nodejs)
|
|
13
|
+
- [Deno (experimental)](#deno-experimental)
|
|
14
|
+
- [Browser (webpack)](#browser-webpack)
|
|
15
|
+
- [Browser (plain files)](#browser-plain-files)
|
|
15
16
|
- [Examples](#examples)
|
|
16
|
-
- [Set up](#set-up)
|
|
17
17
|
- [Encrypt and decrypt *Uint8Array* data with a password](#encrypt-and-decrypt-uint8array-data-with-a-password)
|
|
18
18
|
- [Encrypt and decrypt *String* data with PGP keys](#encrypt-and-decrypt-string-data-with-pgp-keys)
|
|
19
|
-
- [Encrypt with compression](#encrypt-with-compression)
|
|
19
|
+
- [Encrypt symmetrically with compression](#encrypt-symmetrically-with-compression)
|
|
20
20
|
- [Streaming encrypt *Uint8Array* data with a password](#streaming-encrypt-uint8array-data-with-a-password)
|
|
21
21
|
- [Streaming encrypt and decrypt *String* data with PGP keys](#streaming-encrypt-and-decrypt-string-data-with-pgp-keys)
|
|
22
22
|
- [Generate new key pair](#generate-new-key-pair)
|
|
23
23
|
- [Revoke a key](#revoke-a-key)
|
|
24
|
-
- [Lookup public key on HKP server](#lookup-public-key-on-hkp-server)
|
|
25
|
-
- [Upload public key to HKP server](#upload-public-key-to-hkp-server)
|
|
26
24
|
- [Sign and verify cleartext messages](#sign-and-verify-cleartext-messages)
|
|
27
25
|
- [Create and verify *detached* signatures](#create-and-verify-detached-signatures)
|
|
28
26
|
- [Streaming sign and verify *Uint8Array* data](#streaming-sign-and-verify-uint8array-data)
|
|
@@ -32,17 +30,12 @@ OpenPGP.js [
|
|
33
31
|
- [How do I get involved?](#how-do-i-get-involved)
|
|
34
32
|
- [License](#license)
|
|
35
|
-
- [Resources](#resources)
|
|
36
|
-
|
|
37
|
-
<!-- markdown-toc end -->
|
|
38
33
|
|
|
39
34
|
### Platform Support
|
|
40
35
|
|
|
41
|
-
* The `dist/openpgp.min.js` bundle works well with recent versions of Chrome, Firefox, Safari and Edge.
|
|
42
|
-
|
|
43
|
-
* The `dist/compat/openpgp.min.js` bundle also works with Internet Explorer 11 and old versions of Safari. Please note that this bundle overwrites the global `Promise` with a polyfill version even in some cases where it already exists, which may cause issues. It also adds some built-in prototype functions if they don't exist, such as `Array.prototype.includes`.
|
|
36
|
+
* The `dist/openpgp.min.js` bundle works well with recent versions of Chrome, Firefox, Safari and Edge.
|
|
44
37
|
|
|
45
|
-
*
|
|
38
|
+
* The `dist/node/openpgp.min.js` bundle works well in Node.js. It is used by default when you `require('openpgp')` in Node.js.
|
|
46
39
|
|
|
47
40
|
* Currently, Chrome, Safari and Edge have partial implementations of the
|
|
48
41
|
[Streams specification](https://streams.spec.whatwg.org/), and Firefox
|
|
@@ -60,119 +53,150 @@ library to convert back and forth between them.
|
|
|
60
53
|
|
|
61
54
|
### Performance
|
|
62
55
|
|
|
63
|
-
* Version 3.0.0 of the library introduces support for public-key cryptography using [elliptic curves](https://wiki.gnupg.org/ECC). We use native implementations on browsers and Node.js when available
|
|
56
|
+
* Version 3.0.0 of the library introduces support for public-key cryptography using [elliptic curves](https://wiki.gnupg.org/ECC). We use native implementations on browsers and Node.js when available. Elliptic curve cryptography provides stronger security per bits of key, which allows for much faster operations. Currently the following curves are supported:
|
|
64
57
|
|
|
58
|
+
| Curve | Encryption | Signature | NodeCrypto | WebCrypto | Constant-Time |
|
|
59
|
+
|:---------------:|:----------:|:---------:|:----------:|:---------:|:-----------------:|
|
|
60
|
+
| curve25519 | ECDH | N/A | No | No | Algorithmically** |
|
|
61
|
+
| ed25519 | N/A | EdDSA | No | No | Algorithmically** |
|
|
62
|
+
| p256 | ECDH | ECDSA | Yes* | Yes* | If native*** |
|
|
63
|
+
| p384 | ECDH | ECDSA | Yes* | Yes* | If native*** |
|
|
64
|
+
| p521 | ECDH | ECDSA | Yes* | Yes* | If native*** |
|
|
65
|
+
| brainpoolP256r1 | ECDH | ECDSA | Yes* | No | If native*** |
|
|
66
|
+
| brainpoolP384r1 | ECDH | ECDSA | Yes* | No | If native*** |
|
|
67
|
+
| brainpoolP512r1 | ECDH | ECDSA | Yes* | No | If native*** |
|
|
68
|
+
| secp256k1 | ECDH | ECDSA | Yes* | No | If native*** |
|
|
65
69
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
| p384 | ECDH | ECDSA | Yes | Yes* | Yes* |
|
|
70
|
-
| p521 | ECDH | ECDSA | Yes | Yes* | Yes* |
|
|
71
|
-
| secp256k1 | ECDH | ECDSA | Yes | Yes* | No |
|
|
72
|
-
| brainpoolP256r1 | ECDH | ECDSA | Yes | Yes* | No |
|
|
73
|
-
| brainpoolP384r1 | ECDH | ECDSA | Yes | Yes* | No |
|
|
74
|
-
| brainpoolP512r1 | ECDH | ECDSA | Yes | Yes* | No |
|
|
75
|
-
| curve25519 | ECDH | N/A | Yes | No | No |
|
|
76
|
-
| ed25519 | N/A | EdDSA | Yes | No | No |
|
|
70
|
+
\* when available
|
|
71
|
+
\** the curve25519 and ed25519 implementations are algorithmically constant-time, but may not be constant-time after optimizations of the JavaScript compiler
|
|
72
|
+
\*** these curves are only constant-time if the underlying native implementation is available and constant-time
|
|
77
73
|
|
|
78
74
|
* Version 2.x of the library has been built from the ground up with Uint8Arrays. This allows for much better performance and memory usage than strings.
|
|
79
75
|
|
|
80
|
-
* If the user's browser supports [native WebCrypto](https://caniuse.com/#feat=cryptography) via the `window.crypto.subtle` API, this will be used. Under Node.js the native [crypto module](https://nodejs.org/api/crypto.html#crypto_crypto) is used.
|
|
76
|
+
* If the user's browser supports [native WebCrypto](https://caniuse.com/#feat=cryptography) via the `window.crypto.subtle` API, this will be used. Under Node.js the native [crypto module](https://nodejs.org/api/crypto.html#crypto_crypto) is used.
|
|
81
77
|
|
|
82
|
-
* The library implements the [IETF proposal](https://tools.ietf.org/html/draft-ietf-openpgp-rfc4880bis-
|
|
78
|
+
* The library implements the [IETF proposal](https://tools.ietf.org/html/draft-ietf-openpgp-rfc4880bis-10) for authenticated encryption using native AES-EAX, OCB, or GCM. This makes symmetric encryption up to 30x faster on supported platforms. Since the specification has not been finalized and other OpenPGP implementations haven't adopted it yet, the feature is currently behind a flag. **Note: activating this setting can break compatibility with other OpenPGP implementations, and also with future versions of OpenPGP.js. Don't use it with messages you want to store on disk or in a database.** You can enable it by setting `openpgp.config.aeadProtect = true`.
|
|
83
79
|
|
|
84
80
|
You can change the AEAD mode by setting one of the following options:
|
|
85
81
|
|
|
86
82
|
```
|
|
87
|
-
openpgp.config.
|
|
88
|
-
openpgp.config.
|
|
89
|
-
openpgp.config.
|
|
83
|
+
openpgp.config.preferredAEADAlgorithm = openpgp.enums.aead.eax // Default, native
|
|
84
|
+
openpgp.config.preferredAEADAlgorithm = openpgp.enums.aead.ocb // Non-native
|
|
85
|
+
openpgp.config.preferredAEADAlgorithm = openpgp.enums.aead.experimentalGCM // **Non-standard**, fastest
|
|
90
86
|
```
|
|
91
87
|
|
|
92
|
-
* For environments that don't provide native crypto, the library falls back to [asm.js](https://caniuse.com/#feat=asmjs) implementations of AES, SHA-1, and SHA-256.
|
|
88
|
+
* For environments that don't provide native crypto, the library falls back to [asm.js](https://caniuse.com/#feat=asmjs) implementations of AES, SHA-1, and SHA-256.
|
|
93
89
|
|
|
94
90
|
|
|
95
91
|
### Getting started
|
|
96
92
|
|
|
97
|
-
####
|
|
93
|
+
#### Node.js
|
|
98
94
|
|
|
99
|
-
|
|
95
|
+
Install OpenPGP.js using npm and save it in your dependencies:
|
|
100
96
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
97
|
+
```sh
|
|
98
|
+
npm install --save openpgp
|
|
99
|
+
```
|
|
104
100
|
|
|
105
|
-
|
|
101
|
+
And import it as a CommonJS module:
|
|
106
102
|
|
|
103
|
+
```js
|
|
104
|
+
const openpgp = require('openpgp');
|
|
105
|
+
```
|
|
107
106
|
|
|
108
|
-
|
|
107
|
+
Or as an ES6 module, from an .mjs file:
|
|
109
108
|
|
|
110
|
-
|
|
109
|
+
```js
|
|
110
|
+
import * as openpgp from 'openpgp';
|
|
111
|
+
```
|
|
111
112
|
|
|
112
|
-
####
|
|
113
|
+
#### Deno (experimental)
|
|
113
114
|
|
|
114
|
-
|
|
115
|
+
Import as an ES6 module, using /dist/openpgp.mjs.
|
|
115
116
|
|
|
116
117
|
```js
|
|
117
|
-
|
|
118
|
+
import * as openpgp from './openpgpjs/dist/openpgp.mjs';
|
|
118
119
|
```
|
|
119
120
|
|
|
120
|
-
|
|
121
|
+
#### Browser (webpack)
|
|
121
122
|
|
|
122
|
-
|
|
123
|
+
Install OpenPGP.js using npm and save it in your devDependencies:
|
|
123
124
|
|
|
124
|
-
```
|
|
125
|
-
|
|
125
|
+
```sh
|
|
126
|
+
npm install --save-dev openpgp
|
|
126
127
|
```
|
|
127
128
|
|
|
128
|
-
|
|
129
|
+
And import it as an ES6 module:
|
|
129
130
|
|
|
130
131
|
```js
|
|
131
|
-
|
|
132
|
+
import * as openpgp from 'openpgp';
|
|
132
133
|
```
|
|
133
134
|
|
|
134
|
-
|
|
135
|
+
You can also only import the functions you need, as follows:
|
|
135
136
|
|
|
136
137
|
```js
|
|
137
|
-
|
|
138
|
+
import { readMessage, decrypt } from 'openpgp';
|
|
138
139
|
```
|
|
139
140
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
If you want to use the lightweight build (which is smaller, and lazily loads non-default curves on demand), copy `dist/lightweight/openpgp.min.js` and `dist/lightweight/elliptic.min.js`, load the former in a script tag, and point `openpgp.config.indutny_elliptic_path` to the latter:
|
|
141
|
+
Or, if you want to use the lightweight build (which is smaller, and lazily loads non-default curves on demand):
|
|
143
142
|
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
<script>
|
|
147
|
-
openpgp.config.indutny_elliptic_path = 'lightweight/elliptic.min.js';
|
|
148
|
-
</script>
|
|
143
|
+
```js
|
|
144
|
+
import * as openpgp from 'openpgp/lightweight';
|
|
149
145
|
```
|
|
150
146
|
|
|
151
|
-
To test whether the lazy loading works, try:
|
|
147
|
+
To test whether the lazy loading works, try to generate a key with a non-standard curve:
|
|
152
148
|
|
|
153
149
|
```js
|
|
154
|
-
|
|
150
|
+
import { generateKey } from 'openpgp/lightweight';
|
|
151
|
+
await generateKey({ curve: 'brainpoolP512r1', userIDs: [{ name: 'Test', email: 'test@test.com' }] });
|
|
155
152
|
```
|
|
156
153
|
|
|
157
154
|
For more examples of how to generate a key, see [Generate new key pair](#generate-new-key-pair). It is recommended to use `curve25519` instead of `brainpoolP512r1` by default.
|
|
158
155
|
|
|
156
|
+
|
|
157
|
+
#### Browser (plain files)
|
|
158
|
+
|
|
159
|
+
Grab `openpgp.min.js` from [unpkg.com/openpgp/dist](https://unpkg.com/openpgp/dist/), and load it in a script tag:
|
|
160
|
+
|
|
161
|
+
```html
|
|
162
|
+
<script src="openpgp.min.js"></script>
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Or, to load OpenPGP.js as an ES6 module, grab `openpgp.min.mjs` from [unpkg.com/openpgp/dist](https://unpkg.com/openpgp/dist/), and import it as follows:
|
|
166
|
+
|
|
167
|
+
```html
|
|
168
|
+
<script type="module">
|
|
169
|
+
import * as openpgp from './openpgp.min.mjs';
|
|
170
|
+
</script>
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
To offload cryptographic operations off the main thread, you can implement a Web Worker in your application and load OpenPGP.js from there. For an example Worker implementation, see `test/worker/worker_example.js`.
|
|
174
|
+
|
|
175
|
+
### Examples
|
|
176
|
+
|
|
177
|
+
Here are some examples of how to use OpenPGP.js v5. For more elaborate examples and working code, please check out the [public API unit tests](https://github.com/openpgpjs/openpgpjs/blob/main/test/general/openpgp.js). If you're upgrading from v4 it might help to check out the [changelog](https://github.com/openpgpjs/openpgpjs/wiki/V5-Changelog) and [documentation](https://github.com/openpgpjs/openpgpjs#documentation).
|
|
178
|
+
|
|
159
179
|
#### Encrypt and decrypt *Uint8Array* data with a password
|
|
160
180
|
|
|
161
|
-
Encryption will use the algorithm specified in config.
|
|
181
|
+
Encryption will use the algorithm specified in config.preferredSymmetricAlgorithm (defaults to aes256), and decryption will use the algorithm used for encryption.
|
|
162
182
|
|
|
163
183
|
```js
|
|
164
184
|
(async () => {
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
185
|
+
const message = await openpgp.createMessage({ binary: new Uint8Array([0x01, 0x01, 0x01]) });
|
|
186
|
+
const encrypted = await openpgp.encrypt({
|
|
187
|
+
message, // input as Message object
|
|
188
|
+
passwords: ['secret stuff'], // multiple passwords possible
|
|
189
|
+
format: 'binary' // don't ASCII armor (for Uint8Array output)
|
|
169
190
|
});
|
|
170
|
-
|
|
191
|
+
console.log(encrypted); // Uint8Array
|
|
171
192
|
|
|
193
|
+
const encryptedMessage = await openpgp.readMessage({
|
|
194
|
+
binaryMessage: encrypted // parse encrypted bytes
|
|
195
|
+
});
|
|
172
196
|
const { data: decrypted } = await openpgp.decrypt({
|
|
173
|
-
message:
|
|
174
|
-
passwords: ['secret stuff'],
|
|
175
|
-
format: 'binary'
|
|
197
|
+
message: encryptedMessage,
|
|
198
|
+
passwords: ['secret stuff'], // decrypt with password
|
|
199
|
+
format: 'binary' // output as Uint8Array
|
|
176
200
|
});
|
|
177
201
|
console.log(decrypted); // Uint8Array([0x01, 0x01, 0x01])
|
|
178
202
|
})();
|
|
@@ -180,14 +204,12 @@ Encryption will use the algorithm specified in config.encryption_cipher (default
|
|
|
180
204
|
|
|
181
205
|
#### Encrypt and decrypt *String* data with PGP keys
|
|
182
206
|
|
|
183
|
-
Encryption will use the algorithm preferred by the public key (defaults to aes256 for keys generated in OpenPGP.js), and decryption will use the algorithm used for encryption.
|
|
207
|
+
Encryption will use the algorithm preferred by the public (encryption) key (defaults to aes256 for keys generated in OpenPGP.js), and decryption will use the algorithm used for encryption.
|
|
184
208
|
|
|
185
209
|
```js
|
|
186
210
|
const openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via window.openpgp
|
|
187
211
|
|
|
188
212
|
(async () => {
|
|
189
|
-
await openpgp.initWorker({ path: 'openpgp.worker.js' }); // set the relative web worker path
|
|
190
|
-
|
|
191
213
|
// put keys in backtick (``) to avoid errors caused by spaces or tabs
|
|
192
214
|
const publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----
|
|
193
215
|
...
|
|
@@ -197,25 +219,40 @@ const openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via w
|
|
|
197
219
|
-----END PGP PRIVATE KEY BLOCK-----`; // encrypted private key
|
|
198
220
|
const passphrase = `yourPassphrase`; // what the private key is encrypted with
|
|
199
221
|
|
|
200
|
-
const
|
|
201
|
-
|
|
222
|
+
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
|
|
223
|
+
|
|
224
|
+
const privateKey = await openpgp.decryptKey({
|
|
225
|
+
privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
|
|
226
|
+
passphrase
|
|
227
|
+
});
|
|
202
228
|
|
|
203
|
-
const
|
|
204
|
-
message: openpgp.
|
|
205
|
-
|
|
206
|
-
|
|
229
|
+
const encrypted = await openpgp.encrypt({
|
|
230
|
+
message: await openpgp.createMessage({ text: 'Hello, World!' }), // input as Message object
|
|
231
|
+
encryptionKeys: publicKey,
|
|
232
|
+
signingKeys: privateKey // optional
|
|
207
233
|
});
|
|
208
234
|
console.log(encrypted); // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
235
|
+
|
|
236
|
+
const message = await openpgp.readMessage({
|
|
237
|
+
armoredMessage: encrypted // parse armored message
|
|
238
|
+
});
|
|
239
|
+
const { data: decrypted, signatures } = await openpgp.decrypt({
|
|
240
|
+
message,
|
|
241
|
+
verificationKeys: publicKey, // optional
|
|
242
|
+
decryptionKeys: privateKey
|
|
213
243
|
});
|
|
214
244
|
console.log(decrypted); // 'Hello, World!'
|
|
245
|
+
// check signature validity (signed messages only)
|
|
246
|
+
try {
|
|
247
|
+
await signatures[0].verified; // throws on invalid signature
|
|
248
|
+
console.log('Signature is valid');
|
|
249
|
+
} catch (e) {
|
|
250
|
+
throw new Error('Signature could not be verified: ' + e.message);
|
|
251
|
+
}
|
|
215
252
|
})();
|
|
216
253
|
```
|
|
217
254
|
|
|
218
|
-
Encrypt
|
|
255
|
+
Encrypt to multiple public keys:
|
|
219
256
|
|
|
220
257
|
```js
|
|
221
258
|
(async () => {
|
|
@@ -231,49 +268,88 @@ Encrypt with multiple public keys:
|
|
|
231
268
|
...
|
|
232
269
|
-----END PGP PRIVATE KEY BLOCK-----`; // encrypted private key
|
|
233
270
|
const passphrase = `yourPassphrase`; // what the private key is encrypted with
|
|
234
|
-
const
|
|
271
|
+
const plaintext = 'Hello, World!';
|
|
235
272
|
|
|
236
|
-
const
|
|
237
|
-
await privateKey.decrypt(passphrase)
|
|
273
|
+
const publicKeys = await Promise.all(publicKeysArmored.map(armoredKey => openpgp.readKey({ armoredKey })));
|
|
238
274
|
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
|
|
275
|
+
const privateKey = await openpgp.decryptKey({
|
|
276
|
+
privateKey: await openpgp.readKey({ armoredKey: privateKeyArmored }),
|
|
277
|
+
passphrase
|
|
278
|
+
});
|
|
242
279
|
|
|
243
|
-
const
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
280
|
+
const message = await openpgp.createMessage({ text: plaintext });
|
|
281
|
+
const encrypted = await openpgp.encrypt({
|
|
282
|
+
message, // input as Message object
|
|
283
|
+
encryptionKeys: publicKeys,
|
|
284
|
+
signingKeys: privateKey // optional
|
|
247
285
|
});
|
|
248
286
|
console.log(encrypted); // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
|
|
249
287
|
})();
|
|
250
288
|
```
|
|
251
289
|
|
|
252
|
-
|
|
290
|
+
If you expect an encrypted message to be signed with one of the public keys you have, and do not want to trust the decrypted data otherwise, you can pass the decryption option `expectSigned = true`, so that the decryption operation will fail if no valid signature is found:
|
|
291
|
+
```js
|
|
292
|
+
(async () => {
|
|
293
|
+
// put keys in backtick (``) to avoid errors caused by spaces or tabs
|
|
294
|
+
const publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----
|
|
295
|
+
...
|
|
296
|
+
-----END PGP PUBLIC KEY BLOCK-----`;
|
|
297
|
+
const privateKeyArmored = `-----BEGIN PGP PRIVATE KEY BLOCK-----
|
|
298
|
+
...
|
|
299
|
+
-----END PGP PRIVATE KEY BLOCK-----`; // encrypted private key
|
|
300
|
+
const passphrase = `yourPassphrase`; // what the private key is encrypted with
|
|
301
|
+
|
|
302
|
+
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
|
|
303
|
+
|
|
304
|
+
const privateKey = await openpgp.decryptKey({
|
|
305
|
+
privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
|
|
306
|
+
passphrase
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
const encryptedAndSignedMessage = `-----BEGIN PGP MESSAGE-----
|
|
310
|
+
...
|
|
311
|
+
-----END PGP MESSAGE-----`;
|
|
312
|
+
|
|
313
|
+
const message = await openpgp.readMessage({
|
|
314
|
+
armoredMessage: encryptedAndSignedMessage // parse armored message
|
|
315
|
+
});
|
|
316
|
+
// decryption will fail if all signatures are invalid or missing
|
|
317
|
+
const { data: decrypted, signatures } = await openpgp.decrypt({
|
|
318
|
+
message,
|
|
319
|
+
decryptionKeys: privateKey,
|
|
320
|
+
expectSigned: true,
|
|
321
|
+
verificationKeys: publicKey, // mandatory with expectSigned=true
|
|
322
|
+
});
|
|
323
|
+
console.log(decrypted); // 'Hello, World!'
|
|
324
|
+
})();
|
|
325
|
+
```
|
|
253
326
|
|
|
254
|
-
|
|
327
|
+
#### Encrypt symmetrically with compression
|
|
255
328
|
|
|
256
|
-
|
|
329
|
+
By default, `encrypt` will not use any compression when encrypting symmetrically only (i.e. when no `encryptionKeys` are given).
|
|
330
|
+
It's possible to change that behaviour by enabling compression through the config, either for the single encryption:
|
|
257
331
|
|
|
258
332
|
```js
|
|
259
333
|
(async () => {
|
|
334
|
+
const message = await openpgp.createMessage({ binary: new Uint8Array([0x01, 0x02, 0x03]) }); // or createMessage({ text: 'string' })
|
|
260
335
|
const encrypted = await openpgp.encrypt({
|
|
261
|
-
message
|
|
262
|
-
passwords: ['secret stuff'],
|
|
263
|
-
|
|
336
|
+
message,
|
|
337
|
+
passwords: ['secret stuff'], // multiple passwords possible
|
|
338
|
+
config: { preferredCompressionAlgorithm: openpgp.enums.compression.zlib } // compress the data with zlib
|
|
264
339
|
});
|
|
265
340
|
})();
|
|
266
341
|
```
|
|
267
342
|
|
|
268
|
-
|
|
269
|
-
|
|
343
|
+
or by changing the default global configuration:
|
|
270
344
|
```js
|
|
271
|
-
openpgp.config.
|
|
345
|
+
openpgp.config.preferredCompressionAlgorithm = openpgp.enums.compression.zlib
|
|
272
346
|
```
|
|
273
347
|
|
|
274
348
|
Where the value can be any of:
|
|
275
349
|
* `openpgp.enums.compression.zip`
|
|
276
350
|
* `openpgp.enums.compression.zlib`
|
|
351
|
+
* `openpgp.enums.compression.uncompressed` (default)
|
|
352
|
+
|
|
277
353
|
|
|
278
354
|
|
|
279
355
|
#### Streaming encrypt *Uint8Array* data with a password
|
|
@@ -287,33 +363,29 @@ Where the value can be any of:
|
|
|
287
363
|
}
|
|
288
364
|
});
|
|
289
365
|
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
366
|
+
const message = await openpgp.createMessage({ binary: readableStream });
|
|
367
|
+
const encrypted = await openpgp.encrypt({
|
|
368
|
+
message, // input as Message object
|
|
369
|
+
passwords: ['secret stuff'], // multiple passwords possible
|
|
370
|
+
format: 'binary' // don't ASCII armor (for Uint8Array output)
|
|
294
371
|
});
|
|
295
|
-
|
|
372
|
+
console.log(encrypted); // raw encrypted packets as ReadableStream<Uint8Array>
|
|
296
373
|
|
|
297
374
|
// Either pipe the above stream somewhere, pass it to another function,
|
|
298
375
|
// or read it manually as follows:
|
|
299
|
-
const
|
|
300
|
-
|
|
301
|
-
const { done, value } = await reader.read();
|
|
302
|
-
if (done) break;
|
|
303
|
-
console.log('new chunk:', value); // Uint8Array
|
|
376
|
+
for await (const chunk of encrypted) {
|
|
377
|
+
console.log('new chunk:', chunk); // Uint8Array
|
|
304
378
|
}
|
|
305
|
-
|
|
306
|
-
// Or, in Node.js, you can pipe the above stream as follows:
|
|
307
|
-
const nodeStream = openpgp.stream.webToNode(encrypted);
|
|
308
|
-
nodeStream.pipe(nodeWritableStream);
|
|
309
379
|
})();
|
|
310
380
|
```
|
|
311
381
|
|
|
312
|
-
For more information on
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
382
|
+
For more information on using ReadableStreams, see [the MDN Documentation on the
|
|
383
|
+
Streams API](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API).
|
|
384
|
+
|
|
385
|
+
You can also pass a [Node.js `Readable`
|
|
386
|
+
stream](https://nodejs.org/api/stream.html#stream_class_stream_readable), in
|
|
387
|
+
which case OpenPGP.js will return a Node.js `Readable` stream as well, which you
|
|
388
|
+
can `.pipe()` to a `Writable` stream, for example.
|
|
317
389
|
|
|
318
390
|
|
|
319
391
|
#### Streaming encrypt and decrypt *String* data with PGP keys
|
|
@@ -323,13 +395,17 @@ its [Reader class](https://openpgpjs.org/web-stream-tools/Reader.html).
|
|
|
323
395
|
const publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----
|
|
324
396
|
...
|
|
325
397
|
-----END PGP PUBLIC KEY BLOCK-----`; // Public key
|
|
326
|
-
const
|
|
398
|
+
const privateKeyArmored = `-----BEGIN PGP PRIVATE KEY BLOCK-----
|
|
327
399
|
...
|
|
328
400
|
-----END PGP PRIVATE KEY BLOCK-----`; // Encrypted private key
|
|
329
401
|
const passphrase = `yourPassphrase`; // Password that private key is encrypted with
|
|
330
402
|
|
|
331
|
-
const
|
|
332
|
-
|
|
403
|
+
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
|
|
404
|
+
|
|
405
|
+
const privateKey = await openpgp.decryptKey({
|
|
406
|
+
privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
|
|
407
|
+
passphrase
|
|
408
|
+
});
|
|
333
409
|
|
|
334
410
|
const readableStream = new ReadableStream({
|
|
335
411
|
start(controller) {
|
|
@@ -339,53 +415,64 @@ its [Reader class](https://openpgpjs.org/web-stream-tools/Reader.html).
|
|
|
339
415
|
});
|
|
340
416
|
|
|
341
417
|
const encrypted = await openpgp.encrypt({
|
|
342
|
-
message: openpgp.
|
|
343
|
-
|
|
344
|
-
|
|
418
|
+
message: await openpgp.createMessage({ text: readableStream }), // input as Message object
|
|
419
|
+
encryptionKeys: publicKey,
|
|
420
|
+
signingKeys: privateKey // optional
|
|
345
421
|
});
|
|
346
|
-
|
|
422
|
+
console.log(encrypted); // ReadableStream containing '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
|
|
347
423
|
|
|
424
|
+
const message = await openpgp.readMessage({
|
|
425
|
+
armoredMessage: encrypted // parse armored message
|
|
426
|
+
});
|
|
348
427
|
const decrypted = await openpgp.decrypt({
|
|
349
|
-
message
|
|
350
|
-
|
|
351
|
-
|
|
428
|
+
message,
|
|
429
|
+
verificationKeys: publicKey, // optional
|
|
430
|
+
decryptionKeys: privateKey
|
|
352
431
|
});
|
|
353
|
-
const
|
|
432
|
+
const chunks = [];
|
|
433
|
+
for await (const chunk of decrypted.data) {
|
|
434
|
+
chunks.push(chunk);
|
|
435
|
+
}
|
|
436
|
+
const plaintext = chunks.join('');
|
|
437
|
+
console.log(plaintext); // 'Hello, World!'
|
|
354
438
|
})();
|
|
355
439
|
```
|
|
356
440
|
|
|
357
441
|
|
|
358
442
|
#### Generate new key pair
|
|
359
443
|
|
|
360
|
-
ECC keys:
|
|
444
|
+
ECC keys (smaller and faster to generate):
|
|
361
445
|
|
|
362
|
-
Possible values for `curve` are: `curve25519`, `ed25519`, `p256`, `p384`, `p521`,
|
|
363
|
-
`brainpoolP256r1`, `brainpoolP384r1`,
|
|
446
|
+
Possible values for `curve` are: `curve25519`, `ed25519`, `p256`, `p384`, `p521`,
|
|
447
|
+
`brainpoolP256r1`, `brainpoolP384r1`, `brainpoolP512r1`, and `secp256k1`.
|
|
364
448
|
Note that both the `curve25519` and `ed25519` options generate a primary key for signing using Ed25519
|
|
365
449
|
and a subkey for encryption using Curve25519.
|
|
366
450
|
|
|
367
451
|
```js
|
|
368
452
|
(async () => {
|
|
369
|
-
const {
|
|
370
|
-
|
|
371
|
-
curve: '
|
|
372
|
-
|
|
453
|
+
const { privateKey, publicKey, revocationCertificate } = await openpgp.generateKey({
|
|
454
|
+
type: 'ecc', // Type of the key, defaults to ECC
|
|
455
|
+
curve: 'curve25519', // ECC curve name, defaults to curve25519
|
|
456
|
+
userIDs: [{ name: 'Jon Smith', email: 'jon@example.com' }], // you can pass multiple user IDs
|
|
457
|
+
passphrase: 'super long and hard to guess secret', // protects the private key
|
|
458
|
+
format: 'armored' // output key format, defaults to 'armored' (other options: 'binary' or 'object')
|
|
373
459
|
});
|
|
374
460
|
|
|
375
|
-
console.log(
|
|
376
|
-
console.log(
|
|
461
|
+
console.log(privateKey); // '-----BEGIN PGP PRIVATE KEY BLOCK ... '
|
|
462
|
+
console.log(publicKey); // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
|
|
377
463
|
console.log(revocationCertificate); // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
|
|
378
464
|
})();
|
|
379
465
|
```
|
|
380
466
|
|
|
381
|
-
RSA keys:
|
|
467
|
+
RSA keys (increased compatibility):
|
|
382
468
|
|
|
383
469
|
```js
|
|
384
470
|
(async () => {
|
|
385
|
-
const
|
|
386
|
-
|
|
387
|
-
rsaBits: 4096,
|
|
388
|
-
|
|
471
|
+
const { privateKey, publicKey } = await openpgp.generateKey({
|
|
472
|
+
type: 'rsa', // Type of the key
|
|
473
|
+
rsaBits: 4096, // RSA key size (defaults to 4096 bits)
|
|
474
|
+
userIDs: [{ name: 'Jon Smith', email: 'jon@example.com' }], // you can pass multiple user IDs
|
|
475
|
+
passphrase: 'super long and hard to guess secret' // protects the private key
|
|
389
476
|
});
|
|
390
477
|
})();
|
|
391
478
|
```
|
|
@@ -395,9 +482,10 @@ RSA keys:
|
|
|
395
482
|
Using a revocation certificate:
|
|
396
483
|
```js
|
|
397
484
|
(async () => {
|
|
398
|
-
const {
|
|
399
|
-
key:
|
|
400
|
-
revocationCertificate
|
|
485
|
+
const { publicKey: revokedKeyArmored } = await openpgp.revokeKey({
|
|
486
|
+
key: await openpgp.readKey({ armoredKey: publicKeyArmored }),
|
|
487
|
+
revocationCertificate,
|
|
488
|
+
format: 'armored' // output armored keys
|
|
401
489
|
});
|
|
402
490
|
console.log(revokedKeyArmored); // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
|
|
403
491
|
})();
|
|
@@ -406,36 +494,11 @@ Using a revocation certificate:
|
|
|
406
494
|
Using the private key:
|
|
407
495
|
```js
|
|
408
496
|
(async () => {
|
|
409
|
-
const {
|
|
410
|
-
key:
|
|
497
|
+
const { publicKey: revokedKeyArmored } = await openpgp.revokeKey({
|
|
498
|
+
key: await openpgp.readKey({ armoredKey: privateKeyArmored }),
|
|
499
|
+
format: 'armored' // output armored keys
|
|
411
500
|
});
|
|
412
|
-
|
|
413
|
-
```
|
|
414
|
-
|
|
415
|
-
#### Lookup public key on HKP server
|
|
416
|
-
|
|
417
|
-
```js
|
|
418
|
-
(async () => {
|
|
419
|
-
var hkp = new openpgp.HKP(); // Defaults to https://keyserver.ubuntu.com, or pass another keyserver URL as a string
|
|
420
|
-
|
|
421
|
-
let publicKeyArmored = await hkp.lookup({
|
|
422
|
-
query: 'alice@example.com'
|
|
423
|
-
});
|
|
424
|
-
var { keys: [publicKey] } = await openpgp.key.readArmored(publicKeyArmored);
|
|
425
|
-
})();
|
|
426
|
-
```
|
|
427
|
-
|
|
428
|
-
#### Upload public key to HKP server
|
|
429
|
-
|
|
430
|
-
```js
|
|
431
|
-
(async () => {
|
|
432
|
-
var hkp = new openpgp.HKP('https://pgp.mit.edu');
|
|
433
|
-
|
|
434
|
-
var publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----
|
|
435
|
-
...
|
|
436
|
-
-----END PGP PUBLIC KEY BLOCK-----`;
|
|
437
|
-
|
|
438
|
-
await hkp.upload(publicKeyArmored);
|
|
501
|
+
console.log(revokedKeyArmored); // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
|
|
439
502
|
})();
|
|
440
503
|
```
|
|
441
504
|
|
|
@@ -451,24 +514,33 @@ Using the private key:
|
|
|
451
514
|
-----END PGP PRIVATE KEY BLOCK-----`; // encrypted private key
|
|
452
515
|
const passphrase = `yourPassphrase`; // what the private key is encrypted with
|
|
453
516
|
|
|
454
|
-
const
|
|
455
|
-
|
|
517
|
+
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
|
|
518
|
+
|
|
519
|
+
const privateKey = await openpgp.decryptKey({
|
|
520
|
+
privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
|
|
521
|
+
passphrase
|
|
522
|
+
});
|
|
456
523
|
|
|
457
|
-
const
|
|
458
|
-
|
|
459
|
-
|
|
524
|
+
const unsignedMessage = await openpgp.createCleartextMessage({ text: 'Hello, World!' });
|
|
525
|
+
const cleartextMessage = await openpgp.sign({
|
|
526
|
+
message: unsignedMessage, // CleartextMessage or Message object
|
|
527
|
+
signingKeys: privateKey
|
|
460
528
|
});
|
|
461
|
-
console.log(
|
|
529
|
+
console.log(cleartextMessage); // '-----BEGIN PGP SIGNED MESSAGE ... END PGP SIGNATURE-----'
|
|
462
530
|
|
|
463
|
-
const
|
|
464
|
-
|
|
465
|
-
publicKeys: (await openpgp.key.readArmored(publicKeyArmored)).keys // for verification
|
|
531
|
+
const signedMessage = await openpgp.readCleartextMessage({
|
|
532
|
+
cleartextMessage // parse armored message
|
|
466
533
|
});
|
|
467
|
-
const
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
}
|
|
471
|
-
|
|
534
|
+
const verificationResult = await openpgp.verify({
|
|
535
|
+
message: signedMessage,
|
|
536
|
+
verificationKeys: publicKey
|
|
537
|
+
});
|
|
538
|
+
const { verified, keyID } = verificationResult.signatures[0];
|
|
539
|
+
try {
|
|
540
|
+
await verified; // throws on invalid signature
|
|
541
|
+
console.log('Signed by key id ' + keyID.toHex());
|
|
542
|
+
} catch (e) {
|
|
543
|
+
throw new Error('Signature could not be verified: ' + e.message);
|
|
472
544
|
}
|
|
473
545
|
})();
|
|
474
546
|
```
|
|
@@ -485,26 +557,35 @@ Using the private key:
|
|
|
485
557
|
-----END PGP PRIVATE KEY BLOCK-----`; // encrypted private key
|
|
486
558
|
const passphrase = `yourPassphrase`; // what the private key is encrypted with
|
|
487
559
|
|
|
488
|
-
const
|
|
489
|
-
|
|
560
|
+
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
|
|
561
|
+
|
|
562
|
+
const privateKey = await openpgp.decryptKey({
|
|
563
|
+
privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
|
|
564
|
+
passphrase
|
|
565
|
+
});
|
|
490
566
|
|
|
491
|
-
const
|
|
492
|
-
|
|
493
|
-
|
|
567
|
+
const message = await openpgp.createMessage({ text: 'Hello, World!' });
|
|
568
|
+
const detachedSignature = await openpgp.sign({
|
|
569
|
+
message, // Message object
|
|
570
|
+
signingKeys: privateKey,
|
|
494
571
|
detached: true
|
|
495
572
|
});
|
|
496
573
|
console.log(detachedSignature);
|
|
497
574
|
|
|
498
|
-
const
|
|
499
|
-
|
|
500
|
-
signature: await openpgp.signature.readArmored(detachedSignature), // parse detached signature
|
|
501
|
-
publicKeys: (await openpgp.key.readArmored(publicKeyArmored)).keys // for verification
|
|
575
|
+
const signature = await openpgp.readSignature({
|
|
576
|
+
armoredSignature: detachedSignature // parse detached signature
|
|
502
577
|
});
|
|
503
|
-
const
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
578
|
+
const verificationResult = await openpgp.verify({
|
|
579
|
+
message, // Message object
|
|
580
|
+
signature,
|
|
581
|
+
verificationKeys: publicKey
|
|
582
|
+
});
|
|
583
|
+
const { verified, keyID } = verificationResult.signatures[0];
|
|
584
|
+
try {
|
|
585
|
+
await verified; // throws on invalid signature
|
|
586
|
+
console.log('Signed by key id ' + keyID.toHex());
|
|
587
|
+
} catch (e) {
|
|
588
|
+
throw new Error('Signature could not be verified: ' + e.message);
|
|
508
589
|
}
|
|
509
590
|
})();
|
|
510
591
|
```
|
|
@@ -528,39 +609,40 @@ Using the private key:
|
|
|
528
609
|
-----END PGP PRIVATE KEY BLOCK-----`; // encrypted private key
|
|
529
610
|
const passphrase = `yourPassphrase`; // what the private key is encrypted with
|
|
530
611
|
|
|
531
|
-
const
|
|
532
|
-
|
|
612
|
+
const privateKey = await openpgp.decryptKey({
|
|
613
|
+
privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
|
|
614
|
+
passphrase
|
|
615
|
+
});
|
|
533
616
|
|
|
534
|
-
const {
|
|
535
|
-
|
|
536
|
-
|
|
617
|
+
const message = await openpgp.createMessage({ binary: readableStream }); // or createMessage({ text: ReadableStream<String> })
|
|
618
|
+
const signatureArmored = await openpgp.sign({
|
|
619
|
+
message,
|
|
620
|
+
signingKeys: privateKey
|
|
537
621
|
});
|
|
538
622
|
console.log(signatureArmored); // ReadableStream containing '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
|
|
539
623
|
|
|
540
|
-
const
|
|
541
|
-
message: await openpgp.
|
|
542
|
-
|
|
624
|
+
const verificationResult = await openpgp.verify({
|
|
625
|
+
message: await openpgp.readMessage({ armoredMessage: signatureArmored }), // parse armored signature
|
|
626
|
+
verificationKeys: await openpgp.readKey({ armoredKey: publicKeyArmored })
|
|
543
627
|
});
|
|
544
628
|
|
|
545
|
-
await
|
|
546
|
-
// Note: you *have* to read `
|
|
629
|
+
for await (const chunk of verificationResult.data) {}
|
|
630
|
+
// Note: you *have* to read `verificationResult.data` in some way or other,
|
|
547
631
|
// even if you don't need it, as that is what triggers the
|
|
548
632
|
// verification of the data.
|
|
549
633
|
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
console.log('
|
|
553
|
-
|
|
554
|
-
throw new Error('
|
|
634
|
+
try {
|
|
635
|
+
await verificationResult.signatures[0].verified; // throws on invalid signature
|
|
636
|
+
console.log('Signed by key id ' + verificationResult.signatures[0].keyID.toHex());
|
|
637
|
+
} catch (e) {
|
|
638
|
+
throw new Error('Signature could not be verified: ' + e.message);
|
|
555
639
|
}
|
|
556
640
|
})();
|
|
557
641
|
```
|
|
558
642
|
|
|
559
643
|
### Documentation
|
|
560
644
|
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
For the documentation of `openpgp.stream`, see the documentation of [the web-stream-tools dependency](https://openpgpjs.org/web-stream-tools/).
|
|
645
|
+
The full documentation is available at [openpgpjs.org](https://docs.openpgpjs.org/).
|
|
564
646
|
|
|
565
647
|
### Security Audit
|
|
566
648
|
|
|
@@ -578,9 +660,9 @@ To create your own build of the library, just run the following command after cl
|
|
|
578
660
|
|
|
579
661
|
npm install && npm test
|
|
580
662
|
|
|
581
|
-
For debugging browser errors, you can
|
|
663
|
+
For debugging browser errors, you can run `npm start` and open [`http://localhost:8080/test/unittests.html`](http://localhost:8080/test/unittests.html) in a browser, or run the following command:
|
|
582
664
|
|
|
583
|
-
|
|
665
|
+
npm run browsertest
|
|
584
666
|
|
|
585
667
|
### How do I get involved?
|
|
586
668
|
|
|
@@ -589,13 +671,3 @@ You want to help, great! It's probably best to send us a message on [Gitter](htt
|
|
|
589
671
|
### License
|
|
590
672
|
|
|
591
673
|
[GNU Lesser General Public License](https://www.gnu.org/licenses/lgpl-3.0.en.html) (3.0 or any later version). Please take a look at the [LICENSE](LICENSE) file for more information.
|
|
592
|
-
|
|
593
|
-
### Resources
|
|
594
|
-
|
|
595
|
-
Below is a collection of resources, many of these were projects that were in someway a precursor to the current OpenPGP.js project. If you'd like to add your link here, please do so in a pull request or email to the list.
|
|
596
|
-
|
|
597
|
-
* [https://www.hanewin.net/encrypt/](https://www.hanewin.net/encrypt/)
|
|
598
|
-
* [https://github.com/seancolyer/gmail-crypt](https://github.com/seancolyer/gmail-crypt)
|
|
599
|
-
* [https://github.com/mete0r/jspg](https://github.com/mete0r/jspg)
|
|
600
|
-
* [https://github.com/GPGTools/Mobile/wiki/Introduction](https://github.com/GPGTools/Mobile/wiki/Introduction)
|
|
601
|
-
* [https://github.com/gmontalvoriv/mailock](https://github.com/gmontalvoriv/mailock)
|