-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: basic and support custom password compare
- Loading branch information
Showing
3 changed files
with
98 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package basic | ||
|
||
import ( | ||
"crypto" | ||
"crypto/subtle" | ||
"encoding/hex" | ||
) | ||
|
||
// Comparator is the interface implemented by types, | ||
// that can generate password hash and compares the hashed password | ||
// with its possible plaintext equivalent | ||
type Comparator interface { | ||
Hash(password string) (string, error) | ||
Verify(hashedPassword, password string) error | ||
} | ||
|
||
type basicHashing struct { | ||
h crypto.Hash | ||
} | ||
|
||
func (b basicHashing) Hash(password string) (string, error) { | ||
hasher := b.h.New() | ||
_, _ = hasher.Write([]byte(password)) | ||
sum := hasher.Sum(nil) | ||
return hex.EncodeToString(sum), nil | ||
} | ||
|
||
func (b basicHashing) Verify(hashedPassword, password string) error { | ||
hash, err := b.Hash(password) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if subtle.ConstantTimeCompare([]byte(hash), []byte(hashedPassword)) == 1 { | ||
return nil | ||
} | ||
|
||
return ErrInvalidCredentials | ||
} | ||
|
||
type plainText struct{} | ||
|
||
func (p plainText) Hash(password string) (string, error) { | ||
return password, nil | ||
} | ||
|
||
func (p plainText) Verify(hashedPassword, password string) error { | ||
if subtle.ConstantTimeCompare([]byte(hashedPassword), []byte(password)) == 1 { | ||
return nil | ||
} | ||
return ErrInvalidCredentials | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package basic | ||
|
||
import ( | ||
"crypto" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBasicHashing(t *testing.T) { | ||
b := basicHashing{h: crypto.SHA256} | ||
pass := "password" | ||
hash, err := b.Hash(pass) | ||
match := b.Verify(hash, pass) | ||
missmatch := b.Verify(hash, "pass") | ||
|
||
assert.NoError(t, err) | ||
assert.NotEqual(t, hash, pass) | ||
assert.NoError(t, match) | ||
assert.Equal(t, ErrInvalidCredentials, missmatch) | ||
} | ||
|
||
func TestPlainText(t *testing.T) { | ||
p := plainText{} | ||
pass := "password" | ||
hash, err := p.Hash(pass) | ||
match := p.Verify(hash, pass) | ||
missmatch := p.Verify(hash, "pass") | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, hash, pass) | ||
assert.NoError(t, match) | ||
assert.Equal(t, ErrInvalidCredentials, missmatch) | ||
} |