mirror of
https://github.com/donl/gPanel.git
synced 2026-05-27 22:07:12 -06:00
added password hashing and testing for the functions
This commit is contained in:
parent
b7571e2d57
commit
d758dbb28c
2 changed files with 57 additions and 0 deletions
19
pkg/encryption/password.go
Normal file
19
pkg/encryption/password.go
Normal 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
|
||||
}
|
||||
38
pkg/encryption/password_test.go
Normal file
38
pkg/encryption/password_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue