added password hashing and testing for the functions

This commit is contained in:
George Shaw 2017-10-26 16:17:27 -05:00
parent b7571e2d57
commit d758dbb28c
2 changed files with 57 additions and 0 deletions

View file

@ -0,0 +1,19 @@
// Encryption package has functions inside of it that utilize various encypting and hashing techniques
package encryption
import "golang.org/x/crypto/bcrypt"
func HashPassword(password string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(hash), err
}
func CheckPassword(hash, plainText []byte) (bool, error) {
err := bcrypt.CompareHashAndPassword(hash, plainText)
if err != nil {
return false, err
}
return true, nil
}

View file

@ -0,0 +1,38 @@
// Encryption package has functions inside of it that utilize various encypting and hashing techniques
package encryption
import "testing"
func TestPasswordHashing(t *testing.T) {
passwords := []struct {
ok bool
plainText string
hash string
}{
{true, "fd#@$s4$oiahfoij", ""},
{false, "ashf324yiuf!@#", ""},
{true, "4892fjsk#@(!!)", ""},
{false, "fsufh$&*(#(*f))", ""},
}
for _, password := range passwords {
var err error
if password.ok {
password.hash, err = HashPassword(password.plainText)
} else {
password.hash, err = HashPassword("this will fail")
}
if err != nil {
t.Errorf("Error in password_test using HashPassword func: %s", err.Error())
}
ok, err := CheckPassword([]byte(password.hash), []byte(password.plainText))
if err != nil {
if ok != password.ok {
t.Errorf("In password_test expected %t, but got %t from CheckPassword func", password.ok, ok)
}
}
}
}