Skip to content

Commit ac9bf07

Browse files
committed
docs: add package-level doc comments
Provide Go-style doc comments for the password hasher API, Argon2id implementation, internal PHC helpers, and comparison utility so exported symbols are self-describing. This clarifies default configuration behavior, registry wiring, and PHC encoding expectations without altering runtime behavior.
1 parent 3cb3c38 commit ac9bf07

8 files changed

Lines changed: 21 additions & 0 deletions

File tree

argon2/argon2id.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/allisson/go-pwdhash/internal/subtle"
1212
)
1313

14+
// Argon2idHasher wraps parameterized Argon2id hashing operations.
1415
type Argon2idHasher struct {
1516
Memory uint32
1617
Iterations uint32
@@ -19,6 +20,7 @@ type Argon2idHasher struct {
1920
KeyLength uint32
2021
}
2122

23+
// Default returns an Argon2idHasher configured with library defaults.
2224
func Default() *Argon2idHasher {
2325
return &Argon2idHasher{
2426
Memory: 64 * 1024,
@@ -29,10 +31,12 @@ func Default() *Argon2idHasher {
2931
}
3032
}
3133

34+
// ID reports the PHC algorithm identifier.
3235
func (a *Argon2idHasher) ID() string {
3336
return "argon2id"
3437
}
3538

39+
// Hash derives an Argon2id key and returns the PHC string.
3640
func (a *Argon2idHasher) Hash(password []byte) (string, error) {
3741
salt := make([]byte, a.SaltLength)
3842
if _, err := rand.Read(salt); err != nil {
@@ -63,6 +67,7 @@ func (a *Argon2idHasher) Hash(password []byte) (string, error) {
6367
return enc.String(), nil
6468
}
6569

70+
// Verify recomputes the Argon2id hash and compares it in constant time.
6671
func (a *Argon2idHasher) Verify(password []byte, encoded string) (bool, error) {
6772
parsed, err := encoding.Parse(encoded)
6873
if err != nil {
@@ -85,6 +90,7 @@ func (a *Argon2idHasher) Verify(password []byte, encoded string) (bool, error) {
8590
return subtle.ConstantTimeCompare(key, parsed.Hash), nil
8691
}
8792

93+
// NeedsRehash reports whether the encoded parameters diverge from the current configuration.
8894
func (a *Argon2idHasher) NeedsRehash(encoded string) (bool, error) {
8995
parsed, err := encoding.Parse(encoded)
9096
if err != nil {

errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ package pwdhash
33
import "errors"
44

55
var (
6+
// ErrInvalidHash indicates that an encoded hash cannot be parsed.
67
ErrInvalidHash = errors.New("invalid encoded hash")
78
)

hasher.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pwdhash
22

3+
// Hasher represents a password hashing algorithm implementation.
34
type Hasher interface {
45
ID() string
56
Hash(password []byte) (string, error)

internal/encoding/parse.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
)
88

9+
// Parse decodes a PHC-formatted string into an EncodedHash structure.
910
func Parse(s string) (*EncodedHash, error) {
1011
parts := strings.Split(s, "$")
1112
if len(parts) < 6 {

internal/encoding/phc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
)
88

9+
// EncodedHash captures a PHC-formatted hash and associated metadata.
910
type EncodedHash struct {
1011
Algorithm string
1112
Version int
@@ -14,6 +15,7 @@ type EncodedHash struct {
1415
Hash []byte
1516
}
1617

18+
// String renders the hash in PHC string format.
1719
func (e EncodedHash) String() string {
1820
params := []string{}
1921
for k, v := range e.Params {

internal/subtle/compare.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package subtle
22

33
import "crypto/subtle"
44

5+
// ConstantTimeCompare matches slices while avoiding timing leaks.
56
func ConstantTimeCompare(a, b []byte) bool {
67
if len(a) != len(b) {
78
return false

options.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ package pwdhash
22

33
import "github.com/allisson/go-pwdhash/argon2"
44

5+
// config holds PasswordHasher construction settings.
56
type config struct {
67
current Hasher
78
}
89

10+
// Option configures PasswordHasher construction.
911
type Option func(*config)
1012

13+
// defaultConfig initializes a config with the default Argon2id hasher.
1114
func defaultConfig() *config {
1215
return &config{
1316
current: argon2.Default(),
1417
}
1518
}
1619

20+
// WithHasher overrides the default hashing algorithm.
1721
func WithHasher(h Hasher) Option {
1822
return func(c *config) {
1923
c.current = h

password.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
"github.com/allisson/go-pwdhash/internal/encoding"
77
)
88

9+
// PasswordHasher manages password hashing operations via registered algorithms.
910
type PasswordHasher struct {
1011
current Hasher
1112
registry map[string]Hasher
1213
}
1314

15+
// New constructs a PasswordHasher configured via the provided options.
1416
func New(opts ...Option) (*PasswordHasher, error) {
1517
cfg := defaultConfig()
1618

@@ -27,10 +29,12 @@ func New(opts ...Option) (*PasswordHasher, error) {
2729
}, nil
2830
}
2931

32+
// Hash encodes the provided password using the active hasher.
3033
func (p *PasswordHasher) Hash(password []byte) (string, error) {
3134
return p.current.Hash(password)
3235
}
3336

37+
// Verify checks whether the encoded hash matches the provided password.
3438
func (p *PasswordHasher) Verify(password []byte, encoded string) (bool, error) {
3539
parsed, err := encoding.Parse(encoded)
3640
if err != nil {
@@ -45,6 +49,7 @@ func (p *PasswordHasher) Verify(password []byte, encoded string) (bool, error) {
4549
return hasher.Verify(password, encoded)
4650
}
4751

52+
// NeedsRehash reports whether the encoded hash should be regenerated.
4853
func (p *PasswordHasher) NeedsRehash(encoded string) (bool, error) {
4954
parsed, err := encoding.Parse(encoded)
5055
if err != nil {

0 commit comments

Comments
 (0)