贾迎博 2024-08-01 15:26:42

PathHash.js

/**
 * @author 贾迎博
 * @description 生成路径中用于混淆的目录名。
 * 使用方式:直接在命令行运行即可:node PathHash.js
 *
 * @version 20231205
 */

const crypto = require('crypto');

// hash算法
function encrypt() {
    // 生成一个随机密钥
    const secretKey = crypto.randomBytes(32).toString('base64');
    // 加密
    const hash = crypto.createHmac('sha256', secretKey) // 注入密钥
        .update(new Date().getTime() + '') // 注入需要加密的字符串
        .digest('hex'); // 可选参数'base64'可生成大小写+数字组合

    // 保留前12位作为结果返回 可根据需求自行修改 原始长度为32位
    return hash.substring(0, 6);
}


console.log(`
生成结果为: ${encrypt()}
复制到粘贴板使用即可
`);