Use sha256 for stamp number to avoid collisions

main
bashrc 2026-07-19 11:41:11 +01:00
parent 3e3ffc51c1
commit f190f0ec8c
2 changed files with 11 additions and 16 deletions

View File

@ -283,13 +283,18 @@ def get_featured_collections_feed(base_dir: str,
def store_feature_authorization(base_dir: str, nickname: str, domain: str, def store_feature_authorization(base_dir: str, nickname: str, domain: str,
featured_item: {}) -> bool: featured_item: {}) -> bool:
"""Stores a feature authorization """Stores a feature authorization after receiving a request for a
particular actor or hashtag
""" """
if not featured_item.get('featureAuthorization'):
return False
if not isinstance(featured_item['featureAuthorization'], str):
return False
accounts_dir: str = acct_dir(base_dir, nickname, domain) accounts_dir: str = acct_dir(base_dir, nickname, domain)
if not is_a_dir(accounts_dir + '/stamps'): if not is_a_dir(accounts_dir + '/stamps'):
makedir(accounts_dir + '/stamps') makedir(accounts_dir + '/stamps')
stamp_id = featured_item['featureAuthorization'].split('/')[1] stamp_id = url_text_to_number(featured_item['featureAuthorization'])
feature_authorization_filename = accounts_dir + '/' + stamp_id feature_authorization_filename = accounts_dir + '/stamps/' + stamp_id
if not is_a_file(feature_authorization_filename): if not is_a_file(feature_authorization_filename):
return False return False
return save_json(featured_item, feature_authorization_filename) return save_json(featured_item, feature_authorization_filename)

View File

@ -17,6 +17,7 @@ import datetime
import json import json
import locale import locale
import idna import idna
import base64
from cryptography.hazmat.backends import default_backend from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives import hashes
from src.followingCalendar import add_person_to_calendar from src.followingCalendar import add_person_to_calendar
@ -58,8 +59,6 @@ INVALID_ACTOR_URL_CHARACTERS = (
';', '=' ';', '='
) )
URL_TEXT_MAGIC_NUMBER = 7439
def is_account_dir(dir_name: str) -> bool: def is_account_dir(dir_name: str) -> bool:
"""Is the given directory an account within /accounts ? """Is the given directory an account within /accounts ?
@ -4381,14 +4380,5 @@ def get_preferred_username(actor: {}, system_language: str) -> str:
def url_text_to_number(url: str) -> str: def url_text_to_number(url: str) -> str:
"""converts url text or collection name to a number string used as an id """converts url text or collection name to a number string used as an id
""" """
result = '' hash_result = get_sha_256(url)
for char in url: return base64.b64encode(hash_result).decode('utf-8')
num = ord(char)
if num > 99:
continue
if num > 9:
result = str(num) + result
else:
result = '0' + str(num) + result
num = (URL_TEXT_MAGIC_NUMBER + int(result)) % 9999999999999999999999999999
return str(num)