Add checkPass func and it's tests
This commit is contained in:
parent
7b8f753832
commit
6384678d16
|
@ -0,0 +1,14 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// checkPass takes a string and hash as input and returns an error. If
|
||||
// the error is not nil then the consider the password wrong. We're
|
||||
// returning error instead of a bool so that we can print failed
|
||||
// logins to log and logging shouldn't happen here.
|
||||
func checkPass(password, hash string) error {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||
return err
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestCheckPass tests the checkPass function.
|
||||
func TestCheckPass(t *testing.T) {
|
||||
var passhash = make(map[string]string)
|
||||
passhash["password"] = "$2a$10$hyV9vtsYXX88wz1rmA1x0.tcdkyvd6QsmV6gLOcR5wtYBE2GaSqT."
|
||||
|
||||
for p, h := range passhash {
|
||||
err := checkPass(p, h)
|
||||
if err != nil {
|
||||
t.Errorf("password: %s, hash: %s didn't match.",
|
||||
p, h)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue