Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_encrypt_unsupported_algorithm(self):
lit = PGPMessage.new('tests/testdata/lit')
with pytest.raises(PGPEncryptionError):
lit.encrypt("QwertyUiop", cipher=SymmetricKeyAlgorithm.Twofish256)
def sign_encrypt(object):
message = pgpy.PGPMessage.new(json.dumps([object]))
message |= exchange_key.sign(message)
# explicit encode for Python 3 compatibility
return json.dumps({'type': 'data', 'data': str(trader_key.encrypt(message))}).encode('utf8')
def sign(object):
message = pgpy.PGPMessage.new(json.dumps(object))
message |= exchange_key.sign(message)
# explicit encode for Python 3 compatibility
return json.dumps({'type': 'data', 'data': str(message)}).encode('utf8')
def test_encrypt_passphrase_2(self):
mtxt = "This message is to be encrypted"
msg = PGPMessage.new(mtxt)
assert not msg.is_encrypted
sk = SymmetricKeyAlgorithm.AES256.gen_key()
encmsg = msg.encrypt("QwertyUiop", sessionkey=sk).encrypt("AsdfGhjkl", sessionkey=sk)
assert isinstance(encmsg, PGPMessage)
assert encmsg.is_encrypted
assert encmsg.type == 'encrypted'
# decrypt with PGPy only, since GnuPG can't do multiple passphrases
for passwd in ["QwertyUiop", "AsdfGhjkl"]:
decmsg = encmsg.decrypt(passwd)
assert isinstance(decmsg, PGPMessage)
assert decmsg.type == msg.type
assert decmsg.is_compressed
def test_encrypt_message(self, pub, cipher):
if pub.key_algorithm in {PubKeyAlgorithm.DSA}:
pytest.skip('Asymmetric encryption only implemented for RSA/ECDH currently')
if cipher in {SymmetricKeyAlgorithm.Plaintext, SymmetricKeyAlgorithm.Twofish256, SymmetricKeyAlgorithm.IDEA}:
pytest.xfail('Symmetric cipher {} not supported for encryption'.format(cipher))
# test encrypting a message
mtxt = "This message will have been encrypted"
msg = PGPMessage.new(mtxt)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
emsg = pub.encrypt(msg, cipher=cipher)
self.msgs[(pub.fingerprint, cipher)] = emsg
'ohawApu63JzJNXoRpjeBhox073iEjeSbvq/pJ/+y0t6KkFZXoXgpqACGDNjPpojk\n'
'6YTo6Da7GpyXefhKyH4IQ9Lbd9UIEhOKfktXTJfR/EoCZb+rmTm0WnjwkwOAVdQv\n'
'vuMRm8Hc39xn+Mt0CV+0KcYHhNfK+A2XU6bZkHuwaTzxTaotmeLeCgC+BA2Phwxp\n'
'BIhkSNE8ayYLN0VBPraw28xONzNV5e6f8RWNoGTDQxhZflmvIGz/XO5wo1DV1G0k\n'
'VIR49brAmrapqpCW7XWhuuupVbrpbjRUa+c4G03tySXQXUTdA3etH0EAEQEAAYkB\n'
'NgQYAQgAIBYhBC+wZs+/pkuENyw0jGyG3k7bRXOMBQJbSm0kAhsMAAoJEGyG3k7b\n'
'RXOMKbMIAKymk6Fe1qpjXtK56jpMurz1wBL0/twQbvtKQlgMBNdro0MX30xKGXh1\n'
'rCEIV3ls7CJnUm2NEeqFPzFZhsZS2FkgDXXT20K3S6nscv8xlF2z+jktK2RY6oCJ\n'
'Lgw447Rgjw5ARgW2XNrGRzapAf4KBgcyO1KtTCbjh8leg4Fs1O7B8EbiBvoeUJR0\n'
'wj2xNG4cOHoWN7Zjv8lLsJn60+ZbTeU25ghybmt7WjCs4ht7TZmamerLPzrFvP2c\n'
'ftLsb17HhrBPdfs42SsD8A816JDM7PcJWujlDV9FPJgoVjndK+4Jfpg9b4jOBA7J\n'
'7zeGuobtKdS9Y97BVFNtTPZK66YUIEQ=\n'
'=lGIy\n'
'-----END PGP PUBLIC KEY BLOCK-----\n')
pubkey, _ = PGPKey.from_blob(keyblob)
msg = PGPMessage.new('asdf')
with warnings.catch_warnings():
warnings.simplefilter('ignore')
pubkey.encrypt(msg)
def test_new(self, comp_alg, sensitive):
mtxt = u"This is a new message!"
msg = PGPMessage.new(mtxt, compression=comp_alg, sensitive=sensitive)
assert isinstance(msg, PGPMessage)
assert msg.filename == ('_CONSOLE' if sensitive else '')
assert msg.is_sensitive is sensitive
assert msg.type == 'literal'
assert msg.message == mtxt
assert msg._compression == comp_alg
if gpg:
# see if GPG can parse our message
assert self.gpg_message(msg).decode('utf-8') == mtxt
def encrypt(message: str, key: bytes) -> str:
import pgpy
from pgpy import PGPMessage
from pgpy.constants import CompressionAlgorithm
message = PGPMessage.new(message, compression=CompressionAlgorithm.Uncompressed)
pub_key, _ = pgpy.PGPKey.from_blob(key)
return str(pub_key.encrypt(message))
def _encrypt_msg_with_pkey(self, data, key):
clear_msg = PGPMessage.new(data)
pkey = key if key.is_public else key.pubkey
enc_msg = pkey.encrypt(clear_msg)
return enc_msg