aboutsummaryrefslogtreecommitdiff
path: root/pkg/database/password.go
blob: 10abe4b73c5b4b8254074d856c8f9b94da1d9520 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package database

import "golang.org/x/crypto/bcrypt"

// To allow for testing the error case (bad random is hard to trigger)
var passwordFunction = bcrypt.GenerateFromPassword

func hashPassword(password string) (string, error) {
	bytes, err := passwordFunction([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		return "", newPasswordError(err)
	}
	return string(bytes), nil
}

func checkPassword(hash string, password string) error {
	err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
	if err != nil {
		return newPasswordError(err)
	}
	return nil
}