Skip to content

Commit

Permalink
use replaceAll
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Jan 24, 2024
1 parent 0fe1f73 commit c691788
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

const crypto = require('node:crypto')

const base64PaddingRE = /=/gu

function Signer (secrets, algorithm = 'sha256') {
if (!(this instanceof Signer)) {
return new Signer(secrets, algorithm)
Expand Down Expand Up @@ -45,12 +43,13 @@ function _sign (value, secret, algorithm) {
if (typeof value !== 'string') {
throw new TypeError('Cookie value must be provided as a string.')
}

return value + '.' + crypto
.createHmac(algorithm, secret)
.update(value)
.digest('base64')
// remove base64 padding (=) as it has special meaning in cookies
.replace(base64PaddingRE, '')
.replaceAll('=', '')
}

function _unsign (signedValue, secrets, algorithm) {
Expand All @@ -67,7 +66,8 @@ function _unsign (signedValue, secrets, algorithm) {
.update(value)
.digest('base64')
// remove base64 padding (=) as it has special meaning in cookies
.replace(base64PaddingRE, ''))
.replaceAll('=', ''))

if (
expected.length === actual.length &&
crypto.timingSafeEqual(expected, actual)
Expand Down

0 comments on commit c691788

Please sign in to comment.