Merge pull request #703 from nguyendviet/master
new Encrypt and decrypt String data with PGP keys example
This commit is contained in:
commit
be26302d99
51
README.md
51
README.md
|
@ -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() => {
|
||||||
|
const privKeyObj = openpgp.key.readArmored(privkey).keys[0]
|
||||||
|
await privKeyObj.decrypt(passphrase)
|
||||||
|
|
||||||
|
const options = {
|
||||||
data: 'Hello, World!', // input as String (or Uint8Array)
|
data: 'Hello, World!', // input as String (or Uint8Array)
|
||||||
publicKeys: openpgp.key.readArmored(pubkey).keys, // for encryption
|
publicKeys: openpgp.key.readArmored(pubkey).keys, // for encryption
|
||||||
privateKeys: [privKeyObj] // for signing (optional)
|
privateKeys: [privKeyObj] // for signing (optional)
|
||||||
};
|
}
|
||||||
|
|
||||||
openpgp.encrypt(options).then(function(ciphertext) {
|
openpgp.encrypt(options).then(ciphertext => {
|
||||||
encrypted = ciphertext.data; // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
|
encrypted = ciphertext.data // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
|
||||||
});
|
return encrypted
|
||||||
```
|
})
|
||||||
|
.then(encrypted => {
|
||||||
```js
|
const options = {
|
||||||
options = {
|
|
||||||
message: openpgp.message.readArmored(encrypted), // parse armored message
|
message: openpgp.message.readArmored(encrypted), // parse armored message
|
||||||
publicKeys: openpgp.key.readArmored(pubkey).keys, // for verification (optional)
|
publicKeys: openpgp.key.readArmored(pubkey).keys, // for verification (optional)
|
||||||
privateKeys: [privKeyObj] // for decryption
|
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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user