Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# The test doesn't work so well, because comments
# are not rendered :-/
# assert_in(uid.uid, [e[0] for e in uid_encrypted])
# Decrypt...
from monkeysign.gpg import Keyring
# We sent back the key to the key-sending side
kr = Keyring(homedir=self.sender_homedir)
log.info("encrypted UID: %r", enc_uid)
decrypted = kr.decrypt_data(signed_uid)
log.info("ctx out: %r", kr.context.stdout)
log.info("ctx err: %r", kr.context.stderr)
assert_true (decrypted, "Error decrypting %r" % signed_uid)
# Now we have the signed UID. We want see if it really carries a signature.
pgpykeys = pgpy.PGPKey.from_blob(decrypted)
log.info("Loaded Signed Keys: %r", pgpykeys)
k = pgpykeys[0]
# assert_equal(uid_from_signing, k.userids[0])
assert_equal(len(k.userids), 1)
uid = k.userids[0]
uidstr = u"{}".format(uid)
assert_in(uidstr, signatures_before)
# Now we have the signed UID. We want see if it really carries a signature.
signatures_after[uidstr] = uid._signatures
assert_less(len(signatures_before[uidstr]), len(signatures_after[uidstr]))
lFgEXEcE6RYJKwYBBAHaRw8BAQdArjWwk3FAqyiFbFBKT4TzXcVBqPTB3gmzlC/U
b7O1u10AAP9XBeW6lzGOLx7zHH9AsUDUTb2pggYGMzd0P3ulJ2AfvQ4RtCZBbGlj
ZSBMb3ZlbGFjZSA8YWxpY2VAb3BlbnBncC5leGFtcGxlPoiQBBMWCAA4AhsDBQsJ
CAcCBhUKCQgLAgQWAgMBAh4BAheAFiEE64W7X6M6deFelE5j8jFVDE9H444FAl2l
nzoACgkQ8jFVDE9H447pKwD6A5xwUqIDprBzrHfahrImaYEZzncqb25vkLV2arYf
a78A/R3AwtLQvjxwLDuzk4dUtUwvUYibL2sAHwj2kGaHnfICnF0EXEcE6RIKKwYB
BAGXVQEFAQEHQEL/BiGtq0k84Km1wqQw2DIikVYrQrMttN8d7BPfnr4iAwEIBwAA
/3/xFPG6U17rhTuq+07gmEvaFYKfxRB6sgAYiW6TMTpQEK6IeAQYFggAIBYhBOuF
u1+jOnXhXpROY/IxVQxPR+OOBQJcRwTpAhsMAAoJEPIxVQxPR+OOWdABAMUdSzpM
hzGs1O0RkWNQWbUzQ8nUOeD9wNbjE3zR+yfRAQDbYqvtWQKN4AQLTxVJN5X5AWyb
Pnn+We1aTBhaGa86AQ==
=n8OM
-----END PGP PRIVATE KEY BLOCK-----
'''
alice_key, _ = PGPKey.from_blob(alice_sec)
class FixedOffset(datetime.tzinfo):
def __init__(self, hours, name):
self.__offset = datetime.timedelta(hours=hours)
self.__name = name
def utcoffset(self, dt):
return self.__offset
def tzname(self, dt):
return self.__name
def dst(self, dt):
return datetime.timedelta(0)
# America/New_York during DST:
tz = FixedOffset(-4, 'EDT')
# 2019-10-20T09:18:11-0400
when = datetime.datetime.fromtimestamp(1571577491, tz)
def test_load_from_bytes(self, kf):
with open(kf, 'rb') as tkf:
key, _ = PGPKey.from_blob(tkf.read())
assert key.fingerprint == _fingerprints[os.path.basename(kf)]
'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
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)]
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 _verify_keybase_signature(raw_pgp_sig_msg):
# load the raw pgp message
pgp_msg = PGPMessage.from_blob(raw_pgp_sig_msg)
# load keybase's claimed public key
# see: https://keybase.io/docs/server_security/our_merkle_key
kb_public_key, _ = PGPKey.from_blob(KEYBASE_PGP_VERIFICATION_KEY)
# verify it: https://pgpy.readthedocs.io/en/latest/examples.html#verifying-things
f = io.StringIO()
with redirect_stderr(f):
# suppress unnecessary stdout
verification_result = kb_public_key.verify(pgp_msg)
if not verification_result:
raise VerificationError("API response did not verify with Keybase's public key")
good_signatures = list(verification_result.good_signatures)
if len(good_signatures) != 1:
logger.error(f"good_signatures = {good_signatures}")
raise VerificationError(f"Expected 1 valid signature, got {len(good_signatures)} from {specific_url}")
return json.loads(good_signatures[0].subject)