package htpasswd

import "golang.org/x/crypto/bcrypt"

// Bcrypt facilitates bcrypt style hashing
type Bcrypt struct{}

// Hash returns the hashed variant of the password or an error
func (*Bcrypt) Hash(password string) (string, error) {
	passwordBytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		return "", err
	}
	return string(passwordBytes), nil
}

// Match verifier the hashed password using the original
func (*Bcrypt) Match(password, hashedPassword string) error {
	return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
}

// Name returns the name of the hasher
func (*Bcrypt) Name() string { return "bcrypt" }

// Prefix returns the hasher's prefix
func (*Bcrypt) Prefix() string { return "$2a$" }