First update.
This commit is contained in:
26
CreateRoom.js
Normal file
26
CreateRoom.js
Normal file
@@ -0,0 +1,26 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.generateStealthId = generateStealthId;
|
||||
exports.isStealthId = isStealthId;
|
||||
function generateStealthId(length = 32) {
|
||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
const base = chars.length;
|
||||
const randomChars = (count) => Array.from({ length: count }, () => chars.charAt(Math.floor(Math.random() * base))).join('');
|
||||
// 生成前 length - 1 位
|
||||
const body = randomChars(length - 1);
|
||||
// 将 body 每个字符的 charCode 累加,再 mod base,作为校验位
|
||||
const sum = body.split('').reduce((acc, c) => acc + c.charCodeAt(0), 0);
|
||||
const checksum = chars.charAt(sum % base);
|
||||
return body + checksum;
|
||||
}
|
||||
function isStealthId(id) {
|
||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
const base = chars.length;
|
||||
if (!/^[A-Za-z0-9]{32}$/.test(id))
|
||||
return false;
|
||||
const body = id.slice(0, -1);
|
||||
const expectedChecksum = id.slice(-1);
|
||||
const sum = body.split('').reduce((acc, c) => acc + c.charCodeAt(0), 0);
|
||||
const checksum = chars.charAt(sum % base);
|
||||
return checksum === expectedChecksum;
|
||||
}
|
Reference in New Issue
Block a user