node package manager


OpenPGP.js Build Status

OpenPGP.js is a Javascript implementation of the OpenPGP protocol. This is defined in RFC 4880.

Saucelabs Test Status

Platform support

  • OpenPGP.js v2.x is written in ES6 but is transpiled to ES5 using Babel to run in most environments. We support node.js v0.12+ and browsers that implement window.crypto.getRandomValues.

  • The api uses ES6 promises which are available in most modern browsers. If you need to support browsers that do not support Promises, fear not! There is a polyfill, which is included in our build. So no action required on your part!

  • For the OpenPGP HTTP Key Server (HKP) client the new fetch api is used. There is a polyfill for both browsers and node.js runtimes. The node module is included as a dependency if you install via npm, but we do not include the browser polyfill in our build. So you'll need to include it in your app if you intend to use the HKP client.


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

  • If the user's browser supports native WebCrypto via the window.crypto.subtle api, this will be used. Under node.js the native crypto module is used. This can be deactivated by setting openpgp.config.use_native = false.

  • The library implements the IETF proposal for authenticated encryption using native AES-GCM. This makes symmetric encryption about 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. You can activate it by setting openpgp.config.aead_protect = true.

  • For environments that don't provide native crypto, the library falls back to asm.js implementations of AES, SHA-1, and SHA-256. We use Rusha and asmCrypto Lite (a minimal subset of asmCrypto.js built specifically for OpenPGP.js).

Getting started


npm install --save openpgp


bower install --save openpgp

Or just fetch a minified build under dist.


Here are some examples of how to use the v2.x api. For more elaborate examples and working code, please check out the public api unit tests. If you're upgrading from v1.x it might help to check out the documentation.

Set up

var openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via window.openpgp 
openpgp.initWorker({ path:'openpgp.worker.js' }) // set the relative web worker path 
openpgp.config.aead_protect = true // activate fast AES-GCM mode (not yet OpenPGP standard) 

Encrypt and decrypt Uint8Array data with a password

var options, encrypted;
options = {
    data: new Uint8Array([0x01, 0x01, 0x01]), // input as Uint8Array (or String) 
    passwords: ['secret stuff'],              // multiple passwords possible 
    armor: false                              // don't ASCII armor (for Uint8Array output) 
openpgp.encrypt(options).then(function(ciphertext) {
    encrypted = ciphertext.message.packets.write(); // get raw encrypted packets as Uint8Array 
options = {
    message:, // parse encrypted bytes 
    password: 'secret stuff',                 // decrypt with password 
    format: 'binary'                          // output as Uint8Array 
openpgp.decrypt(options).then(function(plaintext) {
    return // Uint8Array([0x01, 0x01, 0x01]) 

Encrypt and decrypt String data with PGP keys

var options, encrypted;
options = {
    data: 'Hello, World!',                             // input as String (or Uint8Array) 
    publicKeys: openpgp.key.readArmored(pubkey).keys,  // for encryption 
    privateKeys: openpgp.key.readArmored(privkey).keys // for signing (optional) 
openpgp.encrypt(options).then(function(ciphertext) {
    encrypted =; // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----' 
options = {
    message: openpgp.message.readArmored(encrypted),     // parse armored message 
    publicKeys: openpgp.key.readArmored(pubkey).keys,    // for verification (optional) 
    privateKey: openpgp.key.readArmored(privkey).keys[0] // for decryption 
openpgp.decrypt(options).then(function(plaintext) {
    return; // 'Hello, World!' 

Generate new key pair

var options = {
    userIds: [{ name:'Jon Smith', email:'' }], // multiple user IDs 
    numBits: 4096,                                            // RSA key size 
    passphrase: 'super long and hard to guess secret'         // protects the private key 
openpgp.generateKey(options).then(function(key) {
    var privkey = key.privateKeyArmored; // '-----BEGIN PGP PRIVATE KEY BLOCK ... ' 
    var pubkey = key.publicKeyArmored;   // '-----BEGIN PGP PUBLIC KEY BLOCK ... ' 

Lookup public key on HKP server

var hkp = new openpgp.HKP('');
var options = {
    query: ''
hkp.lookup(options).then(function(key) {
    var pubkey = openpgp.key.readArmored(key);

Upload public key to HKP server

var hkp = new openpgp.HKP('');
hkp.upload(pubkey).then(function() { ... });

Sign and verify cleartext messages

var options, cleartext, validity;
options = {
    data: 'Hello, World!',                             // input as String (or Uint8Array) 
    privateKeys: openpgp.key.readArmored(privkey).keys // for signing 
openpgp.sign(options).then(function(signed) {
    cleartext =; // '-----BEGIN PGP SIGNED MESSAGE ... END PGP SIGNATURE-----' 
options = {
    message: openpgp.cleartext.readArmored(cleartext), // parse armored message 
    publicKeys: openpgp.key.readArmored(pubkey).keys   // for verification 
openpgp.verify(options).then(function(verified) {
    validity = verified.signatures[0].valid; // true 
    if (validity) {
        console.log('signed by key id ' + verified.signatures[0].keyid.toHex());

Create and verify detached signatures for binary data

var content, detachedSig, validity;
content = 'Hello, World!';                               // input as String 
var privateKeys = openpgp.key.readArmored(privkey).keys; // for signing 
var bytes = openpgp.util.str2Uint8Array(content);        // convert text to binary 
var message = openpgp.message.fromBinary(bytes);
var signedMessage = message.sign(privateKeys);
var signature = signedMessage.packets.filterByTag(openpgp.enums.packet.signature);
var armoredMessage = openpgp.armor.encode(openpgp.enums.armor.message, signature.write());
armoredMessage = armoredMessage.replace('-----BEGIN PGP MESSAGE-----\r\n', '-----BEGIN PGP SIGNATURE-----\r\n');
armoredMessage = armoredMessage.replace('-----END PGP MESSAGE-----\r\n', '-----END PGP SIGNATURE-----\r\n');
detachedSig = armoredMessage; // '-----BEGIN PGP SIGNATURE ... END PGP SIGNATURE-----' 
var publicKeys = openpgp.key.readArmored(pubkey).keys; // for verifying signatures 
var msg = openpgp.message.readSignedContent(content, detachedSig);
var result = msg.verify(publicKeys);
validity = result[0].valid; // true 


A jsdoc build of our code comments is available at doc/index.html. Public calls should generally be made through the OpenPGP object doc/openpgp.html.

Security Audit

To date the OpenPGP.js code base has undergone two complete security audits from Cure53. The first audit's report has been published here.

Security recommendations

It should be noted that js crypto apps deployed via regular web hosting (a.k.a. host-based security) provide users with less security than installable apps with auditable static versions. Installable apps can be deployed as a Firefox or Chrome packaged app. These apps are basically signed zip files and their runtimes typically enforce a strict Content Security Policy (CSP) to protect users against XSS. This blogpost explains the trust model of the web quite well.

It is also recommended to set a strong passphrase that protects the user's private key on disk.


To create your own build of the library, just run the following command after cloning the git repo. This will download all dependencies, run the tests and create a minified bundle under dist/openpgp.min.js to use in your project:

npm install && npm test

How do I get involved?

You want to help, great! Go ahead and fork our repo, make your changes and send us a pull request.


GNU Lesser General Public License (3.0 or any later version). Please take a look at the LICENSE file for more information.


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.