Add checkPass func and it's tests

This commit is contained in:
Andinus 2020-03-27 01:01:07 +05:30
parent 7b8f753832
commit 6384678d16
Signed by: andinus
GPG Key ID: B67D55D482A799FD
2 changed files with 33 additions and 0 deletions

14
auth/checkpass.go Normal file
View File

@ -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
}

19
auth/checkpass_test.go Normal file
View File

@ -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)
}
}
}