from hashlib import sha256 _AUDIT_IDENTITY_DOMAIN = b"company-ai-platform:feishu-audit-identity:v1\0" def feishu_audit_identity_hash(tenant_key: str, open_id: str) -> str: """Return the erasable pseudonymous subject used by pre-registration audits.""" tenant_bytes = tenant_key.encode("utf-8") open_id_bytes = open_id.encode("utf-8") identity = b"".join( ( len(tenant_bytes).to_bytes(4, "big"), tenant_bytes, len(open_id_bytes).to_bytes(4, "big"), open_id_bytes, ) ) digest = sha256(_AUDIT_IDENTITY_DOMAIN + identity).hexdigest() return f"feishu-identity-sha256-{digest}"