def test_from_armor_reader(): ctx = Context(network_policy=NetworkPolicy.Offline, ephemeral=True) k = open(asc, "rb").read() r = Reader.from_bytes(ctx, k) r = ArmorReader.new(ctx, r) t = TPK.from_reader(ctx, r) assert t.fingerprint() == fp
def test_clone(): ctx = Context(network_policy=NetworkPolicy.Offline, ephemeral=True) a = TPK.open(ctx, pgp) b = a.copy() del a c = b.copy() del b assert c.fingerprint() == fp
def test_from_serialize(): ctx = Context(network_policy=NetworkPolicy.Offline, ephemeral=True) with TemporaryDirectory() as tmp: sink = join(tmp, "a") t = TPK.open(ctx, pgp) with Writer.open(ctx, sink) as s: t.serialize(s) t = TPK.open(ctx, sink) assert t.fingerprint() == fp
def test_decryption(): ctx = Context(network_policy=NetworkPolicy.Offline, ephemeral=True) class State(Enum): # XXX: In Python 3.6, we can use enum.auto() to assign values. # But we want to support Debian 9, which uses Python 3.5, as # long as it is Debian stable. Start = 1 Decrypted = 2 Deciphered = 3 Done = 4 state = State.Start algo, key = None, None ppr = PacketParser.open(ctx, pgp) while True: pp = ppr.packet_parser() if not pp: break packet = pp.packet tag = packet.tag print(state, pp.recursion_depth, packet) if state == State.Start: assert pp.recursion_depth == 0 if tag == Tag.SKESK: algo, key = packet.match().decrypt(b"123456789") state = State.Decrypted elif state == State.Decrypted: assert pp.recursion_depth == 0 if tag == Tag.SEIP: pp.decrypt(algo, key) state = State.Deciphered elif state == State.Deciphered: assert pp.recursion_depth == 1 if tag == Tag.Literal: body = pp.buffer_unread_content() assert body[:].decode() == open(plain).read() state = State.Done _, ppr = pp.recurse() assert ppr.eof().is_message() assert state == State.Done
import sys import os from getpass import getpass from enum import Enum, auto from sequoia.core import Context, NetworkPolicy from sequoia.openpgp import Tag, PacketParser ctx = Context(network_policy=NetworkPolicy.Offline, ephemeral=True) class State(Enum): Start = auto() Decrypted = auto() Deciphered = auto() Done = auto() state = State.Start algo, key = None, None pp = PacketParser.open(ctx, sys.argv[1]) while pp.has_packet: packet = pp.packet tag = packet.tag if state == State.Start: if tag == Tag.SKESK: passphrase = getpass("Enter passphrase to decrypt message: ") algo, key = packet.match().decrypt(passphrase.encode()) state = State.Decrypted elif tag == Tag.PKESK: sys.stderr.write("Decryption using PKESK not yet supported.\n")
def test_equals(): ctx = Context(network_policy=NetworkPolicy.Offline, ephemeral=True) b = open(pgp, "rb").read() t = TPK.from_bytes(ctx, b) u = TPK.from_bytes(ctx, b) assert t == u
def test_from_bytes(): ctx = Context(network_policy=NetworkPolicy.Offline, ephemeral=True) t = TPK.from_bytes(ctx, open(pgp, "rb").read()) assert t.fingerprint() == fp
def test_from_packet_pile(): ctx = Context(network_policy=NetworkPolicy.Offline, ephemeral=True) r = PacketPile.open(ctx, pgp) t = TPK.from_packet_pile(ctx, r) assert t.fingerprint() == fp
def test_from_file(): ctx = Context(network_policy=NetworkPolicy.Offline, ephemeral=True) t = TPK.open(ctx, pgp) assert t.fingerprint() == fp
def test_from_reader(): ctx = Context(network_policy=NetworkPolicy.Offline, ephemeral=True) r = Reader.open(ctx, pgp) t = TPK.from_reader(ctx, r) assert t.fingerprint() == fp