lab 7 done

This commit is contained in:
Khakimov Dmitrij
2026-11-29 21:59:16 +03:00
parent 20afc2c790
commit 2a7418fc45
170 changed files with 10332 additions and 96 deletions
+50
View File
@@ -0,0 +1,50 @@
package password_test
import (
"context"
"strings"
"testing"
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/password"
)
func TestUnicodeAndSalt(t *testing.T) {
ctx := context.Background()
secret := strings.Repeat("🔑", 128)
first, err := password.Hash(ctx, secret)
if err != nil {
t.Fatal(err)
}
second, err := password.Hash(ctx, secret)
if err != nil {
t.Fatal(err)
}
if first == second {
t.Fatal("password hashes reused salt")
}
for _, hash := range []string{first, second} {
ok, err := password.Verify(ctx, secret, hash)
if err != nil || !ok {
t.Fatal("128-code-point UTF-8 password rejected")
}
ok, err = password.Verify(ctx, strings.Repeat("🔑", 127)+"x", hash)
if err != nil || ok {
t.Fatal("password suffix was ignored")
}
}
}
func BenchmarkVerify(b *testing.B) {
ctx := context.Background()
secret := "synthetic-test-password"
hash, err := password.Hash(ctx, secret)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if ok, err := password.Verify(ctx, secret, hash); err != nil || !ok {
b.Fatal("valid password rejected")
}
}
}