fix: use crypto-adapter in generateApiKey
- Replace direct crypto.getRandomValues with getCryptoRandomValues - Fixes Node.js test environment compatibility - Maintains production behavior in Electron
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
* Utility functions for authentication and API key management
|
* Utility functions for authentication and API key management
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { getCryptoRandomValues } from './crypto-adapter';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a cryptographically secure random API key
|
* Generates a cryptographically secure random API key
|
||||||
* @param length Length of the API key (default: 32 characters)
|
* @param length Length of the API key (default: 32 characters)
|
||||||
@@ -10,15 +12,15 @@
|
|||||||
export function generateApiKey(length: number = 32): string {
|
export function generateApiKey(length: number = 32): string {
|
||||||
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
||||||
const values = new Uint8Array(length);
|
const values = new Uint8Array(length);
|
||||||
|
|
||||||
// Use crypto.getRandomValues for cryptographically secure random numbers
|
// Use cross-environment crypto adapter
|
||||||
crypto.getRandomValues(values);
|
getCryptoRandomValues(values);
|
||||||
|
|
||||||
let result = '';
|
let result = '';
|
||||||
for (let i = 0; i < length; i++) {
|
for (let i = 0; i < length; i++) {
|
||||||
result += charset[values[i] % charset.length];
|
result += charset[values[i] % charset.length];
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user