Skip to content

Commit d789cd5

Browse files
committed
use strict base64 decoder for strict mode
1 parent 4e6f2d8 commit d789cd5

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

hmac/hmac.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ func NewHasher(secret []byte) hash.Hash {
2323
type Verifier struct {
2424
mac hash.Hash
2525
buf []byte
26+
dec *base64.Encoding
2627
strict bool
2728
}
2829

2930
func NewVerifier(secret []byte, strict bool) *Verifier {
31+
dec := base64.RawURLEncoding
32+
if strict {
33+
dec = dec.Strict()
34+
}
3035
return &Verifier{
3136
mac: hmac.New(sha256.New, secret),
3237
strict: strict,
38+
dec: dec,
3339
}
3440
}
3541

@@ -40,13 +46,13 @@ func (v *Verifier) ensureBufferSize(size int) {
4046
}
4147

4248
func (v *Verifier) VerifyLoginAndPassword(login, password []byte) bool {
43-
if v.strict && len(password) != base64.RawURLEncoding.EncodedLen(HMACExpireSize+v.mac.Size()) {
49+
if v.strict && len(password) != v.dec.EncodedLen(HMACExpireSize+v.mac.Size()) {
4450
return false
4551
}
4652

47-
v.ensureBufferSize(base64.RawURLEncoding.DecodedLen(len(password)))
53+
v.ensureBufferSize(v.dec.DecodedLen(len(password)))
4854
buf := v.buf
49-
n, _ := base64.RawURLEncoding.Decode(buf, password)
55+
n, _ := v.dec.Decode(buf, password)
5056
buf = buf[:n]
5157

5258
var expire int64

0 commit comments

Comments
 (0)