From d892cad72c1eb4ae20c1b7f1c5b9451650454c28 Mon Sep 17 00:00:00 2001 From: Ryan Mehri Date: Mon, 11 May 2020 20:41:16 -0600 Subject: Add password check on post hash --- backend/hashing/hash.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'backend/hashing/hash.go') diff --git a/backend/hashing/hash.go b/backend/hashing/hash.go index 93a9cf9..d8e699a 100644 --- a/backend/hashing/hash.go +++ b/backend/hashing/hash.go @@ -29,4 +29,17 @@ func hashString(text string) string { func HashPassword(password string) (string, error) { hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) return string(hashedPassword), err +} + +func ComparePasswords(dbPassword, gotPassword string) bool { + dbPassBytes := []byte(dbPassword) + gotPassBytes := []byte(gotPassword) + compErr := bcrypt.CompareHashAndPassword(dbPassBytes, gotPassBytes) + + // if comparison error, the given password is not valid + if compErr != nil { + return false + } else { + return true + } } \ No newline at end of file -- cgit v1.2.3 From 5f493fbdabad58412fba14b4c9534709d701192e Mon Sep 17 00:00:00 2001 From: Ryan Mehri Date: Mon, 11 May 2020 20:55:49 -0600 Subject: Rename good --- backend/hashing/hash.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'backend/hashing/hash.go') diff --git a/backend/hashing/hash.go b/backend/hashing/hash.go index d8e699a..d4b2566 100644 --- a/backend/hashing/hash.go +++ b/backend/hashing/hash.go @@ -31,15 +31,15 @@ func HashPassword(password string) (string, error) { return string(hashedPassword), err } -func ComparePasswords(dbPassword, gotPassword string) bool { +func ComparePasswords(dbPassword, parsedPassword string) bool { dbPassBytes := []byte(dbPassword) - gotPassBytes := []byte(gotPassword) - compErr := bcrypt.CompareHashAndPassword(dbPassBytes, gotPassBytes) + parsedPassBytes := []byte(parsedPassword) + compErr := bcrypt.CompareHashAndPassword(dbPassBytes, parsedPassBytes) // if comparison error, the given password is not valid - if compErr != nil { - return false - } else { + if compErr == nil { return true + } else { + return false } } \ No newline at end of file -- cgit v1.2.3 From 53f55ab8b0eceea32e58880c09785dd35943af0b Mon Sep 17 00:00:00 2001 From: Ryan Mehri Date: Mon, 11 May 2020 21:07:23 -0600 Subject: Simplify hashing comparison --- backend/hashing/hash.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'backend/hashing/hash.go') diff --git a/backend/hashing/hash.go b/backend/hashing/hash.go index d4b2566..e944fbe 100644 --- a/backend/hashing/hash.go +++ b/backend/hashing/hash.go @@ -31,15 +31,11 @@ func HashPassword(password string) (string, error) { return string(hashedPassword), err } -func ComparePasswords(dbPassword, parsedPassword string) bool { +func PasswordsEqual(dbPassword, parsedPassword string) bool { dbPassBytes := []byte(dbPassword) parsedPassBytes := []byte(parsedPassword) compErr := bcrypt.CompareHashAndPassword(dbPassBytes, parsedPassBytes) // if comparison error, the given password is not valid - if compErr == nil { - return true - } else { - return false - } + return compErr == nil } \ No newline at end of file -- cgit v1.2.3