Skip to content

Commit

Permalink
bcrypt: update BCrypt to adhere to new Hasher interface
Browse files Browse the repository at this point in the history
Signed-off-by: Amir Aslaminejad <aslaminejad@gmail.com>
  • Loading branch information
aaslamin authored and aeneasr committed Sep 22, 2018
1 parent 02f19fa commit 938e50a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 4 additions & 2 deletions hash_bcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
package fosite

import (
"context"

"github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
)
Expand All @@ -31,15 +33,15 @@ type BCrypt struct {
WorkFactor int
}

func (b *BCrypt) Hash(data []byte) ([]byte, error) {
func (b *BCrypt) Hash(ctx context.Context, data []byte) ([]byte, error) {
s, err := bcrypt.GenerateFromPassword(data, b.WorkFactor)
if err != nil {
return nil, errors.WithStack(err)
}
return s, nil
}

func (b *BCrypt) Compare(hash, data []byte) error {
func (b *BCrypt) Compare(ctx context.Context, hash, data []byte) error {
if err := bcrypt.CompareHashAndPassword(hash, data); err != nil {
return errors.WithStack(err)
}
Expand Down
12 changes: 7 additions & 5 deletions hash_bcrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ package fosite
import (
"testing"

"context"

"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
)
Expand All @@ -33,7 +35,7 @@ func TestHash(t *testing.T) {
WorkFactor: 10,
}
password := []byte("foo")
hash, err := h.Hash(password)
hash, err := h.Hash(context.TODO(), password)
assert.NoError(t, err)
assert.NotNil(t, hash)
assert.NotEqual(t, hash, password)
Expand All @@ -44,10 +46,10 @@ func TestCompareEquals(t *testing.T) {
WorkFactor: 10,
}
password := []byte("foo")
hash, err := h.Hash(password)
hash, err := h.Hash(context.TODO(), password)
assert.NoError(t, err)
assert.NotNil(t, hash)
err = h.Compare(hash, password)
err = h.Compare(context.TODO(), hash, password)
assert.NoError(t, err)
}

Expand All @@ -56,9 +58,9 @@ func TestCompareDifferent(t *testing.T) {
WorkFactor: 10,
}
password := []byte("foo")
hash, err := h.Hash(password)
hash, err := h.Hash(context.TODO(), password)
assert.NoError(t, err)
assert.NotNil(t, hash)
err = h.Compare(hash, []byte(uuid.NewRandom()))
err = h.Compare(context.TODO(), hash, []byte(uuid.NewRandom()))
assert.Error(t, err)
}

0 comments on commit 938e50a

Please sign in to comment.