How to use openpgp - 10 common examples

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 openpgpjs / openpgpjs / test / integration / pgp.js View on Github external
PGP.prototype.importKeys = function(options, callback) {
        var publicKey, privateKey;

        // check passphrase
        if (typeof options.passphrase !== 'string' || !options.privateKeyArmored || !options.publicKeyArmored) {
            callback({
                errMsg: 'Importing keys failed. Not all options set!'
            });
            return;
        }

        // clear any keypair already in the keychain
        openpgp.keyring.init();
        // unlock and import private key 
        if (!openpgp.keyring.importPrivateKey(options.privateKeyArmored, options.passphrase)) {
            openpgp.keyring.init();
            callback({
                errMsg: 'Incorrect passphrase!'
            });
            return;
        }
        // import public key
        openpgp.keyring.importPublicKey(options.publicKeyArmored);

        // check if keys have the same id
        privateKey = openpgp.keyring.exportPrivateKey(0);
        publicKey = openpgp.keyring.getPublicKeysForKeyId(privateKey.keyId)[0];
        if (!privateKey || !privateKey.armored || !publicKey || !publicKey.armored || privateKey.keyId !== publicKey.keyId) {
            // reset keyring
github openpgpjs / openpgpjs / test / integration / pgp.js View on Github external
// clear any keypair already in the keychain
        openpgp.keyring.init();
        // unlock and import private key 
        if (!openpgp.keyring.importPrivateKey(options.privateKeyArmored, options.passphrase)) {
            openpgp.keyring.init();
            callback({
                errMsg: 'Incorrect passphrase!'
            });
            return;
        }
        // import public key
        openpgp.keyring.importPublicKey(options.publicKeyArmored);

        // check if keys have the same id
        privateKey = openpgp.keyring.exportPrivateKey(0);
        publicKey = openpgp.keyring.getPublicKeysForKeyId(privateKey.keyId)[0];
        if (!privateKey || !privateKey.armored || !publicKey || !publicKey.armored || privateKey.keyId !== publicKey.keyId) {
            // reset keyring
            openpgp.keyring.init();
            callback({
                errMsg: 'Key IDs dont match!'
            });
            return;
        }

        callback();
    };
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 DefinitelyTyped / DefinitelyTyped / types / openpgp / openpgp-tests.ts View on Github external
openpgp.message.readArmored(pgpMessageStr).then((pgpMessage) => {
    const options = {
        message: pgpMessage
    };
    return openpgp.decrypt(options);
}).then((plaintext) => {
    // success
github autocrypt / autocrypt-js / test / util.js View on Github external
setup: function (fromAddr, cb) {
    var crypt = autocrypt({storage: memdb({valueEncoding: 'json'})})
    openpgp.initWorker({ path: 'openpgp.worker.js' }) // set the relative web worker path
    openpgp.config.aead_protect = true // activate fast AES-GCM mode (not yet OpenPGP standard)

    openpgp.generateKey({
      userIds: [{ name: 'Jon Smith', email: fromAddr }],
      numBits: 1096,
      passphrase: 'super long and hard to guess'
    }
    ).then((key) => cb(crypt, decode(key.publicKeyArmored), done)
  ).catch((err) => { throw err })

    function done (cb) {
      crypt.storage.close(cb)
    }
  },
  decode: decode
github DefinitelyTyped / DefinitelyTyped / types / openpgp / openpgp-tests.ts View on Github external
detached: true
    }).then(s => s.signature/* as openpgp.signature.Signature*/);
    openpgp.sign({
        message: null,
        privateKeys: [],
        armor: false,
        detached: false,
    }).then(s => s.message/* as openpgp.message.Message*/);

    const verifyOptions: openpgp.VerifyOptions = {
        message,
        signature,
        publicKeys: publicKey.keys
    };

    const verified = await openpgp.verify(verifyOptions);

    return verified.signatures[0].valid;
})();
github autocrypt / autocrypt-js / test / util.js View on Github external
setup: function (fromAddr, cb) {
    var crypt = autocrypt({storage: memdb({valueEncoding: 'json'})})
    openpgp.initWorker({ path: 'openpgp.worker.js' }) // set the relative web worker path
    openpgp.config.aead_protect = true // activate fast AES-GCM mode (not yet OpenPGP standard)

    openpgp.generateKey({
      userIds: [{ name: 'Jon Smith', email: fromAddr }],
      numBits: 1096,
      passphrase: 'super long and hard to guess'
    }
    ).then((key) => cb(crypt, decode(key.publicKeyArmored), done)
  ).catch((err) => { throw err })

    function done (cb) {
      crypt.storage.close(cb)
    }
  },
  decode: decode
github DefinitelyTyped / DefinitelyTyped / types / openpgp / ts3.2 / openpgp-tests.ts View on Github external
const publicKey = (await openpgp.key.readArmored(spubkey));

    return publicKey.keys[0].primaryKey.getFingerprint()/* as string*/
})

// Open PGP Tests


var keyoptions: openpgp.KeyOptions;
var mpi: openpgp.type.mpi.MPI;
var mpis: Array;

