Skip to content

Commit

Permalink
fix: allow lower bcrypt values and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Nov 17, 2020
1 parent 90dfaf5 commit 812a21c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .schema/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@
"type": "integer",
"description": "Sets the BCrypt cost. The higher the value, the more CPU time is being used to generate hashes.",
"default": 10,
"minimum": 8,
"minimum": 4,
"maximum": 31
}
}
Expand Down
37 changes: 37 additions & 0 deletions x/hasher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package x

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

type hasherConfig struct {
cost int
}

func (c hasherConfig) BCryptCost() int {
return c.cost
}

func TestHasher(t *testing.T) {
for _, cost := range []int{1, 8, 10, 16} {
result, err := NewBCrypt(&hasherConfig{cost: cost}).Hash(context.Background(), []byte("foobar"))
require.NoError(t, err)
require.NotEmpty(t, result)
}
}

func BenchmarkHasher(b *testing.B) {
for cost := 1; cost <= 16; cost++ {
b.Run(fmt.Sprintf("cost=%d", cost), func(b *testing.B) {
for n := 0; n < b.N; n++ {
result, err := NewBCrypt(&hasherConfig{cost: cost}).Hash(context.Background(), []byte("foobar"))
require.NoError(b, err)
require.NotEmpty(b, result)
}
})
}
}

0 comments on commit 812a21c

Please sign in to comment.