// Copyright 2026 OpenObserve Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
/** Hash of "email:org" (djb2 without crypto.subtle); an org-only key leaks one user's data to the next login. */
export const computeUserOrgKey = async (
userEmail: string,
orgIdentifier: string,
): Promise => {
const raw = `${userEmail}:${orgIdentifier}`;
if (typeof crypto !== "undefined" && crypto.subtle) {
const buf = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(raw));
return Array.from(new Uint8Array(buf))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
// Synchronous djb2 fallback (test environments without crypto.subtle)
let hash = 5381;
for (let i = 0; i < raw.length; i++) {
hash = (((hash << 5) + hash) ^ raw.charCodeAt(i)) >>> 0;
}
return hash.toString(36);
};