('当前运行环境不支持密码学安全随机数生成器(CSPRNG)'); } } // 1 字节(Byte)会转化为 2 位十六进制字符,所以按需分配字节数组长度 c...

2026年06月04日 12:46 8.06 秒 success

错误信息

/** * 生成绝对安全的、指定长度的十六进制 AppSecret * @param length 期望生成的密钥长度(因为1字节转2位十六进制,建议传 32 或 64) * @returns 纯十六进制小写字符串 (例如 32位: "a1b2c3d4e5f6...") */ const generateSecureAppSecret = (length: number = 32): string => { const cryptoProvider = typeof window !== 'undefined' ? window.crypto : globalThis.crypto; if (!cryptoProvider || !cryptoProvider.getRandomValues) { // 针对旧版本 Node.js 环境的兼容兜底 (如果你在纯后端 Express 里跑) try { const nodeCrypto = require('crypto'); return nodeCrypto.randomBytes(length / 2).toString('hex'); } catch (e) { throw new Error('当前运行环境不支持密码学安全随机数生成器(CSPRNG)'); } } // 1 字节(Byte)会转化为 2 位十六进制字符,所以按需分配字节数组长度 const byteLength = Math.ceil(length / 2); const uint8Array = new Uint8Array(byteLength); // 由操作系统/硬件级安全随机数填充数组 cryptoProvider.getRandomValues(uint8Array); // 将字节数组高效转化为十六进制字符串 return Array.from(uint8Array) .map((byte: number) => byte.toString(16).padStart(2, '0')) .join('') .substring(0, length); // 严格截取到指定长度 }; // ==================== 使用方式 ==================== // 直接生成 32 位绝对安全的纯字母数字 Secret customerForm.appSecret = generateSecureAppSecret(32);

AI 解决方案

我只能分析代码报错信息,请提供具体的错误信息。