openpgpjs/openpgpjs

Name: openpgpjs

Owner: OpenPGP.js

Description: OpenPGP implementation for JavaScript

Created: 2011-11-13 10:36:43.0

Updated: 2018-01-19 02:57:50.0

Pushed: 2018-01-19 14:02:23.0

Homepage: https://openpgpjs.org

Size: 21827

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

OpenPGP.js Build Status

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

Saucelabs Test Status

Table of Contents

Platform Support
Performance

| Curve | Encryption | Signature | Elliptic | NodeCrypto | WebCrypto | |:———- |:———-:|:———:|:——–:|:———-:|:———:| | p256 | ECDH | ECDSA | Yes | Yes | Yes | | p384 | ECDH | ECDSA | Yes | Yes | Yes | | p521 | ECDH | ECDSA | Yes | Yes | Yes | | secp256k1 | ECDH | ECDSA | Yes | Yes* | No | | curve25519 | ECDH | N/A | Yes | No | No | | ed25519 | N/A | EdDSA | Yes | No | No |

Getting started
Npm
npm install --save openpgp
Bower
bower install --save openpgp

Or just fetch a minified build under dist.

Examples

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
openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via window.openpgp

pgp.initWorker({ path:'openpgp.worker.js' }) // set the relative web worker path

pgp.config.aead_protect = true // activate fast AES-GCM mode (not yet OpenPGP standard)
Encrypt and decrypt Uint8Array data with a password
options, encrypted;

ons = {
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)


pgp.encrypt(options).then(function(ciphertext) {
encrypted = ciphertext.message.packets.write(); // get raw encrypted packets as Uint8Array

s
ons = {
message: openpgp.message.read(encrypted), // parse encrypted bytes
password: 'secret stuff',                 // decrypt with password
format: 'binary'                          // output as Uint8Array


pgp.decrypt(options).then(function(plaintext) {
return plaintext.data // Uint8Array([0x01, 0x01, 0x01])

Encrypt and decrypt String data with PGP keys
options, encrypted;

pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----';
privkey = '-----BEGIN PGP PRIVATE KEY BLOCK ... END PGP PRIVATE KEY BLOCK-----'; //encrypted private key
passphrase = 'secret passphrase'; //what the privKey is encrypted with

privKeyObj = openpgp.key.readArmored(privkey).keys[0];
KeyObj.decrypt(passphrase);

ons = {
data: 'Hello, World!',                             // input as String (or Uint8Array)
publicKeys: openpgp.key.readArmored(pubkey).keys,  // for encryption
privateKeys: privKeyObj // for signing (optional)


pgp.encrypt(options).then(function(ciphertext) {
encrypted = ciphertext.data; // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'

s
ons = {
message: openpgp.message.readArmored(encrypted),     // parse armored message
publicKeys: openpgp.key.readArmored(pubkey).keys,    // for verification (optional)
privateKey: privKeyObj // for decryption


pgp.decrypt(options).then(function(plaintext) {
return plaintext.data; // 'Hello, World!'

Encrypt with compression

By default, encrypt will not use any compression. It's possible to override that behavior in two ways:

Either set the compression parameter in the options object when calling encrypt.

options, encrypted;

ons = {
data: new Uint8Array([0x01, 0x02, 0x03]),    // input as Uint8Array (or String)
passwords: ['secret stuff'],                 // multiple passwords possible
compression: openpgp.enums.compression.zip   // compress the data with zip


pgp.encrypt(options).then(function(ciphertext) {
// use ciphertext

Or, override the config to enable compression:

pgp.config.compression = openpgp.enums.compression.zip

Where compression can take the value of openpgp.enums.compression.zlib or openpgp.enums.compression.zip.

Generate new key pair

RSA keys:

options = {
userIds: [{ name:'Jon Smith', email:'jon@example.com' }], // multiple user IDs
numBits: 4096,                                            // RSA key size
passphrase: 'super long and hard to guess secret'         // protects the private key

ECC keys:

options = {
userIds: [{ name:'Jon Smith', email:'jon@example.com' }], // multiple user IDs
curve: "ed25519",                                         // ECC curve (curve25519, p256, p384, p521, or secp256k1)
passphrase: 'super long and hard to guess secret'         // protects the private key

s
pgp.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
hkp = new openpgp.HKP('https://pgp.mit.edu');

options = {
query: 'alice@example.com'


lookup(options).then(function(key) {
var pubkey = openpgp.key.readArmored(key);

Upload public key to HKP server
hkp = new openpgp.HKP('https://pgp.mit.edu');

pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----';

upload(pubkey).then(function() { ... });
Sign and verify cleartext messages
options, cleartext, validity;

pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----';
privkey = '-----BEGIN PGP PRIVATE KEY BLOCK ... END PGP PRIVATE KEY BLOCK-----'; //encrypted private key
passphrase = 'secret passphrase'; //what the privKey is encrypted with

privKeyObj = openpgp.key.readArmored(privkey).keys[0];
KeyObj.decrypt(passphrase);
s
ons = {
data: 'Hello, World!',                             // input as String (or Uint8Array)
privateKeys: privKeyObj // for signing


pgp.sign(options).then(function(signed) {
cleartext = signed.data; // '-----BEGIN PGP SIGNED MESSAGE ... END PGP SIGNATURE-----'

s
ons = {
message: openpgp.cleartext.readArmored(cleartext), // parse armored message
publicKeys: openpgp.key.readArmored(pubkey).keys   // for verification


pgp.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
options, detachedSig, validity;

pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----';
privkey = '-----BEGIN PGP PRIVATE KEY BLOCK ... END PGP PRIVATE KEY BLOCK-----'; //encrypted private key
passphrase = 'secret passphrase'; //what the privKey is encrypted with

privKeyObj = openpgp.key.readArmored(privkey).keys[0];
KeyObj.decrypt(passphrase);
s
ons = {
data: 'Hello, World!',                             // input as String (or Uint8Array)
privateKeys: privKeyObj, // for signing
detached: true


pgp.sign(options).then(function(signed) {
detachedSig = signed.signature;

s
ons = {
message: openpgp.message.fromText('Hello, World!'), // input as Message object
signature: openpgp.signature.readArmored(detachedSig), // parse detached signature
publicKeys: openpgp.key.readArmored(pubkey).keys   // for verification


pgp.verify(options).then(function(verified) {
validity = verified.signatures[0].valid; // true
if (validity) {
    console.log('signed by key id ' + verified.signatures[0].keyid.toHex());
}

Documentation

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.

Development

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

For debugging browser errors, you can open test/unittests.html in a browser or, after running the following command, open http://localhost:3000/test/unittests.html:

grunt browsertest
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.

License

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

Resources

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.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.