Example #1
0
def prepare_pgp_message(orig_msg: Message, pgp_fingerprint: str):
    msg = MIMEMultipart("encrypted", protocol="application/pgp-encrypted")

    # copy all headers from original message except all standard MIME headers
    for i in reversed(range(len(orig_msg._headers))):
        header_name = orig_msg._headers[i][0].lower()
        if header_name.lower() not in _MIME_HEADERS:
            msg[header_name] = orig_msg._headers[i][1]

    # Delete unnecessary headers in orig_msg except to save space
    delete_all_headers_except(
        orig_msg,
        _MIME_HEADERS,
    )

    first = MIMEApplication(_subtype="pgp-encrypted",
                            _encoder=encoders.encode_7or8bit,
                            _data="")
    first.set_payload("Version: 1")
    msg.attach(first)

    second = MIMEApplication("octet-stream", _encoder=encoders.encode_7or8bit)
    second.add_header("Content-Disposition", "inline")
    # encrypt original message
    encrypted_data = pgp_utils.encrypt_file(BytesIO(orig_msg.as_bytes()),
                                            pgp_fingerprint)
    second.set_payload(encrypted_data)
    msg.attach(second)

    return msg
Example #2
0
def test_encrypt():
    fingerprint = load_public_key(pubkey)
    secret = encrypt_file(BytesIO(b"abcd"), fingerprint)
    assert secret != ""
def test_encrypt():
    public_key_path = os.path.join(ROOT_DIR, "local_data/public-pgp.asc")
    public_key = open(public_key_path).read()
    fingerprint = load_public_key(public_key)
    secret = encrypt_file(BytesIO(b"abcd"), fingerprint)
    assert secret != ""