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')