forked from mtusi2026/mtusi2026lab7
34 lines
1.1 KiB
Go
34 lines
1.1 KiB
Go
package password
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"golang.org/x/crypto/argon2"
|
|
"testing"
|
|
)
|
|
|
|
func TestLegacyParametersVerifyAndRequireRehash(t *testing.T) {
|
|
salt := []byte("1234567890abcdef")
|
|
hash := argon2.IDKey([]byte("synthetic-password"), salt, 1, 8192, 1, 32)
|
|
encoded := fmt.Sprintf("argon2id$v19$m8192t1p1$%s$%s", base64.RawStdEncoding.EncodeToString(salt), base64.RawStdEncoding.EncodeToString(hash))
|
|
a := Argon{}
|
|
if !a.Verify("synthetic-password", encoded) {
|
|
t.Fatal("valid legacy password rejected")
|
|
}
|
|
if !a.NeedsRehash(encoded) {
|
|
t.Fatal("legacy cost must upgrade")
|
|
}
|
|
current, e := a.Hash("synthetic-password")
|
|
if e != nil || !a.Verify("synthetic-password", current) || a.NeedsRehash(current) {
|
|
t.Fatal("current profile must verify without repeated rehash")
|
|
}
|
|
for _, bad := range []string{"argon2id$v19$m4294967295t2p1$x$x", "argon2id$v19$m32768t9999p1$x$x", encoded + "$extra", "bad"} {
|
|
if a.Verify("synthetic-password", bad) {
|
|
t.Fatal("unsafe encoded parameters accepted")
|
|
}
|
|
}
|
|
if a.Verify("wrong-password", encoded) {
|
|
t.Fatal("wrong password accepted")
|
|
}
|
|
}
|