Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add type hints to the crypto module. (#8999)
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep authored Jan 4, 2021
1 parent a685bbb commit 1c9a850
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 113 deletions.
1 change: 1 addition & 0 deletions changelog.d/8999.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type hints to the crypto module.
2 changes: 2 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ files =
synapse/api,
synapse/appservice,
synapse/config,
synapse/crypto,
synapse/event_auth.py,
synapse/events/builder.py,
synapse/events/validator.py,
Expand Down Expand Up @@ -75,6 +76,7 @@ files =
synapse/storage/background_updates.py,
synapse/storage/databases/main/appservice.py,
synapse/storage/databases/main/events.py,
synapse/storage/databases/main/keys.py,
synapse/storage/databases/main/pusher.py,
synapse/storage/databases/main/registration.py,
synapse/storage/databases/main/stream.py,
Expand Down
2 changes: 1 addition & 1 deletion synapse/crypto/context_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class ConnectionVerifier:

# This code is based on twisted.internet.ssl.ClientTLSOptions.

def __init__(self, hostname: bytes, verify_certs):
def __init__(self, hostname: bytes, verify_certs: bool):
self._verify_certs = verify_certs

_decoded = hostname.decode("ascii")
Expand Down
29 changes: 18 additions & 11 deletions synapse/crypto/event_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import collections.abc
import hashlib
import logging
from typing import Dict
from typing import Any, Callable, Dict, Tuple

from canonicaljson import encode_canonical_json
from signedjson.sign import sign_json
Expand All @@ -27,13 +27,18 @@

from synapse.api.errors import Codes, SynapseError
from synapse.api.room_versions import RoomVersion
from synapse.events import EventBase
from synapse.events.utils import prune_event, prune_event_dict
from synapse.types import JsonDict

logger = logging.getLogger(__name__)

Hasher = Callable[[bytes], "hashlib._Hash"]

def check_event_content_hash(event, hash_algorithm=hashlib.sha256):

def check_event_content_hash(
event: EventBase, hash_algorithm: Hasher = hashlib.sha256
) -> bool:
"""Check whether the hash for this PDU matches the contents"""
name, expected_hash = compute_content_hash(event.get_pdu_json(), hash_algorithm)
logger.debug(
Expand Down Expand Up @@ -67,18 +72,19 @@ def check_event_content_hash(event, hash_algorithm=hashlib.sha256):
return message_hash_bytes == expected_hash


def compute_content_hash(event_dict, hash_algorithm):
def compute_content_hash(
event_dict: Dict[str, Any], hash_algorithm: Hasher
) -> Tuple[str, bytes]:
"""Compute the content hash of an event, which is the hash of the
unredacted event.
Args:
event_dict (dict): The unredacted event as a dict
event_dict: The unredacted event as a dict
hash_algorithm: A hasher from `hashlib`, e.g. hashlib.sha256, to use
to hash the event
Returns:
tuple[str, bytes]: A tuple of the name of hash and the hash as raw
bytes.
A tuple of the name of hash and the hash as raw bytes.
"""
event_dict = dict(event_dict)
event_dict.pop("age_ts", None)
Expand All @@ -94,18 +100,19 @@ def compute_content_hash(event_dict, hash_algorithm):
return hashed.name, hashed.digest()


def compute_event_reference_hash(event, hash_algorithm=hashlib.sha256):
def compute_event_reference_hash(
event, hash_algorithm: Hasher = hashlib.sha256
) -> Tuple[str, bytes]:
"""Computes the event reference hash. This is the hash of the redacted
event.
Args:
event (FrozenEvent)
event
hash_algorithm: A hasher from `hashlib`, e.g. hashlib.sha256, to use
to hash the event
Returns:
tuple[str, bytes]: A tuple of the name of hash and the hash as raw
bytes.
A tuple of the name of hash and the hash as raw bytes.
"""
tmp_event = prune_event(event)
event_dict = tmp_event.get_pdu_json()
Expand Down Expand Up @@ -156,7 +163,7 @@ def add_hashes_and_signatures(
event_dict: JsonDict,
signature_name: str,
signing_key: SigningKey,
):
) -> None:
"""Add content hash and sign the event
Args:
Expand Down
Loading

0 comments on commit 1c9a850

Please sign in to comment.