Skip to content

Instantly share code, notes, and snippets.

@ffflorian
Created July 10, 2016 11:54
Show Gist options
  • Save ffflorian/b2327c75820574df706d016de9fd04d5 to your computer and use it in GitHub Desktop.
Save ffflorian/b2327c75820574df706d016de9fd04d5 to your computer and use it in GitHub Desktop.
Simple OpenPGP.js example with Promises for node.js
/*
* Simple OpenPGP.js example with Promises for node.js
* See: https://github.com/openpgpjs/openpgpjs
*/
'use strict';
const fs = require('fs');
const openpgp = require('openpgp');
const keyFile = 'pubkey.asc';
openpgp.initWorker({ path: 'openpgp.worker.js' });
/*
* Enable AES-GCM mode only if your encryption tool (e.g. GnuPG)
* supports it, since it's not OpenPGP standard yet:
* openpgp.config.aead_protect = true
*/
new Promise((resolve, reject) => {
fs.readFile(keyFile, (error, file) => {
if (error !== null) {
return reject(error);
}
return resolve(file);
});
}).then((buffer) => {
let pubkey = buffer.toString();
let options = {
data: 'Hello world!',
publicKeys: openpgp.key.readArmored(pubkey).keys,
};
return openpgp.encrypt(options);
}).then((ciphertext) => {
console.log(ciphertext.data);
}).catch((error) => {
console.log(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment