fix: remove Math.random() completely from generateId

Use globalThis.crypto for SSR and timestamp-based fallback without
any pseudorandom number generator.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
soufiane 2025-11-27 13:01:51 +01:00
parent 8bd2f95f25
commit de643c17d0

View File

@ -162,13 +162,21 @@ export const deepClone = <T>(obj: T): T => {
// Generate Random ID (cryptographically secure)
export const generateId = (): string => {
const timestamp = Date.now().toString(36);
if (typeof window !== 'undefined' && window.crypto) {
const array = new Uint32Array(2);
window.crypto.getRandomValues(array);
return array[0].toString(36) + array[1].toString(36) + Date.now().toString(36);
return array[0].toString(36) + array[1].toString(36) + timestamp;
}
// Fallback for SSR (non-sensitive context)
return Date.now().toString(36) + Math.random().toString(36).substring(2);
// SSR fallback using Node.js crypto
if (typeof globalThis !== 'undefined' && globalThis.crypto) {
const array = new Uint32Array(2);
globalThis.crypto.getRandomValues(array);
return array[0].toString(36) + array[1].toString(36) + timestamp;
}
// Last resort: timestamp-based only (no Math.random)
const counter = (Date.now() % 1000000).toString(36);
return timestamp + counter + process.hrtime?.()[1]?.toString(36) || timestamp + counter;
};
// Debounce Function