From 6384678d168f517fddf09135059793dc7281531f Mon Sep 17 00:00:00 2001 From: Andinus Date: Fri, 27 Mar 2020 01:01:07 +0530 Subject: Add checkPass func and it's tests --- auth/checkpass.go | 14 ++++++++++++++ auth/checkpass_test.go | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 auth/checkpass.go create mode 100644 auth/checkpass_test.go diff --git a/auth/checkpass.go b/auth/checkpass.go new file mode 100644 index 0000000..64e3c9f --- /dev/null +++ b/auth/checkpass.go @@ -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 +} diff --git a/auth/checkpass_test.go b/auth/checkpass_test.go new file mode 100644 index 0000000..d22bf48 --- /dev/null +++ b/auth/checkpass_test.go @@ -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) + } + } +} -- cgit 1.4.1-2-gfad0