How to use the openpgp.util 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 isomorphic-git / isomorphic-git / src / models / PGP.js View on Github external
static async createBinaryDetachedSignature (email, plaintext) {
    // Load keypair from localstorage
    const privateKey = PGP.lookupPrivateKey(email)
    if (privateKey) {
      // Is the only difference between cleartext signatures and detached binary the text normalization?
      // If so, I could probably add that functionality to openpgpjs - I'd just need a little guidance
      // on how to encode the PacketType and add the functionality to export to armor.js
      const bytes = openpgp.util.str2Uint8Array(plaintext)
      const message = openpgp.message.fromBinary(bytes)
      const signedMessage = message.sign([privateKey])
      const signature = signedMessage.packets.filterByTag(
        openpgp.enums.packet.signature
      )
      let armoredMessage = openpgp.armor.encode(
        openpgp.enums.armor.message,
        signature.write()
      )
      // Github won't recognize the signature unless we rename the headers (Tested 2017-01-04)
      armoredMessage = armoredMessage.replace(
        '-----BEGIN PGP MESSAGE-----\r\n',
        '-----BEGIN PGP SIGNATURE-----\r\n'
      )
      armoredMessage = armoredMessage.replace(
        '-----END PGP MESSAGE-----\r\n',
github ProtonMail / WebClient / src / app / libraries / pmcrypto.js View on Github external
// Login page
        getHashedPassword,

        // Javascript string to/from base64-encoded and/or UTF8
        encode_utf8,
        decode_utf8,
        encode_base64,
        decode_base64,
        encode_utf8_base64,
        decode_utf8_base64,

        // Typed array/binary string conversions
        arrayToBinaryString,
        binaryStringToArray,
        concatArrays: openpgp.util.concatUint8Array,

        // Split existing encrypted file into data and non-data parts
        splitFile,

        // Dump key information
        keyInfo,
        keyCheck
    };
}());
github openpgpjs / openpgpjs / test / crypto / hash / ripemd.js View on Github external
'use strict';

var openpgp = require('openpgp'),
  util = openpgp.util,
  RMDstring = openpgp.crypto.hash.ripemd,
  chai = require('chai'),
  expect = chai.expect;

it("RIPE-MD 160 bits with test vectors from http://homes.esat.kuleuven.be/~bosselae/ripemd160.html", function(done) {
	expect(util.hexstrdump(RMDstring('')), 'RMDstring("") = 9c1185a5c5e9fc54612808977ee8f548b2258d31').to.equal('9c1185a5c5e9fc54612808977ee8f548b2258d31');
  expect(util.hexstrdump(RMDstring('a')), 'RMDstring("a") = 0bdc9d2d256b3ee9daae347be6f4dc835a467ffe').to.equal('0bdc9d2d256b3ee9daae347be6f4dc835a467ffe');
  expect(util.hexstrdump(RMDstring('abc')), 'RMDstring("abc") = 8eb208f7e05d987a9b044a8e98c6b087f15a0bfc').to.equal('8eb208f7e05d987a9b044a8e98c6b087f15a0bfc');
  expect(util.hexstrdump(RMDstring('message digest')), 'RMDstring("message digest") = 5d0689ef49d2fae572b881b123a85ffa21595f36').to.equal('5d0689ef49d2fae572b881b123a85ffa21595f36');
	done();
});
github openpgpjs / openpgpjs / test / crypto / cipher / blowfish.js View on Github external
'use strict';

var openpgp = require('openpgp'),
  util = openpgp.util,
  BFencrypt = openpgp.crypto.cipher.blowfish,
  chai = require('chai'),
  expect = chai.expect;

