【问题标题】:How to genarate NodeJS signature code from hmacsha1 in NodeJS如何在 Node JS 中从 hmac sha1 生成 NodeJS 签名代码
【发布时间】:2025-11-23 02:50:01
【问题描述】:

当我请求一个函数来生成签名但签名哈希它仍然是时,时间戳和设备 ID 总是会改变

ea6b458e9a840b7f93236244bf1ea7cb564a8f08

这个哈希生成代码

let array = [login_type, device_id, timestamp]; let hash = crypto.createHmac('sha1', secret_key).update(implode(array, "|")).digest('hex');

function timeMil(){
    var date = new Date();
    var timeMil = date.getTime();
    return timeMil;
}

const device_id = "2752707c1c745ff8";
const secret_key = "9LXAVCxcITaABNK48pAVgc4muuTNJ4enIKS5YzKyGZ";
const timestamp = timeMil();

let array = [login_type, device_id, timestamp];
let hash = crypto.createHmac('sha1', secret_key).update(implode(array, "|")).digest('hex');

console.log(hash);

生成的 hash_hmac 总是 ea6b458e9a840b7f93236244bf1ea7cb564a8f08

【问题讨论】:

    标签: javascript node.js sha1 hmacsha1


    【解决方案1】:

    JavaScript 中没有“内爆”函数;它的等价物是在数组上使用join

    const crypto = require('crypto');
    
    function timeMil(){
       return new Date().getTime();
    }
    
    const login_type = 'test';
    const device_id = "2752707c1c745ff8";
    const secret_key = "9LXAVCxcITaABNK48pAVgc4muuTNJ4enIKS5YzKyGZ";
    const timestamp = timeMil();
    
    let array = [login_type, device_id, timestamp];
    let hash = crypto.createHmac('sha1', secret_key).update(array.join("|")).digest('hex');
    
    console.log(hash);
    

    【讨论】:

    • 谢谢。解决了
    最近更新 更多