refactor: follow the google go style guide
Document every package and exported identifier, return the log level from NewLogger instead of keeping it in a package variable, and stop tests from mutating the argon2 parameters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
048e114fd9
commit
d20225982a
21 files changed
+147
-36
No files matched your search
@@ -7,11 +7,11 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
// OWASP 2024 baseline for argon2id.
|
||||
type argonParams struct {
|
||||
memory uint32
|
||||
time uint32
|
||||
@@ -19,24 +19,32 @@ type argonParams struct {
|
||||
keyLen uint32
|
||||
}
|
||||
|
||||
var current = argonParams{memory: 19 * 1024, time: 2, threads: 1, keyLen: 32}
|
||||
// defaultParams is the OWASP baseline for argon2id. Raising it makes
|
||||
// VerifyPassword request a rehash of older hashes on their next login.
|
||||
var defaultParams = argonParams{memory: 19 * 1024, time: 2, threads: 1, keyLen: 32}
|
||||
|
||||
// MinPasswordLength follows NIST SP 800-63B; no composition rules apply.
|
||||
const MinPasswordLength = 12
|
||||
|
||||
// ErrPasswordTooShort is returned by HashPassword.
|
||||
var ErrPasswordTooShort = fmt.Errorf("auth: password must have at least %d characters", MinPasswordLength)
|
||||
|
||||
// HashPassword returns an argon2id hash in PHC string format.
|
||||
func HashPassword(pw string) (string, error) {
|
||||
if len([]rune(pw)) < MinPasswordLength {
|
||||
return "", ErrPasswordTooShort
|
||||
}
|
||||
return hashPassword(pw, defaultParams), nil
|
||||
}
|
||||
|
||||
func hashPassword(pw string, p argonParams) string {
|
||||
salt := make([]byte, 16)
|
||||
_, _ = rand.Read(salt)
|
||||
p := current
|
||||
hash := argon2.IDKey([]byte(pw), salt, p.time, p.memory, p.threads, p.keyLen)
|
||||
return fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
|
||||
argon2.Version, p.memory, p.time, p.threads,
|
||||
base64.RawStdEncoding.EncodeToString(salt),
|
||||
base64.RawStdEncoding.EncodeToString(hash)), nil
|
||||
base64.RawStdEncoding.EncodeToString(hash))
|
||||
}
|
||||
|
||||
// VerifyPassword reports whether pw matches and whether the stored hash uses
|
||||
@@ -48,13 +56,14 @@ func VerifyPassword(encoded, pw string) (ok, rehash bool, err error) {
|
||||
}
|
||||
got := argon2.IDKey([]byte(pw), salt, p.time, p.memory, p.threads, uint32(len(want)))
|
||||
ok = subtle.ConstantTimeCompare(got, want) == 1
|
||||
return ok, ok && p != current, nil
|
||||
return ok, ok && p != defaultParams, nil
|
||||
}
|
||||
|
||||
// dummyHash keeps the response time of unknown accounts equal to known ones.
|
||||
var dummyHash, _ = HashPassword("not-a-real-password-for-timing")
|
||||
var dummyHash = sync.OnceValue(func() string { return hashPassword("timing-equalizer", defaultParams) })
|
||||
|
||||
func BurnPasswordCheck(pw string) { _, _, _ = VerifyPassword(dummyHash, pw) }
|
||||
// BurnPasswordCheck spends the time of a real verification. Call it when the
|
||||
// account does not exist so response times do not reveal that.
|
||||
func BurnPasswordCheck(pw string) { _, _, _ = VerifyPassword(dummyHash(), pw) }
|
||||
|
||||
func decodeHash(s string) (argonParams, []byte, []byte, error) {
|
||||
var p argonParams
|
||||
|
||||
Reference in new issue
Block a user