This project is a simple wrapper for the golang.org/x/crypto/bcrypt library. It was developed exclusively for personal study purposes, focusing on abstracting hashing logic and implementing interfaces in Go.
The package provides a standardized interface for generating and comparing hashes, making it easier to replace implementations or mock behaviors in unit tests.
- Generate bcrypt hashes from byte slices.
- Validate passwords against existing hashes.
- Custom error handling.
- Automatic validation of the algorithm's cost.
The package exposes the following interface:
type Hasher interface {
Generate(password []byte) ([]byte, error)
Compare(hash, password []byte) error
}To create a new instance with the default cost:
h := hasher.New(hasher.DefaultCost)password := []byte("my_secure_password")
hash, err := h.Generate(password)
if err != nil {
// handle error
}err := h.Compare(hash, []byte("my_secure_password"))
if err != nil {
// if the error is "hasher: invalid password", the password is incorrect
}The wrapper exposes bcrypt cost constants for easier configuration:
MinCost: 4MaxCost: 31DefaultCost: 10
As per the Go bcrypt implementation, the maximum password length is 72 bytes. If a larger password is sent to the Generate method, the bcrypt.ErrPasswordTooLong error will be returned.
Even though this is a personal study project, contributions are highly appreciated! Feel free to submit Pull Requests at any time to improve features, refactor code, or add new capabilities.