diff --git a/lib/verify.js b/lib/verify.js index b053fd6..3923868 100644 --- a/lib/verify.js +++ b/lib/verify.js @@ -73,7 +73,22 @@ module.exports = { h1.update(hmac.digest()); h1 = h1.digest(); var h2 = crypto.createHmac(hashAlg, secret); - h2.update(new Buffer(parsedSignature.params.signature, 'base64')); + + var signatureBase64 = parsedSignature.params.signature; + var signatureBuffer; + if (Buffer.from && Buffer.from !== Uint8Array.from) { + // Node.js 4.5.0 and newer + signatureBuffer = Buffer.from(signatureBase64, 'base64'); + } else { + // Node.js <4.5.0 || >=5.0.0 <5.10.0 + if (typeof signatureBase64 === 'number') { + // type-guard against uninitentional uninitialized Buffer allocation + throw new Error('Unexpected .signature type: number, string expected'); + } + signatureBuffer = new Buffer(signatureBase64, 'base64'); + } + h2.update(signatureBuffer); + h2 = h2.digest(); /* Node 0.8 returns strings from .digest(). */