chore: finalize lab7 security verification
CI / verify (pull_request) Successful in 14m53s

This commit is contained in:
2026-11-06 22:00:42 +03:00
parent 5ac64f51fa
commit a8edaf058d
53 changed files with 1130 additions and 224 deletions
+33 -4
View File
@@ -15,6 +15,7 @@ import (
var (
ErrInvalidConfig = errors.New("invalid password configuration")
ErrInvalidHash = errors.New("invalid password hash")
ErrBusy = errors.New("password operation limit reached")
)
type Config struct {
@@ -26,17 +27,24 @@ type Config struct {
}
type Hasher struct {
config Config
dummyHash string
config Config
dummyHash string
operations chan struct{}
}
func New(config Config) (*Hasher, error) {
func New(config Config, maxConcurrentOperations int) (*Hasher, error) {
if err := validateConfig(config); err != nil {
return nil, err
}
if maxConcurrentOperations < 1 || maxConcurrentOperations > 64 {
return nil, ErrInvalidConfig
}
salt := make([]byte, config.SaltLength)
digest := argon2.IDKey([]byte("invalid-user-password"), salt, config.Iterations, config.MemoryKiB, config.Parallelism, config.KeyLength)
return &Hasher{config: config, dummyHash: encode(config, salt, digest)}, nil
return &Hasher{
config: config, dummyHash: encode(config, salt, digest),
operations: make(chan struct{}, maxConcurrentOperations),
}, nil
}
func validateConfig(config Config) error {
@@ -51,6 +59,10 @@ func validateConfig(config Config) error {
}
func (h *Hasher) Hash(value string) (string, error) {
if !h.acquire() {
return "", ErrBusy
}
defer h.release()
salt := make([]byte, h.config.SaltLength)
if _, err := rand.Read(salt); err != nil {
return "", fmt.Errorf("generate password salt: %w", err)
@@ -60,6 +72,10 @@ func (h *Hasher) Hash(value string) (string, error) {
}
func (h *Hasher) Verify(value, encodedHash string) (bool, error) {
if !h.acquire() {
return false, ErrBusy
}
defer h.release()
config, salt, digest, err := decode(encodedHash)
if err != nil {
return false, err
@@ -68,6 +84,19 @@ func (h *Hasher) Verify(value, encodedHash string) (bool, error) {
return subtle.ConstantTimeCompare(actual, digest) == 1, nil
}
func (h *Hasher) acquire() bool {
select {
case h.operations <- struct{}{}:
return true
default:
return false
}
}
func (h *Hasher) release() {
<-h.operations
}
func (h *Hasher) NeedsRehash(encodedHash string) (bool, error) {
config, _, _, err := decode(encodedHash)
if err != nil {