it('Blowfish cipher test with test vectors from http://www.schneier.com/code/vectors.txt', function(done) {
  function test_bf(input, key, output) {
    var blowfish = new openpgp.crypto.cipher.blowfish(util.bin2str(key));
    var result = util.bin2str(blowfish.encrypt(input));

    return (util.hexstrdump(result) == util.hexstrdump(util.bin2str(output)));
  }

  var testvectors = [[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x4E,0xF9,0x97,0x45,0x61,0x98,0xDD,0x78]],
                     [[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],[0x51,0x86,0x6F,0xD5,0xB8,0x5E,0xCB,0x8A]],
                     [[0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00],[0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x01],[0x7D,0x85,0x6F,0x9A,0x61,0x30,0x63,0xF2]],
                     [[0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11],[0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11],[0x24,0x66,0xDD,0x87,0x8B,0x96,0x3C,0x9D]],
github openpgpjs / openpgpjs / test / crypto / cipher / twofish.js View on Github external
'use strict';

var openpgp = require('openpgp'),
  util = openpgp.util,
  chai = require('chai'),
  expect = chai.expect;

it('Twofish with test vectors from http://www.schneier.com/code/ecb_ival.txt', function(done) {
  function TFencrypt(block, key) {
    var tf = new openpgp.crypto.cipher.twofish(key);

    return tf.encrypt(block);
  }

  var start = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  var start_short = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  var testvectors = [[0x57,0xFF,0x73,0x9D,0x4D,0xC9,0x2C,0x1B,0xD7,0xFC,0x01,0x70,0x0C,0xC8,0x21,0x6F],
            [0xD4,0x3B,0xB7,0x55,0x6E,0xA3,0x2E,0x46,0xF2,0xA2,0x82,0xB7,0xD4,0x5B,0x4E,0x0D],
            [0x90,0xAF,0xE9,0x1B,0xB2,0x88,0x54,0x4F,0x2C,0x32,0xDC,0x23,0x9B,0x26,0x35,0xE6],
            [0x6C,0xB4,0x56,0x1C,0x40,0xBF,0x0A,0x97,0x05,0x93,0x1C,0xB6,0xD4,0x08,0xE7,0xFA],
github openpgpjs / openpgpjs / test / crypto / hash / sha.js View on Github external
'use strict';

var openpgp = require('openpgp'),
  util = openpgp.util,
  hash = openpgp.crypto.hash,
  chai = require('chai'),
  expect = chai.expect;

it('SHA* with test vectors from NIST FIPS 180-2', function(done) {
	expect(util.hexstrdump(hash.sha1('abc')), 'hash.sha1("abc") = a9993e364706816aba3e25717850c26c9cd0d89d').to.equal('a9993e364706816aba3e25717850c26c9cd0d89d');
  expect(util.hexstrdump(hash.sha1('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')), 'hash.sha1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") = 84983e441c3bd26ebaae4aa1f95129e5e54670f1').to.equal('84983e441c3bd26ebaae4aa1f95129e5e54670f1');
  expect(util.hexstrdump(hash.sha224('abc')), 'hash.sha224("abc") = 23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7').to.equal('23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7');
  expect(util.hexstrdump(hash.sha224('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')), 'hash.sha224("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") = 75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525').to.equal('75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525');
  expect(util.hexstrdump(hash.sha256('abc')), 'hash.sha256("abc") = ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad').to.equal('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad');
  expect(util.hexstrdump(hash.sha256('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')), 'hash.sha256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") = 248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1').to.equal('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1');
  expect(util.hexstrdump(hash.sha384('abc')), 'hash.sha384("abc") = cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7').to.equal('cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7');
  expect(util.hexstrdump(hash.sha384('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')), 'hash.sha384("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") = 3391fdddfc8dc7393707a65b1b4709397cf8b1d162af05abfe8f450de5f36bc6b0455a8520bc4e6f5fe95b1fe3c8452b').to.equal('3391fdddfc8dc7393707a65b1b4709397cf8b1d162af05abfe8f450de5f36bc6b0455a8520bc4e6f5fe95b1fe3c8452b');
  expect(util.hexstrdump(hash.sha512('abc')), 'hash.sha512("abc") = ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f').to.equal('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f');
  expect(util.hexstrdump(hash.sha512('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')), 'hash.sha512("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") = 204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445').to.equal('204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445');
	done();
github openpgpjs / openpgpjs / test / crypto / cipher / cast5.js View on Github external
'use strict';

var openpgp = require('openpgp'),
  util = openpgp.util,
  chai = require('chai'),
  expect = chai.expect;

it('CAST-128 cipher test with test vectors from RFC2144', function (done) {
	function test_cast(input, key, output) {
		var cast5 = new openpgp.crypto.cipher.cast5(util.bin2str(key));
		var result = util.bin2str(cast5.encrypt(input));

		return util.hexstrdump(result) == util.hexstrdump(util.bin2str(output));
	}
	
	var testvectors = [[[0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9A],[0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF],[0x23,0x8B,0x4F,0xE5,0x84,0x7E,0x44,0xB2]]];

	for (var i = 0; i < testvectors.length; i++) {
    var res = test_cast(testvectors[i][1],testvectors[i][0],testvectors[i][2]);
		expect(res, 'vector with block ' + util.hexidump(testvectors[i][0]) +
github openpgpjs / openpgpjs / test / crypto / hash / md5.js View on Github external
'use strict';

var openpgp = require('openpgp'),
  util = openpgp.util,
  MD5 = openpgp.crypto.hash.md5,
  chai = require('chai'),
  expect = chai.expect;

it('MD5 with test vectors from RFC 1321', function(done) {
  expect(util.hexstrdump(MD5('')), 'MD5("") = d41d8cd98f00b204e9800998ecf8427e').to.equal('d41d8cd98f00b204e9800998ecf8427e');
	expect(util.hexstrdump(MD5('abc')), 'MD5("a") = 0cc175b9c0f1b6a831c399e269772661').to.equal('900150983cd24fb0d6963f7d28e17f72');
	expect(util.hexstrdump(MD5('message digest')), 'MD5("message digest") = f96b697d7cb7938d525a2f31aaf161d0').to.equal('f96b697d7cb7938d525a2f31aaf161d0');
	expect(util.hexstrdump(MD5('abcdefghijklmnopqrstuvwxyz')), 'MD5("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b').to.equal('c3fcd3d76192e4007dfb496cca67e13b');
	expect(util.hexstrdump(MD5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')), 'MD5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") = d174ab98d277d9f5a5611c2c9f419d9f').to.equal('d174ab98d277d9f5a5611c2c9f419d9f');
	expect(util.hexstrdump(MD5('12345678901234567890123456789012345678901234567890123456789012345678901234567890')), 'MD5("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = 57edf4a22be3c955ac49da2e2107b67a').to.equal('57edf4a22be3c955ac49da2e2107b67a');
	done();
});
github DefinitelyTyped / DefinitelyTyped / types / openpgp / openpgp-tests.ts View on Github external
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("");
openpgp.message.readArmored("");

openpgp.packet.fromStructuredClone({});
openpgp.packet.newPacketFromTag("");

openpgp.util.Uint8Array_to_str(new Uint8Array([1, 0]));
openpgp.util.decode_utf8(new Uint8Array([1, 0]));
openpgp.util.encode_utf8("");
openpgp.util.getWebCrypto();
openpgp.util.hex_to_Uint8Array("");
openpgp.util.print_debug_hexarray_dump("");
openpgp.util.print_debug_hexstr_dump("");
openpgp.util.print_debug("");
openpgp.util.print_debug_hexstr_dump("");
openpgp.util.shiftRight(new Uint8Array([1, 0]), 1);
openpgp.util.str_to_Uint8Array("");
openpgp.util.Uint8Array_to_str(openpgp.util.str_to_Uint8Array(""));
github tasn / webext-signed-pages / src / background.js View on Github external
function processPage(rawContent, signature, url, tabId) {
  const content = new Minimize({ spare:true, conditionals: true, empty: true, quotes: true }).parse(rawContent)
    .replace(/^\s*]*>/i, '');

  const shouldCheck = getPubkey(patterns, url);

  if (shouldCheck) {
    try {
      const pubkey = patterns[shouldCheck];

      const options = {
        message: openpgp.message.fromBinary(openpgp.util.str2Uint8Array(content)),
        signature: openpgp.signature.readArmored(signature),
        publicKeys: openpgp.key.readArmored(pubkey).keys,
      };

      openpgp.verify(options).then((verified) => {
        const signatureData = (verified.signatures[0].valid) ? goodSignature : badSignature;
        updateBrowserAction(signatureData, tabId);
        statusCache[url] = signatureData;
      });
    } catch (e) {
      updateBrowserAction(badSignature, tabId);
      statusCache[url] = badSignature;
    }
  } else {
    updateBrowserAction(neutralSignature, tabId);
  }