Skip to content

Commit

Permalink
Removes slice allocations (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
andriibeee authored Nov 16, 2024
1 parent 3b8aaeb commit 3761d84
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions totp.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,21 @@ func (t *TOTP) Validate(passcode string, at time.Time, secret string) error {
return ErrCodeLengthMismatch
}

counters := make([]uint64, 0, 2*t.cfg.Skew+1)
counter := int64(math.Floor(float64(at.Unix()) / float64(t.cfg.Period)))
counters = append(counters, uint64(counter))

for i := uint(1); i <= t.cfg.Skew; i++ {
counters = append(counters, uint64(counter+int64(i)), uint64(counter-int64(i)))
if err := t.hotp.Validate(passcode, uint64(counter), secret); err == nil {
return nil
}

for _, counter := range counters {
err := t.hotp.Validate(passcode, counter, secret)
if err == nil {
for i := uint(1); i <= t.cfg.Skew; i++ {
if err := t.hotp.Validate(passcode, uint64(counter+int64(i)), secret); err == nil {
return nil
}

if err := t.hotp.Validate(passcode, uint64(counter-int64(i)), secret); err == nil {
return nil
}
}

return ErrCodeIsNotValid
}

0 comments on commit 3761d84

Please sign in to comment.