How to use the pgpy.constants.HashAlgorithm function in PGPy

To help you get started, we’ve selected a few PGPy 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 SecurityInnovation / PGPy / tests / test_05_actions.py View on Github external
def test_add_altuid(self, pkspec):
        if pkspec not in self.keys:
            pytest.skip('Keyspec {} not in keys; must not have generated'.format(pkspec))

        key = self.keys[pkspec]
        uid = PGPUID.new('T. Keyerson', 'Secondary UID', 'testkey@localhost.local')

        expiration = datetime.utcnow() + timedelta(days=2)

        # add all of the sbpackets that only work on self-certifications
        with warnings.catch_warnings():
            warnings.simplefilter('ignore')
            key.add_uid(uid,
                        usage=[KeyFlags.Certify, KeyFlags.Sign],
                        ciphers=[SymmetricKeyAlgorithm.AES256, SymmetricKeyAlgorithm.Camellia256],
                        hashes=[HashAlgorithm.SHA384],
                        compression=[CompressionAlgorithm.ZLIB],
                        key_expiration=expiration,
                        keyserver_flags={KeyServerPreferences.NoModify},
                        keyserver='about:none',
                        primary=False)

        sig = uid.selfsig

        assert sig.type == SignatureType.Positive_Cert
        assert sig.cipherprefs == [SymmetricKeyAlgorithm.AES256, SymmetricKeyAlgorithm.Camellia256]
        assert sig.hashprefs == [HashAlgorithm.SHA384]
        assert sig.compprefs == [CompressionAlgorithm.ZLIB]
        assert sig.features == {Features.ModificationDetection}
        assert sig.key_expiration == expiration - key.created
        assert sig.keyserver == 'about:none'
        assert sig.keyserverprefs == {KeyServerPreferences.NoModify}
github SecurityInnovation / PGPy / tests / test_01_packetfields.py View on Github external
def test_iterated_string2key(self, is2k):
        b = is2k[:]
        s = String2Key()
        s.parse(is2k)

        assert len(is2k) == 0
        assert len(s) == len(b)
        assert s.__bytes__() == b

        assert bool(s)
        assert s.halg in HashAlgorithm
        assert s.encalg in SymmetricKeyAlgorithm
        assert s.specifier == String2KeyType.Iterated
        assert s.salt == _salt
        assert s.count == 2048
        assert s.iv == _iv
github SecurityInnovation / PGPy / tests / test_10_exceptions.py View on Github external
def test_sign_bad_prefs(self, rsa_sec, recwarn):
        rsa_sec.subkeys['2A834D8E5918E886'].sign(PGPMessage.new('asdf'), hash=HashAlgorithm.RIPEMD160)

        w = recwarn.pop(UserWarning)
        assert str(w.message) == "Selected hash algorithm not in key preferences"
        assert w.filename == __file__
github SecurityInnovation / PGPy / tests / test_05_actions.py View on Github external
def test_gen_key(self, alg, size):
        # create a primary key with a UID
        uid = PGPUID.new('Test Key', '{}.{}'.format(alg.name, size), 'user@localhost.local')
        key = PGPKey.new(alg, size)

        if alg is PubKeyAlgorithm.ECDSA:
            # ECDSA keys require larger hash digests
            key.add_uid(uid, hashes=[HashAlgorithm.SHA384])

        else:
            key.add_uid(uid, hashes=[HashAlgorithm.SHA224])

        assert uid in key

        # self-verify the key
        assert key.verify(key)
        self.keys[(alg, size)] = key

        if gpg:
            # try to verify with GPG
            self.gpg_verify_key(key)
github leapcode / soledad / tests / e2e / utils.py View on Github external
def gen_key(username):
    print("generating OpenPGP key pair")
    key = pgpy.PGPKey.new(PubKeyAlgorithm.RSAEncryptOrSign, 4096)
    uid = pgpy.PGPUID.new(username, email='%s@%s' % (username, _provider))
    key.add_uid(
        uid,
        usage={KeyFlags.EncryptCommunications},
        hashes=[HashAlgorithm.SHA512],
        ciphers=[SymmetricKeyAlgorithm.AES256],
        compression=[CompressionAlgorithm.Uncompressed]
    )
    return key
github SecurityInnovation / PGPy / tests / test_03_armor.py View on Github external
[('__sig__',       b'\x70\x38\x79\xd0\x58\x70\x58\x7b\x50\xe6\xab\x8f\x9d\xc3\x46\x2c\x5a\x6b\x98\x96\xcf'
                   b'\x3b\xa3\x79\x13\x08\x6d\x90\x9d\x67\xd2\x48\x7d\xd7\x1a\xa5\x98\xa7\x8f\xca\xe3\x24'
                   b'\xd4\x19\xab\xe5\x45\xc5\xff\x21\x0c\x72\x88\x91\xe6\x67\xd7\xe5\x00\xb3\xf5\x55\x0b'
                   b'\xd0\xaf\x77\xb3\x7e\xa4\x79\x59\x06\xa2\x05\x44\x9d\xd2\xa9\xcf\xb1\xf8\x03\xc1\x90'
                   b'\x81\x87\x36\x1a\xa6\x5c\x79\x98\xfe\xdb\xdd\x23\x54\x69\x92\x2f\x0b\xc4\xee\x2a\x61'
                   b'\x77\x35\x59\x6e\xb2\xe2\x1b\x80\x61\xaf\x2d\x7a\x64\x38\xfe\xe3\x95\xcc\xe8\xa4\x05'
                   b'\x55\x5d'),
         ('cipherprefs',    []),
         ('compprefs',      []),
         ('created',        datetime.utcfromtimestamp(1402615373)),
         ('embedded',       False),
         ('exportable',     True),
         ('features',       set()),
         ('hash2',          b'\xc4\x24'),
         ('hashprefs',      []),
         ('hash_algorithm', HashAlgorithm.SHA512),
         ('is_expired',     False),
         ('key_algorithm',  PubKeyAlgorithm.RSAEncryptOrSign),
         ('key_flags',      set()),
         ('keyserver',      ''),
         ('keyserverprefs', []),
         ('magic',          "SIGNATURE"),
         ('notation',       {}),
         ('policy_uri',     ''),
         ('revocable',      True),
         ('revocation_key', None),
         ('signer',         'FCAE54F74BA27CF7'),
         ('type',           SignatureType.BinaryDocument)],

    'tests/testdata/blocks/eccpubkey.asc':
        [('created',       datetime(2010, 9, 17, 20, 33, 49)),
         ('expires_at',    None),
github SecurityInnovation / PGPy / pgpy / constants.py View on Github external
def kdf_halg(self):
        # return the hash algorithm to specify in the KDF fields when generating a key
        algs = {256: HashAlgorithm.SHA256,
                384: HashAlgorithm.SHA384,
                512: HashAlgorithm.SHA512,
                521: HashAlgorithm.SHA512}

        return algs.get(self.key_size, None)
github SecurityInnovation / PGPy / pgpy / constants.py View on Github external
def kdf_halg(self):
        # return the hash algorithm to specify in the KDF fields when generating a key
        algs = {256: HashAlgorithm.SHA256,
                384: HashAlgorithm.SHA384,
                512: HashAlgorithm.SHA512,
                521: HashAlgorithm.SHA512}

        return algs.get(self.key_size, None)
github hpk42 / muacrypt / core / autocrypt / pgpycrypto.py View on Github external
import sys

import six
from pgpy import PGPUID, PGPKey, PGPMessage, PGPKeyring, PGPSignature
from pgpy.constants import (CompressionAlgorithm, HashAlgorithm, KeyFlags,
                            PubKeyAlgorithm, SymmetricKeyAlgorithm)

# TODO: these two functions should be in a separate file
from .bingpg import KeyInfo, b64encode_u


# NOTE: key size was decided to be 2048
KEY_SIZE = 2048
# TODO: see which defaults we would like here
SKEY_ARGS = {
    'hashes': [HashAlgorithm.SHA512, HashAlgorithm.SHA256],
    'ciphers': [SymmetricKeyAlgorithm.AES256,
                SymmetricKeyAlgorithm.AES192,
                SymmetricKeyAlgorithm.AES128],
    'compression': [CompressionAlgorithm.ZLIB,
                    CompressionAlgorithm.BZ2,
                    CompressionAlgorithm.ZIP,
                    CompressionAlgorithm.Uncompressed]
}
# RSAEncrypt is deprecated, therefore using RSAEncryptOrSign
# also for the subkey
SKEY_ALG = PubKeyAlgorithm.RSAEncryptOrSign
SKEY_USAGE_SIGN = {KeyFlags.Sign}
SKEY_USAGE_ENC = {KeyFlags.EncryptCommunications, KeyFlags.EncryptStorage}
SKEY_USAGE_ALL = {KeyFlags.Sign, KeyFlags.EncryptCommunications,
                  KeyFlags.EncryptStorage}