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:
parent
8bd2f95f25
commit
de643c17d0
|
|
@ -162,13 +162,21 @@ export const deepClone = <T>(obj: T): T => {
|
||||||
|
|
||||||
// Generate Random ID (cryptographically secure)
|
// Generate Random ID (cryptographically secure)
|
||||||
export const generateId = (): string => {
|
export const generateId = (): string => {
|
||||||
|
const timestamp = Date.now().toString(36);
|
||||||
if (typeof window !== 'undefined' && window.crypto) {
|
if (typeof window !== 'undefined' && window.crypto) {
|
||||||
const array = new Uint32Array(2);
|
const array = new Uint32Array(2);
|
||||||
window.crypto.getRandomValues(array);
|
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)
|
// SSR fallback using Node.js crypto
|
||||||
return Date.now().toString(36) + Math.random().toString(36).substring(2);
|
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
|
// Debounce Function
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user