Skip to content

Commit c8f300b

Browse files
denjilunny
authored andcommitted
Safe compare password (timing attack) (#338)
1 parent db6a4e9 commit c8f300b

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

models/user.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"container/list"
1010
"crypto/sha256"
11+
"crypto/subtle"
1112
"encoding/hex"
1213
"errors"
1314
"fmt"
@@ -368,7 +369,7 @@ func (u *User) EncodePasswd() {
368369
func (u *User) ValidatePassword(passwd string) bool {
369370
newUser := &User{Passwd: passwd, Salt: u.Salt}
370371
newUser.EncodePasswd()
371-
return u.Passwd == newUser.Passwd
372+
return subtle.ConstantTimeCompare([]byte(u.Passwd), []byte(newUser.Passwd)) == 1
372373
}
373374

374375
// UploadAvatar saves custom avatar for user.

0 commit comments

Comments
 (0)