From 5bde2379fcf43a51fab7f3311f008e769a45cef8 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 17 Mar 2023 22:26:12 +0000 Subject: [PATCH] xor sha256 hashes --- followerSync.py | 22 ++++++++++++++-------- tests.py | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/followerSync.py b/followerSync.py index e6c0aac85..db5a1d582 100644 --- a/followerSync.py +++ b/followerSync.py @@ -8,6 +8,7 @@ __status__ = "Production" __module_group__ = "ActivityPub" import os +import hashlib from hashlib import sha256 from utils import acct_dir from utils import get_user_paths @@ -85,7 +86,7 @@ def _get_followers_sync_json(base_dir: str, return sync_json -def _get_followers_sync_hash(sync_json: {}) -> str: +def get_followers_sync_hash(sync_json: {}) -> str: """Returns a hash used within the Collection-Synchronization http header See https://github.com/mastodon/mastodon/pull/14510 https://codeberg.org/fediverse/fep/src/branch/main/feps/fep-8fcf.md @@ -94,14 +95,19 @@ def _get_followers_sync_hash(sync_json: {}) -> str: return None sync_hash = None for actor in sync_json['orderedItems']: - actor_hash = sha256(actor.encode('utf-8')) + curr_sync_hash = sha256(actor.encode('utf-8')) + sync_hash_hex = curr_sync_hash.hexdigest() + sync_hash_int = int(sync_hash_hex, 16) if sync_hash: - sync_hash = sync_hash ^ actor_hash + sync_hash = sync_hash ^ sync_hash_int else: - sync_hash = actor_hash - if sync_hash: - sync_hash = sync_hash.hexdigest() - return sync_hash + sync_hash = sync_hash_int + if sync_hash is None: + return None + sync_hash_int = sync_hash + sync_hash = hashlib.sha256() + sync_hash_bytes = sync_hash_int.to_bytes(32, 'big') + return sync_hash_bytes.hex() def update_followers_sync_cache(base_dir: str, @@ -124,7 +130,7 @@ def update_followers_sync_cache(base_dir: str, http_prefix, domain_full, calling_domain) - sync_hash = _get_followers_sync_hash(sync_json) + sync_hash = get_followers_sync_hash(sync_json) if sync_hash: sync_cache[foll_sync_key] = { "hash": sync_hash, diff --git a/tests.py b/tests.py index 91fd9e854..c0bd3b133 100644 --- a/tests.py +++ b/tests.py @@ -202,6 +202,7 @@ from happening import dav_day_via_server from webapp_theme_designer import color_contrast from maps import get_map_links_from_post_content from maps import geocoords_from_map_link +from followerSync import get_followers_sync_hash TEST_SERVER_GROUP_RUNNING = False @@ -7942,6 +7943,23 @@ def _test_convert_markdown() -> None: assert message_json['mediaType'] == 'text/html' +def _test_xor_hashes(): + print('xor_hashes') + sync_json = { + "orderedItems": [ + 'https://somedomain/users/somenick', + 'https://anotherdomain/users/anothernick' + ] + } + result = get_followers_sync_hash(sync_json) + expected = \ + '316f8dfdf471920a9cdc17da48feead398378e927dee3372d938c524aa7d8917' + if result != expected: + print('expected: ' + expected) + print('result: ' + result) + assert result == expected + + def run_all_tests(): base_dir = os.getcwd() print('Running tests...') @@ -7959,6 +7977,7 @@ def run_all_tests(): _test_checkbox_names() _test_thread_functions() _test_functions() + _test_xor_hashes() _test_convert_markdown() _test_remove_style() _test_html_closing_tag()