2020-03-23 07:06:44 +01:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
)
|
|
|
|
|
|
2020-03-30 09:28:00 +02:00
|
|
|
var _ HashAlgorithm = (*BCrypt)(nil)
|
2020-03-23 07:06:44 +01:00
|
|
|
|
|
|
|
|
type BCrypt struct {
|
|
|
|
|
cost int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewBCrypt(cost int) *BCrypt {
|
|
|
|
|
return &BCrypt{cost: cost}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *BCrypt) Algorithm() string {
|
|
|
|
|
return "bcrypt"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *BCrypt) Hash(value []byte) ([]byte, error) {
|
|
|
|
|
return bcrypt.GenerateFromPassword(value, b.cost)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *BCrypt) CompareHash(hashed, value []byte) error {
|
|
|
|
|
return bcrypt.CompareHashAndPassword(hashed, value)
|
|
|
|
|
}
|