forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_id.py
More file actions
42 lines (35 loc) · 1.83 KB
/
Copy pathcontent_id.py
File metadata and controls
42 lines (35 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Stable, privacy-safe identities for sync batches across job re-uploads."""
import hashlib
import hmac
import os
from pathlib import Path
from typing import Iterable
def compute_sync_content_id(uid: str, paths: Iterable[str]) -> str:
secret = os.getenv('SYNC_CONTENT_ID_SECRET') or os.getenv('ENCRYPTION_SECRET')
if not secret:
raise RuntimeError('SYNC_CONTENT_ID_SECRET or ENCRYPTION_SECRET is required for durable sync idempotency')
file_digests: list[str] = []
for path in paths:
digest = hashlib.sha256()
with open(path, 'rb') as audio_file:
while chunk := audio_file.read(1024 * 1024):
digest.update(chunk)
# The basename carries the capture timestamp/codec in the sync
# protocol. Include it so two distinct recordings with identical
# bytes (notably silence) cannot collapse onto one ledger entry, while
# remaining stable across temporary job directories.
file_digests.append(f'{Path(path).name}:{digest.hexdigest()}')
canonical = '\n'.join(sorted(file_digests))
return hmac.new(secret.encode(), f'{uid}\n{canonical}'.encode(), hashlib.sha256).hexdigest()
def compute_sync_segment_id(uid: str, path: str) -> str:
"""Stable identity for one VAD segment across job-directory changes."""
secret = os.getenv('SYNC_CONTENT_ID_SECRET') or os.getenv('ENCRYPTION_SECRET')
if not secret:
raise RuntimeError('SYNC_CONTENT_ID_SECRET or ENCRYPTION_SECRET is required for durable sync idempotency')
digest = hashlib.sha256()
with open(path, 'rb') as audio_file:
while chunk := audio_file.read(1024 * 1024):
digest.update(chunk)
logical_name = Path(path).name
payload = f'{uid}\n{logical_name}\n{digest.hexdigest()}'
return hmac.new(secret.encode(), payload.encode(), hashlib.sha256).hexdigest()