Skip to content

Commit

Permalink
raise bcrypt cost factor lower bound to x/crypto/bcrypt default
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Shannon <[email protected]>
  • Loading branch information
adamdecaf committed Oct 25, 2018
1 parent 211b43b commit 3388bed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions hash_bcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type BCrypt struct {
}

func (b *BCrypt) Hash(ctx context.Context, data []byte) ([]byte, error) {
if b.WorkFactor == 0 {
b.WorkFactor = bcrypt.DefaultCost
}
s, err := bcrypt.GenerateFromPassword(data, b.WorkFactor)
if err != nil {
return nil, errors.WithStack(err)
Expand Down
15 changes: 15 additions & 0 deletions hash_bcrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/crypto/bcrypt"
)

func TestCompare(t *testing.T) {
Expand Down Expand Up @@ -110,3 +111,17 @@ func TestHash(t *testing.T) {
})
}
}

func TestDefaultWorkFactor(t *testing.T) {
b := &BCrypt{}
data := []byte("secrets")
hash, err := b.Hash(context.TODO(), data)
if err != nil {
t.Fatal(err)
}

cost, err := bcrypt.Cost(hash)
if cost != bcrypt.DefaultCost {
t.Errorf("got cost factor %d", cost)
}
}

0 comments on commit 3388bed

Please sign in to comment.