gob/secret/bcrypt.go

30 lines
690 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package secret
import (
"golang.org/x/crypto/bcrypt"
)
// Bcrypt permet de calculer un hash en utilisant lalgorithme Bcrypt.
type Bcrypt struct {
cost int
}
// Bcrypt initialise une fonction de hashage de type Bcrypt et de coût cost.
func NewBcrypt(cost int) Bcrypt {
return Bcrypt{cost: cost}
}
// Hash retourne le hash des bytes dentrée.
func (h Bcrypt) Hash(data []byte) []byte {
hashed, _ := bcrypt.GenerateFromPassword(data, h.cost)
return hashed
}
// Compare vérifie quune donnée dentrée a bien pour hash la valeur hashed.
func (h Bcrypt) Compare(data []byte, hashed []byte) bool {
err := bcrypt.CompareHashAndPassword(hashed, data)
return err == nil
}