openpgp.encoding.armor.armor(openpgp.enums.armor.message, {}, 0, 1);
openpgp.encoding.armor.dearmor("");

openpgp.cleartext.readArmored("");

openpgp.crypto.crypto.generateSessionKey(openpgp.enums.symmetric.aes128);
openpgp.crypto.crypto.getPrefixRandom(openpgp.enums.symmetric.aes128);
// openpgp.crypto.crypto.getPrivateMpiCount(openpgp.enums.symmetric.aes128);
openpgp.crypto.crypto.publicKeyDecrypt(openpgp.enums.publicKey.rsa_encrypt, mpis, mpis, "");
openpgp.crypto.crypto.publicKeyEncrypt(openpgp.enums.publicKey.rsa_encrypt, mpis, mpi, "");

openpgp.crypto
// API update with no documentation
openpgp.crypto.cfb.decrypt("", "", "", true);
openpgp.crypto.cfb.encrypt("", "", "", true);
// Function removed from openpgp.crypto.cfb
// openpgp.crypto.cfb.mdc({}, "", "");

openpgp.crypto.hash.digest(openpgp.enums.hash.md5, new Uint8Array([0, 1]));
openpgp.crypto.hash.getHashByteLength(openpgp.enums.hash.md5);
github DefinitelyTyped / DefinitelyTyped / types / openpgp / openpgp-tests.ts View on Github external
openpgp.cleartext.readArmored("");

openpgp.crypto.crypto.generateSessionKey(openpgp.enums.symmetric.aes128);
openpgp.crypto.crypto.getPrefixRandom(openpgp.enums.symmetric.aes128);
// openpgp.crypto.crypto.getPrivateMpiCount(openpgp.enums.symmetric.aes128);
openpgp.crypto.crypto.publicKeyDecrypt(openpgp.enums.publicKey.rsa_encrypt, mpis, mpis, "");
openpgp.crypto.crypto.publicKeyEncrypt(openpgp.enums.publicKey.rsa_encrypt, mpis, mpi, "");

openpgp.crypto;
// API update with no documentation
openpgp.crypto.cfb.decrypt("", "", "", true);
openpgp.crypto.cfb.encrypt("", "", "", true);
// Function removed from openpgp.crypto.cfb
// openpgp.crypto.cfb.mdc({}, "", "");

openpgp.crypto.hash.digest(openpgp.enums.hash.md5, new Uint8Array([0, 1]));
openpgp.crypto.hash.getHashByteLength(openpgp.enums.hash.md5);

openpgp.crypto.random.getRandomBN(mpi, mpi);
openpgp.crypto.random.getRandomBytes(0);
// function removed from openpgp.crypto.random
// openpgp.crypto.random.getRandomValues(openpgp.util.str_to_Uint8Array(""));
// openpgp.crypto.random.getSecureRandom(0, 1);

openpgp.crypto.signature.sign(openpgp.enums.publicKey.rsa_encrypt, openpgp.enums.hash.md5, mpis, new Uint8Array([0, 1]), new Uint8Array([0, 1]));
openpgp.crypto.signature.verify(openpgp.enums.publicKey.rsa_encrypt, openpgp.enums.hash.md5, mpis, mpis, new Uint8Array([0, 1]), new Uint8Array([0, 1]));

openpgp.key.generate(keyoptions);
openpgp.key.readArmored("");

openpgp.message.fromBinary(new Uint8Array([0x01, 0x02, 0x03]));
openpgp.message.fromText("");
github DefinitelyTyped / DefinitelyTyped / types / openpgp / ts3.2 / openpgp-tests.ts View on Github external
return publicKey.keys[0].primaryKey.getFingerprint()/* as string*/
})

// Open PGP Tests


var keyoptions: openpgp.KeyOptions;
var mpi: openpgp.type.mpi.MPI;
var mpis: Array;

openpgp.encoding.armor.armor(openpgp.enums.armor.message, {}, 0, 1);
openpgp.encoding.armor.dearmor("");

openpgp.cleartext.readArmored("");

openpgp.crypto.crypto.generateSessionKey(openpgp.enums.symmetric.aes128);
openpgp.crypto.crypto.getPrefixRandom(openpgp.enums.symmetric.aes128);
// openpgp.crypto.crypto.getPrivateMpiCount(openpgp.enums.symmetric.aes128);
openpgp.crypto.crypto.publicKeyDecrypt(openpgp.enums.publicKey.rsa_encrypt, mpis, mpis, "");
openpgp.crypto.crypto.publicKeyEncrypt(openpgp.enums.publicKey.rsa_encrypt, mpis, mpi, "");

openpgp.crypto
// API update with no documentation
openpgp.crypto.cfb.decrypt("", "", "", true);
openpgp.crypto.cfb.encrypt("", "", "", true);
// Function removed from openpgp.crypto.cfb
// openpgp.crypto.cfb.mdc({}, "", "");

openpgp.crypto.hash.digest(openpgp.enums.hash.md5, new Uint8Array([0, 1]));
openpgp.crypto.hash.getHashByteLength(openpgp.enums.hash.md5);

openpgp.crypto.random.getRandomBN(mpi, mpi);