From ee1bcce275241d5e12cb15f5867cd0226c2bca2e Mon Sep 17 00:00:00 2001 From: Stanislav Mikhaylov Date: Mon, 8 Oct 2018 18:23:54 +0300 Subject: [PATCH 1/3] Multiple public keys code Update README.md with the code example by @avimar (https://github.com/openpgpjs/openpgpjs/issues/271#issuecomment-274901731). Think that it's too hard to find and missed in documentation. --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 88f532f4..1769cec2 100644 --- a/README.md +++ b/README.md @@ -344,6 +344,16 @@ openpgp.generateKey(options).then(function(key) { }); ``` +#### Encrypt with multiple public keys + +```js + publicKeys: [ // for encryption + openpgp.key.readArmored(pubkey1).keys[0], + openpgp.key.readArmored(pubkey2).keys[0] + ] + }; +``` + #### Revoke a key Using a revocation signature: From 9f8c93dc0445e1ff15112ae6e59c41e37d1bd0df Mon Sep 17 00:00:00 2001 From: Stanislav Mikhaylov Date: Tue, 9 Oct 2018 10:51:38 +0300 Subject: [PATCH 2/3] Multiple public keys example --- README.md | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1769cec2..5e4a7389 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,40 @@ const encryptDecryptFunction = async() => { encryptDecryptFunction() ``` +Encrypt with multiple public keys: + +```js +const pubkey1 = `-----BEGIN PGP PUBLIC KEY BLOCK----- +... +-----END PGP PUBLIC KEY BLOCK-----` +const pubkey2 = `-----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 + +const encryptWithMultiplePublicKeys = async() => { + const privKeyObj = (await openpgp.key.readArmored(privkey)).keys[0] + await privKeyObj.decrypt(passphrase) + + const options = { + message: openpgp.message.fromText('Hello, World!'), // input as Message object + publicKeys: [ // for encryption + openpgp.key.readArmored(pubkey1).keys[0], + openpgp.key.readArmored(pubkey2).keys[0] + ], + privateKeys: [privKeyObj] // for signing (optional) + } + + openpgp.encrypt(options).then(ciphertext => { + encrypted = ciphertext.data // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----' + return encrypted + }) + }; +``` + #### Encrypt with compression By default, `encrypt` will not use any compression. It's possible to override that behavior in two ways: @@ -344,16 +378,6 @@ openpgp.generateKey(options).then(function(key) { }); ``` -#### Encrypt with multiple public keys - -```js - publicKeys: [ // for encryption - openpgp.key.readArmored(pubkey1).keys[0], - openpgp.key.readArmored(pubkey2).keys[0] - ] - }; -``` - #### Revoke a key Using a revocation signature: From a4276677b8e101198ed412e16bea8f4c5044a0e2 Mon Sep 17 00:00:00 2001 From: Stanislav Mikhaylov Date: Tue, 9 Oct 2018 11:10:27 +0300 Subject: [PATCH 3/3] Update function to be clear Update function to be clear and without hardcoded payload --- README.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 5e4a7389..d8892a0a 100644 --- a/README.md +++ b/README.md @@ -201,31 +201,33 @@ encryptDecryptFunction() Encrypt with multiple public keys: ```js -const pubkey1 = `-----BEGIN PGP PUBLIC KEY BLOCK----- +const pubkeys = [`-----BEGIN PGP PUBLIC KEY BLOCK----- ... ------END PGP PUBLIC KEY BLOCK-----` -const pubkey2 = `-----BEGIN PGP PUBLIC KEY BLOCK----- +-----END PGP PUBLIC KEY BLOCK-----`, +`-----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 +const message = 'Hello, World!' // input as Message object -const encryptWithMultiplePublicKeys = async() => { +async encryptWithMultiplePublicKeys(pubkeys, privkey, passphrase, message) { const privKeyObj = (await openpgp.key.readArmored(privkey)).keys[0] await privKeyObj.decrypt(passphrase) + pubkeys = pubkeys.map(async (key) => { + return (await openpgp.key.readArmored(key)).keys[0] + }); + const options = { - message: openpgp.message.fromText('Hello, World!'), // input as Message object - publicKeys: [ // for encryption - openpgp.key.readArmored(pubkey1).keys[0], - openpgp.key.readArmored(pubkey2).keys[0] - ], + message: openpgp.message.fromText(message), + publicKeys: pubkeys, // for encryption privateKeys: [privKeyObj] // for signing (optional) } - openpgp.encrypt(options).then(ciphertext => { + return openpgp.encrypt(options).then(ciphertext => { encrypted = ciphertext.data // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----' return encrypted })