mirror of
https://github.com/nestriness/nestri.git
synced 2026-09-19 17:25:19 +03:00
feat: Sync to OSS repo
This commit is contained in:
24
packages/auth/src/random.ts
Normal file
24
packages/auth/src/random.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { timingSafeEqual } from 'node:crypto';
|
||||
|
||||
export function generateUnbiasedDigits(length: number): string {
|
||||
const result: number[] = [];
|
||||
while (result.length < length) {
|
||||
const buffer = crypto.getRandomValues(new Uint8Array(length * 2));
|
||||
for (const byte of buffer) {
|
||||
if (byte < 250 && result.length < length) {
|
||||
result.push(byte % 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.join('');
|
||||
}
|
||||
|
||||
export function timingSafeCompare(a: string, b: string): boolean {
|
||||
if (typeof a !== 'string' || typeof b !== 'string') {
|
||||
return false;
|
||||
}
|
||||
if (a.length !== b.length) {
|
||||
return false;
|
||||
}
|
||||
return timingSafeEqual(Buffer.from(a), Buffer.from(b));
|
||||
}
|
||||
Reference in New Issue
Block a user