· 5 years ago · Dec 07, 2019, 08:50 PM
1const expectedHash = body.hash;
2 const checkString = Object.entries(body)
3 .filter(e => e[0] != 'hash')
4 .sort((e1, e2) => e1[0].localeCompare(e2[0]))
5 .map(([key, val]) => key + '=' + val)
6 .join('\n');
7
8 const secretKey = crypto.createHash('sha256')
9 .update(token).digest('hex');
10
11 const hash = crypto.createHmac('sha256', checkString, secretKey).digest('hex');
12
13 console.log({
14 expectedHash,
15 hash,
16 equal: expectedHash == hash, // false
17 });