Created
June 10, 2019 09:29
-
-
Save bemobtrk/ed5e638cfe3f3ea8f212fd1a1c473cb8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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