Skip to content

Instantly share code, notes, and snippets.

@bemobtrk
Created June 10, 2019 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bemobtrk/ed5e638cfe3f3ea8f212fd1a1c473cb8 to your computer and use it in GitHub Desktop.
Save bemobtrk/ed5e638cfe3f3ea8f212fd1a1c473cb8 to your computer and use it in GitHub Desktop.
const crypto = require('crypto');
module.exports = (secretKey, ttl, signature) => {
try {
let sign = Buffer.from(signature, 'base64').toString('utf8');
sign = JSON.parse(sign);
if (!sign.timestamp || !sign.hash) return false;
const signedHash = crypto
.createHmac('sha1', secretKey)
.update(sign.timestamp)
.digest('hex');
if (signedHash !== sign.hash || sign.timestamp < (((new Date().getTime() / 1000) | 0) - ttl).toString()) return false;
return true;
} catch (err) {
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment