3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 11:59:40 +01:00
ergo/irc/password.go
Robin Burchell 6ad97f7e5d irc: Switch to golang.org versions, not google code versions of dependencies.
This has a few advantages:
* the google code versions are no longer maintained
* the google code versions are using mercurial (which I don't have installed)
2015-07-04 12:19:33 +02:00

38 lines
750 B
Go

package irc
import (
"golang.org/x/crypto/bcrypt"
"encoding/base64"
"errors"
)
var (
EmptyPasswordError = errors.New("empty password")
)
func GenerateEncodedPassword(passwd string) (encoded string, err error) {
if passwd == "" {
err = EmptyPasswordError
return
}
bcrypted, err := bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.MinCost)
if err != nil {
return
}
encoded = base64.StdEncoding.EncodeToString(bcrypted)
return
}
func DecodePassword(encoded string) (decoded []byte, err error) {
if encoded == "" {
err = EmptyPasswordError
return
}
decoded, err = base64.StdEncoding.DecodeString(encoded)
return
}
func ComparePassword(hash, password []byte) error {
return bcrypt.CompareHashAndPassword(hash, password)
}