是否可以使用十六进制的16个字符创建GUID?我问的原因是Cloudflare使用16个字符来识别他们系统的每个请求(他们称之为#34; Ray ID")。与其他GUID格式相比,它们看起来更好(我知道这是愚蠢的偏好)。
密钥空间将包含以下字符:
0-9
a-f
---
16 total possible characters
Example: adttlo9dOd8haoww
此外,对生成这些内容的基本算法的任何提示都会很棒。
最后,我愿意离开" hex"格式和使用:
0-9
a-z
A-Z
---
62 total possible characters
Example: dhmpLTuPFWEwM8UL
答案 0 :(得分:0)
function d2h(d) {
return d.toString(16);
}
function h2d (h) {
return parseInt(h, 16);
}
function stringToHex (tmp) {
var str = '',
i = 0,
tmp_len = tmp.length,
c;
for (; i < tmp_len; i += 1) {
c = tmp.charCodeAt(i);
str += d2h(c) + ' ';
}
return str;
}
function hexToString (tmp) {
var arr = tmp.split(' '),
str = '',
i = 0,
arr_len = arr.length,
c;
for (; i < arr_len; i += 1) {
c = String.fromCharCode( h2d( arr[i] ) );
str += c;
}
return str;
}
//if you can get utc time is even bether
// Tue, 30 Jun 2015 23:01:04 GMT
var time = Date();
var server_point = "S1";
//you can encript this genrated id with blow fish or something, remember encripting existen bytes the length of the result wil grow
//remove spaces
var reg = new RegExp("[ ]+","g");
time = time.replace(reg, "");
var hexaResult = stringToHex(time + server_point);
alert(hexaResult.replace(reg, ""));
答案 1 :(得分:0)
Guids通常是32个十六进制字符,并且以不同的间隔使用短划线。
var crypto = require("crypto");
function create_guid() {
var hexstring = crypto.randomBytes(16).toString("hex"); // 16 bytes generates a 32 character hex string
var guidstring = hexstring.substring(0,8) + "-" + hexstring.substring(8,12) + "-" + hexstring.substring(12,16) + "-" + hexstring.substring(16,20) + "-" + hexstring.substring(20);
return guidstring;
}
如果您不想要破折号,只需修改上述功能即可返回hexstring
而不是guidstring
。如果只需要16个字符而不是32个字符:
function create_guid_simple() {
var hexstring = crypto.randomBytes(8).toString("hex"); // 8 bytes is a 16 character string
return hexstring;
}