cocos uuid解密脚本

cocos uuid解密脚本

//22位压缩后的 UUID解码配置
const BASE64_KEYS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const values: number[] = new Array(123); // max char code in base64Keys
for (let i = 0; i < 123; ++i) { values[i] = 64; } // fill with placeholder('=') index
for (let i = 0; i < 64; ++i) { values[BASE64_KEYS.charCodeAt(i)] = i; }

//23位压缩后的 UUID解码配置
let Base64KeyChars ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for (var AsciiTo64 = new Array(128), i = 0; i < 128; ++i) { AsciiTo64[i] = 0;}
for (i = 0; i < 64; ++i) {AsciiTo64[Base64KeyChars.charCodeAt(i)] = i;}

// 其他配置
const BASE64_VALUES = values; 
const HexChars = '0123456789abcdef'.split('');
const _t = ['', '', '', ''];
const UuidTemplate = _t.concat(_t, '-', _t, '-', _t, '-', _t, '-', _t, _t, _t);
const Indices = UuidTemplate.map((x, i) => (x === '-' ? NaN : i)).filter(isFinite);

/**
 * 22位压缩后的 UUID解码
 * @param base64 压缩后的 UUID 字符串
 * @returns 解码后的标准 UUID 字符串
 */
export default function decodeUuid(base64: string) {
    const strs = base64.split('@');
    const uuid = strs[0];
    if (uuid.length !== 22) {
      return decompressUuid(uuid);
    }
    const result = new Array(36).fill('-');
    result[0] = base64[0];
    result[1] = base64[1];
    let index = 2;
    for (let i = 2; i < 22; i += 2) {
        const lhs = BASE64_VALUES[base64.charCodeAt(i)];
        const rhs = BASE64_VALUES[base64.charCodeAt(i + 1)];
        result[Indices[index++]] = HexChars[lhs >> 2];
        result[Indices[index++]] = HexChars[((lhs & 3) << 2) | rhs >> 4];
        result[Indices[index++]] = HexChars[rhs & 0xF];
    }
    return result.join('');
}

/**
 * 23位压缩后的 UUID解码
 * @param uuid 压缩后的 UUID 字符串
 * @returns 解码后的标准 UUID 字符串
 */
function decompressUuid(uuid: string) {
    // 检查输入的 uuid 长度是否符合要求
    if (uuid.length < 23) {
        console.error('Invalid UUID length. Expected at least 23 characters.');
        return '';
    }
    // 存储解码后的十六进制字符
    const decodedHexChars: string[] = [];
    // 从第 5 个字符开始,每 2 个字符一组进行解码
    for (let index = 5; index < 23; index += 2) {
        const firstCharValue = AsciiTo64[uuid.charCodeAt(index)];
        const secondCharValue = AsciiTo64[uuid.charCodeAt(index + 1)];
        // 解码逻辑
        decodedHexChars.push((firstCharValue >> 2).toString(16));
        decodedHexChars.push((((3 & firstCharValue) << 2) | (secondCharValue >> 4)).toString(16));
        decodedHexChars.push((15 & secondCharValue).toString(16));
    }
    // 拼接解码后的 uuid
    const decodedUuid = uuid.slice(0, 5) + decodedHexChars.join("");
    // 格式化 uuid 为标准格式
    return [
        decodedUuid.slice(0, 8),
        decodedUuid.slice(8, 12),
        decodedUuid.slice(12, 16),
        decodedUuid.slice(16, 20),
        decodedUuid.slice(20),
    ].join("-");
}

 

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容