From 180803d757d8eb7ecbc29e9580ae4238daf78067 Mon Sep 17 00:00:00 2001 From: Jonas Alves Date: Tue, 14 Jul 2026 23:07:59 +0100 Subject: [PATCH] feat: expose public hash-unit function for diagnostic use Extract the MD5 + base64url-no-padding unit hashing logic out of Context.get_unit_hash() into a standalone sdk.internal.hashing.hash_unit() function, so callers that only need the hash (e.g. diagnostic tooling) don't have to construct a full Context. Context.get_unit_hash() now delegates to it instead of duplicating the logic. --- sdk/context.py | 7 ++----- sdk/internal/hashing.py | 7 +++++++ 2 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 sdk/internal/hashing.py diff --git a/sdk/context.py b/sdk/context.py index 5555a82..989ab73 100644 --- a/sdk/context.py +++ b/sdk/context.py @@ -1,6 +1,4 @@ -import base64 import collections -import hashlib import threading from concurrent.futures import Future from typing import Optional @@ -10,6 +8,7 @@ from sdk.context_data_provider import ContextDataProvider from sdk.context_event_handler import ContextEventHandler from sdk.context_event_logger import ContextEventLogger, EventType +from sdk.internal import hashing from sdk.internal.lock.atomic_bool import AtomicBool from sdk.internal.lock.atomic_int import AtomicInt from sdk.internal.lock.concurrency import Concurrency @@ -536,9 +535,7 @@ def peek_treatment(self, experiment_name: str): def get_unit_hash(self, unit_type: str, unit_uid: str): def computer(key: str): - dig = hashlib.md5(unit_uid.encode('utf-8')).digest() - unithash = base64.urlsafe_b64encode(dig).rstrip(b'=') - return unithash + return hashing.hash_unit(unit_uid).encode('ascii') return Concurrency.compute_if_absent_rw( self.context_lock, diff --git a/sdk/internal/hashing.py b/sdk/internal/hashing.py new file mode 100644 index 0000000..9f56e1d --- /dev/null +++ b/sdk/internal/hashing.py @@ -0,0 +1,7 @@ +import base64 +import hashlib + + +def hash_unit(unit: str) -> str: + dig = hashlib.md5(unit.encode('utf-8')).digest() + return base64.urlsafe_b64encode(dig).rstrip(b'=').decode('ascii')