Пример #1
0
    def test_splitting(self) -> None:
        test_string = b'cypherpunk'

        self.assertEqual(
            split_to_substrings(test_string, length=5),
            [b'cyphe', b'ypher', b'pherp', b'herpu', b'erpun', b'rpunk'])

        self.assertEqual(split_to_substrings(test_string, length=7),
                         [b'cypherp', b'ypherpu', b'pherpun', b'herpunk'])

        self.assertEqual(
            split_to_substrings(test_string, length=len(test_string)),
            [b'cypherpunk'])

        self.assertEqual(
            split_to_substrings(test_string, length=len(test_string) + 1), [])
Пример #2
0
def exchange_public_keys(
    onion_pub_key: bytes,
    tfc_public_key_user: bytes,
    kdk_hash: bytes,
    contact: 'Contact',
    settings: 'Settings',
    queues: 'QueueDict',
) -> bytes:
    """Exchange public keys with contact.

    This function outputs the user's public key and waits for user to
    enter the public key of the contact. If the User presses <Enter>,
    the function will resend the users' public key to contact.
    """
    public_key_packet = PUBLIC_KEY_DATAGRAM_HEADER + onion_pub_key + tfc_public_key_user
    queue_to_nc(public_key_packet, queues[RELAY_PACKET_QUEUE])

    while True:
        try:
            tfc_public_key_contact = get_b58_key(B58_PUBLIC_KEY, settings,
                                                 contact.short_address)
        except ValueError as invalid_pub_key:
            invalid_key = str(invalid_pub_key).encode()

            # Do not send packet to Relay Program if the user has for some reason
            # managed to embed the local key decryption key inside the public key.
            substrings = split_to_substrings(invalid_key,
                                             ENCODED_B58_KDK_LENGTH)
            safe_string = not any(
                blake2b(substring) == kdk_hash for substring in substrings)

            if safe_string:
                public_key_packet = (UNENCRYPTED_DATAGRAM_HEADER +
                                     UNENCRYPTED_PUBKEY_CHECK + onion_pub_key +
                                     invalid_key)
                queue_to_nc(public_key_packet, queues[RELAY_PACKET_QUEUE])
            continue

        if tfc_public_key_contact == b'':
            public_key_packet = PUBLIC_KEY_DATAGRAM_HEADER + onion_pub_key + tfc_public_key_user
            queue_to_nc(public_key_packet, queues[RELAY_PACKET_QUEUE])
            continue

        return tfc_public_key_contact