nodejs cryptorandomBytes16toStringhex; 生成不不包含数字token
可以使用以下代码来生成不包含数字的 token:
const crypto = require('crypto');
function generateToken(length) {
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let token = '';
while (token.length < length) {
const bytes = crypto.randomBytes(1);
const char = chars[bytes[0] % chars.length];
token += char;
}
return token;
}
const token = generateToken(16);
console.log(token);
这个函数会生成一个指定长度的随机字符串,只包含字母,不包含数字。你可以将 generateToken 函数的返回值作为 token 使用。
原文地址: https://www.cveoy.top/t/topic/e4HR 著作权归作者所有。请勿转载和采集!