本文整理汇总了Python中pgpy.PGPKey类的典型用法代码示例。如果您正苦于以下问题:Python PGPKey类的具体用法?Python PGPKey怎么用?Python PGPKey使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PGPKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_decrypt_unsigned_message
def test_decrypt_unsigned_message():
from pgpy import PGPKey, PGPMessage
from pgpy.errors import PGPError
# these keys are small because the regression test doesn't really need the security
# if you're reading this, *DO NOT GENERATE RSA KEYS THIS SMALL*
# also, it's probably better to sign-then-encrypt rather than encrypt-then-sign
decrypt_key = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" \
"Version: PGPy v0.4.2\n" \
"\n" \
"xcA4BFlKzk4BAgDL9E6Lpzq9yNhRP49HXeOSYTz4DPI1A2wxwI97qjZFsJ2lJ2aV\n" \
"SYFpbuS6DEPaya+98HQ6xM7o2PhbUnHqcXHzABEBAAEAAf9U/XOVwpQ57e4mvWPJ\n" \
"i5h/sUGk5FAyQ0Dc4q9oCyAenaIIe5npbsR+oKmUHwJ5wWgfrTaxvAkBl15kMtSN\n" \
"VItBAQDv/8BdIdW2Bc9+qvCtC2xiUJ/3Rd+eyXMZhn4VMdA8sQEA2Y1aRBpWjHo9\n" \
"g9KydxAewt8LUwchRHeonMmILuZ58eMBALP8euss11ELnjDOLrgRP2swnOTTTk3b\n" \
"P6aV8/rbcEXOUgPNG1JlZ3Jlc3NvIEVuY3J5cHRlciAoUFIjMTgzKcJrBBMBAgAV\n" \
"BQJZSs6CAhsOAgsHAhUCAhYAAh4BAAoJEA2I8KkOVzh/+IMCAI308quFk/lJXPF/\n" \
"bpvwwgFa9bRdIzl07Qu+3oQcEm+1cu6ivznewIEmQclSUpSLjXrS/LysQSAQye+J\n" \
"PgSEalQ=\n" \
"=Sg/Y\n" \
"-----END PGP PRIVATE KEY BLOCK-----\n"
sign_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rr\n" \
"+Ud/YHq3NVk5UGU0LDfjdBwVaJmOjEUx\n" \
"=ITfp\n" \
"-----END PGP MESSAGE-----\n"
dkey, _ = PGPKey.from_blob(decrypt_key)
skey, _ = PGPKey.from_blob(sign_key)
encmsg = PGPMessage.from_blob(msg)
# this should work
decmsg = dkey.decrypt(encmsg)
assert decmsg.message == "Regression Test for PR#183"
# this should raise PGPError, not PGPDecryptionError
with pytest.raises(PGPError):
skey.decrypt(encmsg)
示例2: temp_key
def temp_key():
u = PGPUID.new('User')
k = PGPKey.new(PubKeyAlgorithm.RSAEncryptOrSign, 512)
k.add_uid(u, usage={KeyFlags.Certify, KeyFlags.Sign}, hashes=[HashAlgorithm.SHA1])
sk = PGPKey.new(PubKeyAlgorithm.RSAEncryptOrSign, 512)
k.add_subkey(sk, usage={KeyFlags.EncryptCommunications})
return k
示例3: test_save
def test_save(self, kf):
# load the key and export it back to binary
key, _ = PGPKey.from_file(kf)
pgpyblob = key.__bytes__()
# try loading the exported key
reloaded, _ = PGPKey.from_file(kf)
assert pgpyblob == reloaded.__bytes__()
示例4: test_parse_wrong_crc24
def test_parse_wrong_crc24(self, recwarn):
keytext = _read('tests/testdata/keys/rsa.1.sec.asc').splitlines()
keytext[-2] = "=abcd"
keytext = '\n'.join(keytext)
key = PGPKey()
key.parse(keytext)
w = recwarn.pop(UserWarning)
assert str(w.message) == "Incorrect crc24"
assert w.filename == __file__
示例5: test_new_key_deprecated_rsa_alg
def test_new_key_deprecated_rsa_alg(self, key_alg_rsa_depr, recwarn):
k = PGPKey.new(key_alg_rsa_depr, 512)
w = recwarn.pop()
assert str(w.message) == '{:s} is deprecated - generating key using RSAEncryptOrSign'.format(key_alg_rsa_depr.name)
# assert w.filename == __file__
assert k.key_algorithm == PubKeyAlgorithm.RSAEncryptOrSign
示例6: test_add_subkey
def test_add_subkey(self, pkspec, skspec):
if pkspec not in self.keys:
pytest.skip('Keyspec {} not in keys; must not have generated'.format(pkspec))
alg, size = skspec
if not alg.can_gen:
pytest.xfail('Key algorithm {} not yet supported'.format(alg.name))
if isinstance(size, EllipticCurveOID) and ((not size.can_gen) or size.name not in _openssl_get_supported_curves()):
pytest.xfail('Curve {} not yet supported'.format(size.name))
key = self.keys[pkspec]
subkey = PGPKey.new(*skspec)
# before adding subkey to key, the key packet should be a PrivKeyV4, not a PrivSubKeyV4
assert isinstance(subkey._key, PrivKeyV4)
assert not isinstance(subkey._key, PrivSubKeyV4)
key.add_subkey(subkey, usage={KeyFlags.EncryptCommunications})
# now that we've added it, it should be a PrivSubKeyV4
assert isinstance(subkey._key, PrivSubKeyV4)
# self-verify
assert key.verify(subkey)
sv = key.verify(key)
assert sv
assert subkey in sv
# try to verify with GPG
self.gpg_verify_key(key)
示例7: test_pubkey_subkey_parent
def test_pubkey_subkey_parent():
from pgpy import PGPKey
# import this small key that has a subkey
keyblob = ('-----BEGIN PGP PRIVATE KEY BLOCK-----\n'
'Version: PGPy v0.4.2\n'
'\n'
'xcA4BFlULU4BAgDeq2bKPPOBzdgd1WF3RBQ0E0kkZbTfpgZjamDzdb6gfQ5TcBhs\n'
'drI4XpxWOV3DorbsZ8Usj4zHx/XmLNCmxwqvABEBAAEAAgCSO76l0qGY/baQ4THB\n'
'QdSC3qeKX8EJn99SKurA+PLYMg6IxLGBpWYIK8tT68xpqQ5ZwE9GodZ2QjfOVz2R\n'
'o4IBAQD/UjtthEtyiMA1CDCPEksfIyd0QDjt82C19MSeqau8WQEA30LydxkjlvgH\n'
'u5/uWVGqoFWhhfw5hDrYy72L6EbCfkcA/2csk7uGw/yg2MDUTlDwdokn1DLGkt/+\n'
'Q/fPAMYvX6gvVoXNFVJlZ3Jlc3NvIChJc3N1ZSAjMTk0KcJrBBMBAgAVBQJZVC3O\n'
'AhsDAgsHAhUCAhYAAh4BAAoJEC4sMTkKIj+F8ywB/AqaNHwi8xM1Rg99mOSib1zi\n'
'jlXALY8pOrNU7Nqtc/6oks+49WeVW5zpE1vl1JPm2WYzvCEnE1KffdyjNR0bQ1XH\n'
'wDgEWVQtUQECAKsWCdSRh6YDP9yuSonfHpBfUzRD/EQvpNnUDiTclV9w6RPMZYk9\n'
'o5oUQTumPKnznsovLpNmIm48DCALMzdTzH0AEQEAAQACAJDfsKNYOM3Toph03pmx\n'
'XmhS0FpJ16zFy4rJjtCYGcUerUqRQ1ehXIY9Ig9J5LitJXThrP4dvUlRCWUcxxl6\n'
'9eEBANOiM8ktXW0bPZfBKunWn7ajA0PMBKG8p2d9iBCawBbbAQDO88L8V0cxCRvH\n'
'8L1J4gsttPWDOnhw5z8Dq4Zv5U3thwD/WwE0miqfEpYAmkhc0g7lHf6l7qo+SrUZ\n'
'ZKl0GLPLKKFRscK9BBgBAgAJBQJZVC3mAhsMAGgJEC4sMTkKIj+FXSAEGQECAAYF\n'
'AllULeYACgkQCK0qxtsEtqzY7QIAoayZGB78eaImQVOpTLX2jnaDR2UY7NtUy6YI\n'
'XMSumCeZj+n+BexmUm6x2kqg0FJLRwAE4i+rnvFA0HHX40/9d221AgCzUxHuHjKP\n'
'b5wNW20vanc6b6ZMi52MyhluXAIdnvgPkPEzVIS+gGOX2DeT4TXAdosKfD1o5qS7\n'
'ANRbocmpDuO3\n'
'=UjzO\n'
'-----END PGP PRIVATE KEY BLOCK-----\n')
privkey, _ = PGPKey.from_blob(keyblob)
pubkey = privkey.pubkey
assert pubkey.subkeys['08AD2AC6DB04B6AC'].parent is pubkey
示例8: test_load_from_bytearray
def test_load_from_bytearray(self, kf):
tkb = bytearray(os.stat(kf).st_size)
with open(kf, 'rb') as tkf:
tkf.readinto(tkb)
key, _ = PGPKey.from_blob(tkb)
assert key.fingerprint == _fingerprints[os.path.basename(kf)]
示例9: test_load_from_bytearray
def test_load_from_bytearray(self, gpg_keyid_file):
tkb = bytearray(os.stat(self.kf).st_size)
with open(self.kf, 'rb') as tkf:
tkf.readinto(tkb)
key, _ = PGPKey.from_blob(tkb)
assert key.fingerprint.keyid in gpg_keyid_file(self.kf.replace('tests/testdata/', ''))
示例10: test_new_key
def test_new_key(self, key_alg):
# create a key and a user id and add the UID to the key
uid = PGPUID.new('Hugo Gernsback', 'Science Fiction Plus', '[email protected]')
key = PGPKey.new(key_alg, key_alg_size[key_alg])
key.add_uid(uid, hashes=[HashAlgorithm.SHA224])
# self-verify the key
assert key.verify(key)
self.gen_keys[key_alg] = key
示例11: test_verify_invalid_sig
def test_verify_invalid_sig(self, string, key_alg):
# generate a keypair
u = PGPUID.new('asdf')
k = PGPKey.new(key_alg, key_alg_size[key_alg])
k.add_uid(u, usage={KeyFlags.Certify, KeyFlags.Sign}, hashes=[HashAlgorithm.SHA1])
# sign string with extra characters (this means k.pubkey.verify(string) will return false
sig = k.sign(string + 'asdf')
assert not k.pubkey.verify(string, sig)
示例12: abe
def abe():
uid = PGPUID.new('Abraham Lincoln', comment='Honest Abe', email='[email protected]')
with open('tests/testdata/abe.jpg', 'rb') as abef:
abebytes = bytearray(os.fstat(abef.fileno()).st_size)
abef.readinto(abebytes)
uphoto = PGPUID.new(abebytes)
# Abe is pretty oldschool, so he uses a DSA primary key
# normally he uses an ElGamal subkey for encryption, but PGPy doesn't support that yet, so he's settled for RSA for now
key = PGPKey.new(PubKeyAlgorithm.DSA, 1024)
subkey = PGPKey.new(PubKeyAlgorithm.RSAEncryptOrSign, 1024)
key.add_uid(uid,
usage={KeyFlags.Certify, KeyFlags.Sign},
hashes=[HashAlgorithm.SHA224, HashAlgorithm.SHA1],
ciphers=[SymmetricKeyAlgorithm.AES128, SymmetricKeyAlgorithm.Camellia128, SymmetricKeyAlgorithm.CAST5],
compression=[CompressionAlgorithm.ZLIB])
key.add_uid(uphoto)
key.add_subkey(subkey, usage={KeyFlags.EncryptCommunications, KeyFlags.EncryptStorage})
return key
示例13: test_verify_invalid_sig
def test_verify_invalid_sig(self, pkspec, string):
# test verifying an invalid signature
u = PGPUID.new('asdf')
k = PGPKey.new(*pkspec)
k.add_uid(u, usage={KeyFlags.Certify, KeyFlags.Sign}, hashes=[HashAlgorithm.SHA1])
# sign the string with extra characters, so that verifying just string fails
sig = k.sign(string + 'asdf')
sv = k.pubkey.verify(string, sig)
assert not sv
assert sig in sv
示例14: test_sign_encrypted_message
def test_sign_encrypted_message(self, sf, cipher):
# test decrypting a message
sec, _ = PGPKey.from_file(sf)
if (sec.fingerprint, cipher) not in self.msgs:
pytest.skip('Message not present; see test_encrypt_message skip or xfail reason')
emsg = self.msgs[(sec.fingerprint, cipher)]
emsg |= sec.sign(emsg)
assert emsg.is_signed
assert emsg.is_encrypted
assert isinstance(next(iter(emsg)), PGPSignature)
示例15: test_encrypt_message_select_uid
def test_encrypt_message_select_uid(self):
# generate a temporary key with two UIDs, then encrypt a message
u1 = PGPUID.new('UID One')
u2 = PGPUID.new('UID Two')
k = PGPKey.new(PubKeyAlgorithm.RSAEncryptOrSign, 512)
flags = {KeyFlags.Certify, KeyFlags.Sign, KeyFlags.EncryptCommunications, KeyFlags.EncryptStorage}
k.add_uid(u1, usage=flags, hashes=[HashAlgorithm.SHA1], ciphers=[SymmetricKeyAlgorithm.AES128])
k.add_uid(u2, usage=flags, hashes=[HashAlgorithm.SHA1], ciphers=[SymmetricKeyAlgorithm.Camellia128])
emsg = k.pubkey.encrypt(PGPMessage.new('This message is about to be encrypted'), user='UID Two')
# assert that it was encrypted with Camellia128 and that we can decrypt it normally
assert emsg._sessionkeys[0].decrypt_sk(k._key)[0] == SymmetricKeyAlgorithm.Camellia128
assert k.decrypt(emsg).message == 'This message is about to be encrypted'