feat: secure service connections and login

This commit is contained in:
2026-11-06 21:14:49 +03:00
parent cb51354889
commit 48918e8b19
59 changed files with 1980 additions and 316 deletions
+8
View File
@@ -68,6 +68,14 @@ func (h *Hasher) Verify(value, encodedHash string) (bool, error) {
return subtle.ConstantTimeCompare(actual, digest) == 1, nil
}
func (h *Hasher) NeedsRehash(encodedHash string) (bool, error) {
config, _, _, err := decode(encodedHash)
if err != nil {
return false, err
}
return config != h.config, nil
}
func (h *Hasher) DummyHash() string {
return h.dummyHash
}
+55
View File
@@ -39,6 +39,61 @@ func (s *PasswordSuite) TestVerifyRejectsMalformedHash() {
s.Error(err)
}
func (s *PasswordSuite) TestNeedsRehashDetectsChangedParameters() {
currentHash, err := s.hasher.Hash("T3st-password-123")
s.Require().NoError(err)
needsRehash, err := s.hasher.NeedsRehash(currentHash)
s.Require().NoError(err)
s.False(needsRehash)
legacyConfig := testConfig()
legacyConfig.KeyLength = 16
legacy, err := New(legacyConfig)
s.Require().NoError(err)
legacyHash, err := legacy.Hash("T3st-password-123")
s.Require().NoError(err)
needsRehash, err = s.hasher.NeedsRehash(legacyHash)
s.Require().NoError(err)
s.True(needsRehash)
}
func testConfig() Config {
return Config{MemoryKiB: 8 * 1024, Iterations: 1, Parallelism: 1, SaltLength: 16, KeyLength: 32}
}
func BenchmarkArgon2idHash(b *testing.B) {
hasher, err := New(productionConfig())
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
if _, err := hasher.Hash("T3st-password-123"); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkArgon2idVerify(b *testing.B) {
hasher, err := New(productionConfig())
if err != nil {
b.Fatal(err)
}
hash, err := hasher.Hash("T3st-password-123")
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
verified, err := hasher.Verify("T3st-password-123", hash)
if err != nil || !verified {
b.Fatalf("verify: result=%v error=%v", verified, err)
}
}
}
func productionConfig() Config {
return Config{MemoryKiB: 64 * 1024, Iterations: 3, Parallelism: 2, SaltLength: 16, KeyLength: 32}
}