MCPcopy Index your code
hub / github.com/openpgpjs/openpgpjs

github.com/openpgpjs/openpgpjs @v6.3.1 sqlite

repository ↗ · DeepWiki ↗ · release v6.3.1 ↗
881 symbols 2,452 edges 153 files 273 documented · 31%
README

OpenPGP.js Join the chat on Gitter

OpenPGP.js is a JavaScript implementation of the OpenPGP protocol. It implements RFC 9580 (superseding RFC 4880 and RFC 4880bis).

Table of Contents

Platform Support

  • 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.

  • In browsers, 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.

  • In browsers: the latest versions of Chrome, Firefox, Edge and Safari support Streams, including 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.
  • In Node.js: OpenPGP.js v6 no longer supports native Node Readable streams in inputs, and instead expects (and outputs) Node's Web Streams. Node v17+ includes utilities to convert from and to Web Streams.

Performance

  • 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

Getting started

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');

Deno (experimental)

Import as an ES6 module, using /dist/openpgp.mjs.

import * as openpgp from './openpgpjs/dist/openpgp.mjs';

Browser (webpack)

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.

Browser (plain files)

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.

TypeScript

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.

Updating from older versions of the library

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.

Code examples

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.

Encrypt and decrypt Uint8Array data with a password

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])
})();

Encrypt and decrypt String data with PGP keys

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: '

Extension points exported contracts — how you extend this code

Config (Interface)
(no doc)
src/config.ts
PrimaryUser (Interface)
(no doc)
src/index.d.ts
PartialConfig (Interface)
(no doc)
src/config.ts
VerificationResult (Interface)
(no doc)
src/index.d.ts
RawSubpacket (Interface)
(no doc)
src/index.d.ts
RawNotation (Interface)
(no doc)
src/index.d.ts
UserID (Interface)
(no doc)
src/index.d.ts

Core symbols most depended-on inside this repo

decrypt
called by 219
src/message.js
encrypt
called by 209
src/message.js
verify
called by 190
src/key/user.js
toHex
called by 153
src/type/oid.js
sign
called by 133
src/message.js
getKeyID
called by 122
src/packet/public_key.js
getAlgorithmInfo
called by 90
src/packet/public_key.js
write
called by 85
src/key/key.js

Shape

Function 406
Method 292
Class 147
Interface 21
Enum 15

Languages

TypeScript100%

Modules by API surface

src/index.d.ts74 symbols
src/message.js30 symbols
src/crypto/cipherMode/cfb.js26 symbols
src/key/key.js24 symbols
src/packet/signature.js22 symbols
src/openpgp.js22 symbols
src/packet/packet.js20 symbols
src/crypto/cipher/twofish.js20 symbols
src/key/helper.js19 symbols
src/crypto/public_key/rsa.js19 symbols
src/packet/public_key.js17 symbols
src/packet/secret_key.js16 symbols

Dependencies from manifests, versioned

@eslint/js9.39.3 · 1×
@noble/ciphers1.3.0 · 1×
@noble/curves1.9.7 · 1×
@noble/hashes1.8.0 · 1×
@openpgp/jsdoc4.0.4 · 1×
@openpgp/tweetnacl1.0.4-2 · 1×
@openpgp/unbzip2-stream2.0.0 · 1×
@openpgp/web-stream-tools0.3.1 · 1×
@rollup/plugin-alias6.0.0 · 1×
@rollup/plugin-commonjs29.0.3 · 1×
@rollup/plugin-node-resolve16.0.3 · 1×
@rollup/plugin-replace6.0.3 · 1×

For agents

$ claude mcp add openpgpjs \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact