Created
July 10, 2016 11:54
-
-
Save ffflorian/b2327c75820574df706d016de9fd04d5 to your computer and use it in GitHub Desktop.
Simple OpenPGP.js example with Promises for node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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