Skip to content

Commit 0b46086

Browse files
committed
Use constant time hash comparison
1 parent 9ba5309 commit 0b46086

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

password.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package password
99

1010
import (
1111
"crypto/rand"
12+
"crypto/subtle"
1213
"encoding/base64"
1314
"encoding/hex"
1415
"fmt"
@@ -230,11 +231,13 @@ func verifyV1(userpass, masterpass string, parts []string) (err error) {
230231
if err != nil {
231232
return err
232233
}
233-
for i := range userpassScrypt {
234-
if decrypted[i] != userpassScrypt[i] {
235-
return ErrPassphraseHashMismatch
236-
}
234+
235+
// Compare given hash input to generated hash
236+
if res := subtle.ConstantTimeCompare(decrypted, userpassScrypt); res != 1 {
237+
// return nil only if supplied hash and computed hash from passphrase match
238+
return ErrPassphraseHashMismatch
237239
}
240+
238241
return err
239242
}
240243
func validateParams(p ScryptParams) error {

0 commit comments

Comments
 (0)