Serie: Go
go
34 líneas
· Actualizado 2026-04-18
password.go
Go/Baasid/internal/auth/password.go
// Package auth contains password hashing and JWT utilities used by the
// authentication layer. Keeping these helpers separate from HTTP handlers
// makes them trivial to unit-test without spinning up Echo.
package auth
import (
"fmt"
"golang.org/x/crypto/bcrypt"
)
// bcryptCost controls how CPU-intensive hashing is. 10 is the library default
// and a good balance for interactive login on modern hardware.
const bcryptCost = 10
// HashPassword returns a bcrypt hash suitable for storing in system_user.password.
func HashPassword(plain string) (string, error) {
if plain == "" {
return "", fmt.Errorf("empty password")
}
b, err := bcrypt.GenerateFromPassword([]byte(plain), bcryptCost)
if err != nil {
return "", fmt.Errorf("bcrypt: %w", err)
}
return string(b), nil
}
// CheckPassword compares plaintext with a bcrypt hash and returns true on match.
func CheckPassword(hash, plain string) bool {
if hash == "" || plain == "" {
return false
}
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(plain)) == nil
}
Artículos relacionados
Go
go
Actualizado 2026-04-18
main.go
main.go — go source code from the Go learning materials (Go/Baasid/cmd/hashpw/main.go).
Leer artículo →
Go
go
Actualizado 2026-04-18
main.go
main.go — go source code from the Go learning materials (Go/Baasid/cmd/server/main.go).
Leer artículo →
Go
go
Actualizado 2026-04-18
docs.go
docs.go — go source code from the Go learning materials (Go/Baasid/docs/docs.go).
Leer artículo →
Go
go
Actualizado 2026-04-18
jwt.go
jwt.go — go source code from the Go learning materials (Go/Baasid/internal/auth/jwt.go).
Leer artículo →
Go
go
Actualizado 2026-04-18
password_test.go
password_test.go — go source code from the Go learning materials (Go/Baasid/internal/auth/password_test.go).
Leer artículo →
Go
go
Actualizado 2026-04-18
config.go
config.go — go source code from the Go learning materials (Go/Baasid/internal/config/config.go).
Leer artículo →