OpenPGP.js is a JavaScript implementation of the OpenPGP protocol. It implements RFC 9580 (superseding RFC 4880 and RFC 4880bis).
Table of Contents
The dist/openpgp.min.js (or .mjs) bundle works with recent versions of Chrome, Firefox, Edge and Safari 14+.
The dist/node/openpgp.min.mjs (or .cjs) bundle works in Node.js v18+: it is used by default when you import ... from 'openpgp' (or require('openpgp'), respectively).
Support for the Web Cryptography API's SubtleCrypto is required.
SubtleCrypto is only available in secure contexts.In supported versions of Node.js, SubtleCrypto is always available.
Support for the Web Streams API is required.
TransformStreams.
These are needed if you use the library with stream inputs.
In previous versions of OpenPGP.js, Web Streams were automatically polyfilled by the library,
but from v6 this task is left up to the library user, due to the more extensive browser support, and the
polyfilling side-effects. If you're working with older browsers versions which do not implement e.g. TransformStreams, you can manually
load the Web Streams polyfill.
Please note that when you load the polyfills, the global ReadableStream property (if it exists) gets overwritten with the polyfill version.
In some edge cases, you might need to use the native
ReadableStream (for example when using it to create a Response
object), in which case you should store a reference to it before loading
the polyfills. There is also the web-streams-adapter
library to convert back and forth between them.Readable streams in inputs, and instead expects (and outputs) Node's Web Streams. Node v17+ includes utilities to convert from and to Web Streams.Version 3.0.0 of the library introduced support for public-key cryptography using elliptic curves. We use native implementations on browsers and Node.js when available. Compared to RSA, elliptic curve cryptography provides stronger security per bits of key, which allows for much faster operations. Currently the following curves are supported:
| Curve | Encryption | Signature | NodeCrypto | WebCrypto | Constant-Time |
|---|---|---|---|---|---|
| curve25519 | ECDH | N/A | No | No | Algorithmically |
| ed25519 | N/A | EdDSA | No | Yes* | If native** |
| nistP256 | ECDH | ECDSA | Yes* | Yes* | If native** |
| nistP384 | ECDH | ECDSA | Yes* | Yes* | If native** |
| nistP521 | ECDH | ECDSA | Yes* | Yes* | If native** |
| brainpoolP256r1 | ECDH | ECDSA | Yes* | No | If native** |
| brainpoolP384r1 | ECDH | ECDSA | Yes* | No | If native** |
| brainpoolP512r1 | ECDH | ECDSA | Yes* | No | If native** |
| secp256k1 | ECDH | ECDSA | Yes* | No | If native** |
* when available
** these curves are only constant-time if the underlying native implementation is available and constant-time
The platform's native Web Crypto API is used for performance. On Node.js the native crypto module is also used, in cases where it offers additional functionality.
The library implements authenticated encryption (AEAD) as per RFC 9580 using AES-GCM, OCB, or EAX. This makes symmetric encryption faster on platforms with native implementations. However, since the specification is very recent and other OpenPGP implementations are in the process of adopting it, the feature is currently behind a flag. Note: activating this setting can break compatibility with other OpenPGP implementations which have yet to implement the feature. You can enable it by setting openpgp.config.aeadProtect = true.
Note that this setting has a different effect from the one in OpenPGP.js v5, which implemented support for a provisional version of AEAD from RFC 4880bis, which was modified in RFC 9580.
You can change the AEAD mode by setting one of the following options:
openpgp.config.preferredAEADAlgorithm = openpgp.enums.aead.gcm; // Default, native in WebCrypto and Node.js
openpgp.config.preferredAEADAlgorithm = openpgp.enums.aead.ocb; // Non-native, but supported across RFC 9580 implementations
openpgp.config.preferredAEADAlgorithm = openpgp.enums.aead.eax; // Native in Node.js
Install OpenPGP.js using npm and save it in your dependencies:
npm install --save openpgp
And import it as an ES module, from a .mjs file:
import * as openpgp from 'openpgp';
Or as a CommonJS module:
const openpgp = require('openpgp');
Import as an ES6 module, using /dist/openpgp.mjs.
import * as openpgp from './openpgpjs/dist/openpgp.mjs';
Install OpenPGP.js using npm and save it in your devDependencies:
npm install --save-dev openpgp
And import it as an ES6 module:
import * as openpgp from 'openpgp';
You can also only import the functions you need, as follows:
import { readMessage, decrypt } from 'openpgp';
Or, if you want to use the lightweight build (which is smaller, and lazily loads non-default curves on demand):
import * as openpgp from 'openpgp/lightweight';
To test whether the lazy loading works, try to generate a key with a non-standard curve:
import { generateKey } from 'openpgp/lightweight';
await generateKey({ curve: 'brainpoolP512r1', userIDs: [{ name: 'Test', email: 'test@test.com' }] });
For more examples of how to generate a key, see Generate new key pair. It is recommended to use curve25519 instead of brainpoolP512r1 by default.
Grab openpgp.min.js from unpkg.com/openpgp/dist, and load it in a script tag:
<script src="https://github.com/openpgpjs/openpgpjs/raw/v6.3.1/openpgp.min.js"></script>
Or, to load OpenPGP.js as an ES6 module, grab openpgp.min.mjs from unpkg.com/openpgp/dist, and import it as follows:
<script type="module">
import * as openpgp from './openpgp.min.mjs';
</script>
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.
Since TS is not fully integrated in the library, TS-only dependencies are currently listed as devDependencies, so to compile the project you’ll need to add @openpgp/web-stream-tools manually:
npm install --save-dev @openpgp/web-stream-tools
If you notice missing or incorrect type definitions, feel free to open a PR.
We recommend updating to the latest major library version as soon as possible to benefit from security and performance improvements.
When releasing a new major version, we will announce the end of life date of the previous one.
For information about which library versions are deprecated, and will thus not receive further security patches, you can refer to our npm release page.
For guidance on how to update to the latest library version, see this wiki page.
Here are some examples of how to use OpenPGP.js v6. For more elaborate examples and working code, please check out the public API unit tests. If you're upgrading from v4 it might help to check out the changelog and documentation.
Encryption will use the algorithm specified in config.preferredSymmetricAlgorithm (defaults to aes256), and decryption will use the algorithm used for encryption.
(async () => {
const message = await openpgp.createMessage({ binary: new Uint8Array([0x01, 0x01, 0x01]) });
const encrypted = await openpgp.encrypt({
message, // input as Message object
passwords: ['secret stuff'], // multiple passwords possible
format: 'binary' // don't ASCII armor (for Uint8Array output)
});
console.log(encrypted); // Uint8Array
const encryptedMessage = await openpgp.readMessage({
binaryMessage: encrypted // parse encrypted bytes
});
const { data: decrypted } = await openpgp.decrypt({
message: encryptedMessage,
passwords: ['secret stuff'], // decrypt with password
format: 'binary' // output as Uint8Array
});
console.log(decrypted); // Uint8Array([0x01, 0x01, 0x01])
})();
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.
```js const openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via window.openpgp
(async () => {
// put keys in backtick (`) to avoid errors caused by spaces or tabs
const publicKeyArmored =-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----;
const privateKeyArmored =-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----; // encrypted private key
const passphrase =yourPassphrase`; // what the private key is encrypted with
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
const privateKey = await openpgp.decryptKey({
privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
passphrase
});
const encrypted = await openpgp.encrypt({
message: await openpgp.createMessage({ text: 'Hello, World!' }), // input as Message object
encryptionKeys: publicKey,
signingKeys: privateKey // optional
});
console.log(encrypted); // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
const message = await openpgp.readMessage({
armoredMessage: encrypted // parse armored message
});
const { data: decrypted, signatures } = await openpgp.decrypt({
message,
verificationKeys: publicKey, // optional
decryptionKeys: privateKey
});
console.log(decrypted); // 'Hello, World!'
// check signature validity (signed messages only)
try {
await signatures[0].verified; // throws on invalid signature
console.log('Signature is valid');
} catch (e) {
throw new Error('Signature could not be verified: '
$ claude mcp add openpgpjs \
-- python -m otcore.mcp_server <graph>