3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-23 04:19:25 +01:00
ergo/irc/password.go

46 lines
1.1 KiB
Go
Raw Normal View History

// Copyright (c) 2012-2014 Jeremy Latt
// released under the MIT license
package irc
import (
"encoding/base64"
"errors"
"golang.org/x/crypto/bcrypt"
)
var (
2016-10-13 09:36:44 +02:00
// ErrEmptyPassword means that an empty password was given.
ErrEmptyPassword = errors.New("empty password")
)
2016-10-13 09:36:44 +02:00
// GenerateEncodedPassword returns an encrypted password, encoded into a string with base64.
func GenerateEncodedPassword(passwd string) (encoded string, err error) {
if passwd == "" {
2016-10-13 09:36:44 +02:00
err = ErrEmptyPassword
return
}
bcrypted, err := bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.MinCost)
if err != nil {
return
}
encoded = base64.StdEncoding.EncodeToString(bcrypted)
return
}
2016-10-13 09:36:44 +02:00
// DecodePasswordHash takes a base64-encoded password hash and returns the appropriate bytes.
func DecodePasswordHash(encoded string) (decoded []byte, err error) {
if encoded == "" {
2016-10-13 09:36:44 +02:00
err = ErrEmptyPassword
return
}
decoded, err = base64.StdEncoding.DecodeString(encoded)
return
}
2016-10-13 09:36:44 +02:00
// ComparePassword compares a given password with the given hash.
func ComparePassword(hash, password []byte) error {
return bcrypt.CompareHashAndPassword(hash, password)
}