26 lines
566 B
Go
26 lines
566 B
Go
|
package password
|
||
|
|
||
|
import "golang.org/x/crypto/bcrypt"
|
||
|
|
||
|
func GetHash(password string) ([]byte, error) {
|
||
|
// NOTE: bcrypt will not operate on passwords longer than 72 characters.
|
||
|
hash, err := bcrypt.GenerateFromPassword(
|
||
|
[]byte(password), bcrypt.DefaultCost,
|
||
|
)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return hash, nil
|
||
|
}
|
||
|
|
||
|
func Compare(oldHash []byte, password string) bool {
|
||
|
// NOTE: bcrypt will not operate on passwords longer than 72 characters.
|
||
|
err := bcrypt.CompareHashAndPassword(oldHash, []byte(password))
|
||
|
if err != nil {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
return true
|
||
|
}
|