Skip to content

Commit 3207cd6

Browse files
committed
db: compare password hashes in constant time
CheckPassword compared the derived hash against the stored one with a plain string comparison, whose timing depends on how many leading bytes match. Use a constant-time comparison to avoid leaking that information. Signed-off-by: Robin Jarry <robin@jarry.cc>
1 parent 8f017f0 commit 3207cd6

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

‎pkg/db/password.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package db
88
import (
99
"crypto/rand"
1010
"crypto/sha256"
11+
"crypto/subtle"
1112
"encoding/base64"
1213
"fmt"
1314
"strconv"
@@ -56,5 +57,5 @@ func CheckPassword(password, encoded string) bool {
5657
)
5758
hash := base64.StdEncoding.EncodeToString(dk)
5859

59-
return hash == expected
60+
return subtle.ConstantTimeCompare([]byte(hash), []byte(expected)) == 1
6061
}

0 commit comments

Comments
 (0)