Dake
Dake

Reputation: 81

How to generate NodeJS signature code from hmacsha1

timestamp and device_id are always changed when I request a function to generate signature, but the signature hash it generates is always

ea6b458e9a840b7f93236244bf1ea7cb564a8f08

Hash generating code:

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);

Genarated hash_hmac alway is ea6b458e9a840b7f93236244bf1ea7cb564a8f08

Upvotes: 2

Views: 1026

Answers (1)

Chris White
Chris White

Reputation: 1063

There is no "implode" function in JavaScript; its equivalent is using join on an array:

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);

Upvotes: 2

Related Questions