Commit a7bb5811 authored by Mayuri Shinde's avatar Mayuri Shinde
Browse files

CheckPasswordHash method added

method added to match hashed pwd with original
parent 598dd68b
2 merge requests!54Devbranch,!49CheckPasswordHash method added
Showing with 24 additions and 3 deletions
......@@ -183,7 +183,7 @@ func CreateSecurityKey(keyLength int) (string, error) {
return "", errormdl.Wrap("length should be less than 256 bytes (2048 bits)")
}
// SaltPassword Salt using bcrypt creates saltedString of given string
// SaltPassword Salt using bcrypt creates saltedString of given string,it generates new saltedString each time for the same input
func SaltPassword(password string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
if errormdl.CheckErr(err) != nil {
......@@ -192,3 +192,9 @@ func SaltPassword(password string) (string, error) {
}
return string(hash), nil
}
// CheckPasswordHash - compares hashed password with its possible plaintext equivalent. Returns true on match, or an false on mismatch.
func CheckPasswordHash(hashedPassword, password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
return err == nil
}
......@@ -183,13 +183,28 @@ func BenchmarkCreateSecurityKey(b *testing.B) {
}
func TestSaltPassword(t *testing.T) {
errormdl.IsTestingNegetiveCaseOnCheckInt2 = true
saltedPwd, _ := SaltPassword("P@ssw0rd")
assert.NotZero(t, len(saltedPwd), "Should give len")
errormdl.IsTestingNegetiveCaseOnCheckInt2 = false
}
func TestSaltPasswordError(t *testing.T) {
errormdl.IsTestingNegetiveCaseOn = true
_, err := SaltPassword("P@ssw0rd")
errormdl.IsTestingNegetiveCaseOn = false
assert.Error(t, err, "This should return error")
}
func BenchmarkSaltPassword(b *testing.B) {
for i := 0; i < b.N; i++ {
SaltPassword("P@ssw0rd")
}
}
func TestCheckPasswordHash(t *testing.T) {
match := CheckPasswordHash("$2a$04$hers/Xb2u00e8wg4e.S7Cu7JbUm4TTR4ED3wU7HTNuuwNGJxOqMZu", "P@ssw0rd")
assert.True(t, match)
}
func BenchmarkCheckPasswordHash(b *testing.B) {
for i := 0; i < b.N; i++ {
CheckPasswordHash("$2a$04$hers/Xb2u00e8wg4e.S7Cu7JbUm4TTR4ED3wU7HTNuuwNGJxOqMZu", "P@ssw0rd")
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment