根据用户详细信息在 Javascript 中创建一个随机令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8532406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a random token in Javascript based on user details
提问by fire
I want to create a random string (token) which can be used to identify a user whilst avoiding any potential conflicts with any other users' tokens.
我想创建一个随机字符串(令牌),可用于识别用户,同时避免与任何其他用户的令牌发生任何潜在冲突。
What I was thinking of was an MD5 hash of navigator.userAgent + new Date().getTime()
to generate the token but that requires a whole Javascript MD5 library to hash it which I don't really want to do.
我在想的是navigator.userAgent + new Date().getTime()
生成令牌的 MD5 散列,但这需要整个 Javascript MD5 库来散列它,我真的不想这样做。
It has to be made up of A-Z/0-9 characters and ideally no longer than 32 characters. I am open to all ideas. Thanks!
它必须由 AZ/0-9 个字符组成,最好不要超过 32 个字符。我对所有想法持开放态度。谢谢!
Just to clarify I'm not looking for any random string generator, the random string has to be generated from the users details available through Javascript and can also use time to avoid potential conflicts!
只是为了澄清我不是在寻找任何随机字符串生成器,随机字符串必须从通过 Javascript 提供的用户详细信息生成,并且还可以利用时间来避免潜在的冲突!
采纳答案by Josnidhin
回答by pimvdb
You could generate a random number and convert it to base 36 (0-9a-z
):
您可以生成一个随机数并将其转换为基数 36 ( 0-9a-z
):
var rand = function() {
return Math.random().toString(36).substr(2); // remove `0.`
};
var token = function() {
return rand() + rand(); // to make it longer
};
token(); // "bnh5yzdirjinqaorq0ox1tf383nb3xr"
回答by Kareem
This function allows you to set the token length and allowed characters.
此功能允许您设置令牌长度和允许的字符。
function generate_token(length){
//edit the token allowed characters
var a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".split("");
var b = [];
for (var i=0; i<length; i++) {
var j = (Math.random() * (a.length-1)).toFixed(0);
b[i] = a[j];
}
return b.join("");
}
Simply call generate_token
只需调用 generate_token
generate_token(32); //returns "qweQj4giRJSdMNzB8g1XIa6t3YtRIHPH"
回答by Jasp402
//length: defines the length of characters to express in the string
const rand=()=>Math.random(0).toString(36).substr(2);
const token=(length)=>(rand()+rand()+rand()+rand()).substr(0,length);
console.log(token(40));
//example1: token(10) => result: tsywlmdqu6
//example2: token(40) => result: m4vni14mtln2547gy54ksclhcv0dj6tp9fhs1k10
回答by ThisClark
I use an approach similar to Kareem's, but with fewer function calls and built-in array operations for a big boost in performance.
我使用类似于Kareem 的方法,但使用较少的函数调用和内置数组操作来大幅提升性能。
According to a performance test, this method also outperforms the accepted answer by a small margin. Moreover it provides a parameter n
to generate any size token length from a white list of acceptable characters. It's flexible and performs well.
根据性能测试,该方法的性能也略高于公认的答案。此外,它提供了一个参数n
来从可接受字符的白名单中生成任何大小的令牌长度。它灵活且性能良好。
function generateToken(n) {
var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var token = '';
for(var i = 0; i < n; i++) {
token += chars[Math.floor(Math.random() * chars.length)];
}
return token;
}
回答by shaack
It is very unlikely, but Math.random() could return 0.0
. In that case, the solution of pimvdbwould return ""
(empty string). So, here is another solution, which returns in every case a random base36 with 10 chars length:
这不太可能,但 Math.random() 可能会返回0.0
。在这种情况下,pimvdb的解决方案将返回""
(空字符串)。所以,这是另一种解决方案,它在每种情况下都返回一个具有 10 个字符长度的随机 base36:
function generateToken() {
Math.floor(1000000000000000 + Math.random() * 9000000000000000)
.toString(36).substr(0, 10)
}