-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from mtlynch/hash-password
Check passwords based on hashes rather than plaintext
- Loading branch information
Showing
6 changed files
with
156 additions
and
17 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
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,43 @@ | ||
package auth | ||
|
||
import ( | ||
"crypto/sha256" | ||
"crypto/subtle" | ||
"errors" | ||
|
||
"golang.org/x/crypto/pbkdf2" | ||
) | ||
|
||
var ErrPasswordTooShort = errors.New("password must be non-empty") | ||
|
||
type HashedPassword struct { | ||
hash []byte | ||
} | ||
|
||
func (hp HashedPassword) Bytes() []byte { | ||
return hp.hash | ||
} | ||
|
||
func (hp HashedPassword) Equals(other HashedPassword) bool { | ||
return subtle.ConstantTimeCompare(hp.hash, other.hash) != 0 | ||
} | ||
|
||
func HashPassword(password string) (HashedPassword, error) { | ||
if len(password) == 0 { | ||
return HashedPassword{}, ErrPasswordTooShort | ||
} | ||
|
||
// These bytes are chosen at random. It's insecure to use a static salt to | ||
// hash a set of passwords, but since we're only ever hashing a single | ||
// password, using a static salt is fine. The salt prevents an attacker from | ||
// using a rainbow table to retrieve the plaintext password from the hashed | ||
// version, and that's all that's necessary for fusion's needs. | ||
staticSalt := []byte{36, 129, 1, 54} | ||
iter := 100 | ||
keyLen := 32 | ||
hash := pbkdf2.Key([]byte(password), staticSalt, iter, keyLen, sha256.New) | ||
|
||
return HashedPassword{ | ||
hash: hash, | ||
}, nil | ||
} |
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,71 @@ | ||
package auth_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/0x2e/fusion/auth" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestHashPassword(t *testing.T) { | ||
for _, tt := range []struct { | ||
explanation string | ||
input string | ||
wantErr error | ||
}{ | ||
{ | ||
explanation: "valid password succeeds", | ||
input: "mypassword", | ||
wantErr: nil, | ||
}, | ||
{ | ||
explanation: "empty password returns ErrPasswordTooShort", | ||
input: "", | ||
wantErr: auth.ErrPasswordTooShort, | ||
}, | ||
} { | ||
t.Run(tt.explanation, func(t *testing.T) { | ||
got, err := auth.HashPassword(tt.input) | ||
require.Equal(t, tt.wantErr, err) | ||
if tt.wantErr == nil { | ||
assert.NotEmpty(t, got.Bytes()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestHashedPasswordEquals(t *testing.T) { | ||
for _, tt := range []struct { | ||
explanation string | ||
hashedPassword1 auth.HashedPassword | ||
hashedPassword2 auth.HashedPassword | ||
want bool | ||
}{ | ||
{ | ||
explanation: "same passwords match", | ||
hashedPassword1: mustHashPassword("password1"), | ||
hashedPassword2: mustHashPassword("password1"), | ||
want: true, | ||
}, | ||
{ | ||
explanation: "different passwords don't match", | ||
hashedPassword1: mustHashPassword("password1"), | ||
hashedPassword2: mustHashPassword("password2"), | ||
want: false, | ||
}, | ||
} { | ||
t.Run(tt.explanation, func(t *testing.T) { | ||
assert.Equal(t, tt.want, tt.hashedPassword1.Equals(tt.hashedPassword2)) | ||
}) | ||
} | ||
} | ||
|
||
func mustHashPassword(password string) auth.HashedPassword { | ||
hashedPassword, err := auth.HashPassword(password) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return hashedPassword | ||
} |
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