How to use the openpgp.key function in openpgp

To help you get started, we’ve selected a few openpgp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / openpgp / openpgp-tests.ts View on Github external
name: 'Jon Smith',
    }, {
    }],
    passphrase: 'super long and hard to guess secret'
};
openpgp.generateKey(options).then((keypair) => {
    // success
    const privkey = keypair.privateKeyArmored;
    const pubkey = keypair.publicKeyArmored;
}).catch((error) => {
    // failure
});

const spubkey = '-----BEGIN PGP PUBLIC KEY BLOCK ... END PGP PUBLIC KEY BLOCK-----';

openpgp.key.readArmored(spubkey)
    .then((publicKey) => {
        return {
            message: openpgp.message.fromText('Hello, World!'),
            publicKeys: publicKey.keys
        };
    })
    .then(openpgp.encrypt)
    .then((pgpMessage) => {
        // success
    })
    .catch((error) => {
        // failure
    });

const sprivkey = '-----BEGIN PGP PRIVATE KEY BLOCK ... END PGP PRIVATE KEY BLOCK-----';
const pgpMessageStr = '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----';
github decentralized-identity / github-did / packages / lib / src / v2 / func / encryptForWithWalletAndResolver.js View on Github external
toPublicKeyId,
  wallet,
  resolver
}) => {
  const message = JSON.stringify(data);

  const toDidDoc = await resolver.resolve(toPublicKeyId);
  const publicKey = getPublicKeyFromDIDDoc(toDidDoc, toPublicKeyId);
  const fromKid = fromPublicKeyId.split("#").pop();
  const privateKey = (
    await openpgp.key.readArmored(wallet.keys[fromKid].privateKey)
  ).keys[0];

  const options = {
    message: openpgp.message.fromText(message), // input as String (or Uint8Array)
    publicKeys: (await openpgp.key.readArmored(publicKey)).keys, // for encryption
    privateKeys: [privateKey] // for signing (optional)
  };

  const cipherText = await openpgp.encrypt(options).then(ciphertext => {
    const encrypted = ciphertext.data; // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
    return encrypted;
  });

  return {
    fromPublicKeyId,
    toPublicKeyId,
    cipherText
  };
};
github decentralized-identity / github-did / packages / lib / src / v2 / func / decryptForWithWalletAndResolver.js View on Github external
const decryptForWithWalletAndResolver = async ({
  data,
  fromPublicKeyId,
  toPublicKeyId,
  wallet,
  resolver
}) => {
  const fromDidDoc = await resolver.resolve(fromPublicKeyId);
  const publicKey = getPublicKeyFromDIDDoc(fromDidDoc, fromPublicKeyId);
  const toKid = toPublicKeyId.split("#").pop();
  const privateKey = (
    await openpgp.key.readArmored(wallet.keys[toKid].privateKey)
  ).keys[0];

  const options = {
    message: await openpgp.message.readArmored(data), // parse armored message
    publicKeys: (await openpgp.key.readArmored(publicKey)).keys, // for verification (optional)
    privateKeys: [privateKey] // for decryption
  };

  const plainText = await openpgp
    .decrypt(options)
    .then(plaintext => plaintext.data);

  return JSON.parse(plainText);
};
github ProtonMail / WebClient / src / app / libraries / pmcrypto.js View on Github external
function getKeys(armoredKeys = '') {
        let keys;
        try {
            keys = openpgp.key.readArmored(armoredKeys);
        } catch (err) {
            return err;
        }

        if (keys === undefined) {
            return new Error('Cannot parse key(s)');
        }
        if (keys.err) {
            // openpgp.key.readArmored returns error arrays.
            return new Error(keys.err[0].message);
        }
        if (keys.keys.length < 1 || keys.keys[0] === undefined) {
            return new Error('Invalid key(s)');
        }

        return keys.keys;
github encryptic-team / encryptic / src / scripts / modules / Users.js View on Github external
saveInvite(options) {
        const {keys, err} = openpgp.key.readArmored(options.publicKey);

        // Wrong fingerprint
        if (err || keys[0].primaryKey.fingerprint !== options.fingerprint) {
            log('Ignore invite - wrong fingerprint');
            return Promise.resolve(false);
        }

        // If the user exists in the DB, automatically accept the invite
        return this.findModel({username: options.username})
        .then(model => {
            // Remove the invite from the server if the invite was already accepted
            if (!model.get('pendingAccept') && !model.get('pendingInvite')) {
                return this.removeServerInvite(model);
            }

            return this.autoAcceptInvite(options, keys, model);
github Mailtrain-org / mailtrain / routes / subscription.js View on Github external
settings.list(['pgpPassphrase', 'pgpPrivateKey'], (err, configItems) => {
        if (err) {
            return next(err);
        }
        if (!configItems.pgpPrivateKey) {
            err = new Error(_('Public key is not set'));
            err.status = 404;
            return next(err);
        }

        let privKey;
        try {
            privKey = openpgp.key.readArmored(configItems.pgpPrivateKey).keys[0];
            if (configItems.pgpPassphrase && !privKey.decrypt(configItems.pgpPassphrase)) {
                privKey = false;
            }
        } catch (E) {
            // just ignore if failed
        }

        if (!privKey) {
            err = new Error(_('Public key is not set'));
            err.status = 404;
            return next(err);
        }

        let pubkey = privKey.toPublic().armor();

        res.writeHead(200, {
github Laverna / laverna / test / tape / models / encryption.js View on Github external
test('models/Encryption: readKeys() - resolve', t => {
    const enc        = new Encryption();
    const read       = sand.stub(openpgp.key, 'readArmored');
    const privateKey = {decrypt: () => true, toPublic: () => 'pub'};
    read.returns({keys: [privateKey]});
    sand.stub(enc, 'readPublicKeys').returns(Promise.resolve(['pub']));

    Radio.replyOnce('collections/Configs', 'findConfigs', {username: 'bob'});

    enc.readKeys({privateKey: 'priv', publicKey: 'pub'})
    .then(res => {
        const keys = {
            privateKey,
            privateKeys: [privateKey],
            publicKeys : {bob: 'pub'},
        };
        t.deepEqual(res, keys, 'resolves with private and public keys');
        t.deepEqual(enc.keys, keys, 'creates "keys" property');
github scastiel / protonmail-export / src / main.js View on Github external
async function getPrivateKey(privateKeyFile: string): Promise {
    const privateKeyText = await fs.readFile(privateKeyFile, 'utf-8');
    return openpgp.key.readArmored(privateKeyText).keys[0];
}
github RedpointGames / pkgsign / src / lib / pgpVerifier.ts View on Github external
const attemptVerify = async (rawPublicKeys: string) => {
            try {
                const publicKeys = openpgp.key.readArmored(rawPublicKeys).keys;
                const verifyOptions = {
                    message: openpgp.message.fromText(deterministicString),
                    signature: openpgp.signature.readArmored(signature),
                    publicKeys: publicKeys
                };
                const verifiedMessage = await openpgp.verify(verifyOptions);
                return verifiedMessage.signatures.length >= 1;
            } catch (e) {
                return false;
            }
        }
github domschiener / identity-on-bitcoin / usecase-examples / token-authentication / app.js View on Github external
function gen_certificate(privkey_str) {
		var privkey = openpgp.key.readArmored(privkey_str).keys[0];
		var passphrase = 'this is the most secret password a super secret website can have';
		var pubkey = privkey.toPublic().armor();
		var date = new Date();

		var certificate = {
			'pubkey': pubkey,
			'info': {
				'company':'Simple Signup LLC',
				'address':'Phanstasie Street 123, London N54 F32, United Kingdom',
				'contact-mail':'Signup Example ',
				'website':'www.example.com',
				'trust-score':'88',
				'date': date
			},
		};