Merge pull request #703 from nguyendviet/master

new Encrypt and decrypt String data with PGP keys example
This commit is contained in:
Sanjana Rajan 2018-05-28 15:17:24 -07:00 committed by GitHub
commit be26302d99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -135,36 +135,49 @@ openpgp.decrypt(options).then(function(plaintext) {
#### Encrypt and decrypt *String* data with PGP keys #### Encrypt and decrypt *String* data with PGP keys
```js ```js
var options, encrypted; const openpgp = require('openpgp') // use as CommonJS, AMD, ES6 module or via window.openpgp
var pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----'; openpgp.initWorker({ path:'openpgp.worker.js' }) // set the relative web worker path
var privkey = '-----BEGIN PGP PRIVATE KEY BLOCK ... END PGP PRIVATE KEY BLOCK-----'; //encrypted private key
var passphrase = 'secret passphrase'; //what the privKey is encrypted with
var privKeyObj = openpgp.key.readArmored(privkey).keys[0]; // put keys in backtick (``) to avoid errors caused by spaces or tabs
await privKeyObj.decrypt(passphrase); const pubkey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----`
const privkey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----` //encrypted private key
const passphrase = `yourPassphrase` //what the privKey is encrypted with
options = { const encryptDecryptFunction = async() => {
data: 'Hello, World!', // input as String (or Uint8Array) const privKeyObj = openpgp.key.readArmored(privkey).keys[0]
publicKeys: openpgp.key.readArmored(pubkey).keys, // for encryption await privKeyObj.decrypt(passphrase)
privateKeys: [privKeyObj] // for signing (optional)
};
openpgp.encrypt(options).then(function(ciphertext) { const options = {
encrypted = ciphertext.data; // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----' data: 'Hello, World!', // input as String (or Uint8Array)
}); publicKeys: openpgp.key.readArmored(pubkey).keys, // for encryption
``` privateKeys: [privKeyObj] // for signing (optional)
}
```js openpgp.encrypt(options).then(ciphertext => {
options = { encrypted = ciphertext.data // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
message: openpgp.message.readArmored(encrypted), // parse armored message return encrypted
publicKeys: openpgp.key.readArmored(pubkey).keys, // for verification (optional) })
privateKeys: [privKeyObj] // for decryption .then(encrypted => {
}; const options = {
message: openpgp.message.readArmored(encrypted), // parse armored message
publicKeys: openpgp.key.readArmored(pubkey).keys, // for verification (optional)
privateKeys: [privKeyObj] // for decryption
}
openpgp.decrypt(options).then(function(plaintext) { openpgp.decrypt(options).then(plaintext => {
return plaintext.data; // 'Hello, World!' console.log(plaintext.data)
}); return plaintext.data // 'Hello, World!'
})
})
}
encryptDecryptFunction()
``` ```
#### Encrypt with compression #### Encrypt with compression