Com*_*ode 3 encryption hash go bcrypt
实际上,似乎基准测试设置不正确,我遵循了用户@Luke Joshua Park 共享的资源,现在可以使用了。
package main
import "testing"
func benchmarkBcrypt(i int, b *testing.B){
for n:= 0; n < b.N; n++ {
HashPassword("my pass", i)
}
}
func BenchmarkBcrypt9(b *testing.B){
benchmarkBcrypt(9, b)
}
func BenchmarkBcrypt10(b *testing.B){
benchmarkBcrypt(10, b)
}
func BenchmarkBcrypt11(b *testing.B){
benchmarkBcrypt(11, b)
}
func BenchmarkBcrypt12(b *testing.B){
benchmarkBcrypt(12, b)
}
func BenchmarkBcrypt13(b *testing.B){
benchmarkBcrypt(13, b)
}
func BenchmarkBcrypt14(b *testing.B){
benchmarkBcrypt(14, b)
}
Run Code Online (Sandbox Code Playgroud)
输出:
BenchmarkBcrypt9-4 30 39543095 ns/op
BenchmarkBcrypt10-4 20 79184657 ns/op
BenchmarkBcrypt11-4 10 158688315 ns/op
BenchmarkBcrypt12-4 5 316070133 ns/op
BenchmarkBcrypt13-4 2 631838101 ns/op
BenchmarkBcrypt14-4 1 1275047344 ns/op
PASS
ok go-playground 10.670s
Run Code Online (Sandbox Code Playgroud)
我在golang上进行了基准测试的一小部分,并对截至2018年5月使用的建议bcrypt成本有什么好奇。
这是我的基准文件:
package main
import "testing"
func BenchmarkBcrypt10(b *testing.B){
HashPassword("my pass", 10)
}
func BenchmarkBcrypt12(b *testing.B){
HashPassword("my pass", 12)
}
func BenchmarkBcrypt13(b *testing.B){
HashPassword("my pass", 13)
}
func BenchmarkBcrypt14(b *testing.B){
HashPassword("my pass", 14)
}
func BenchmarkBcrypt15(b *testing.B){
HashPassword("my pass", 15)
}
Run Code Online (Sandbox Code Playgroud)
这是HashPassword()里面的func main.go:
import (
"golang.org/x/crypto/bcrypt"
)
func HashPassword(password string, cost int) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), cost)
return string(bytes), err
}
Run Code Online (Sandbox Code Playgroud)
当前输出为:
go test -bench=.
BenchmarkBcrypt10-4 2000000000 0.04 ns/op
BenchmarkBcrypt12-4 2000000000 0.16 ns/op
BenchmarkBcrypt13-4 2000000000 0.32 ns/op
BenchmarkBcrypt14-4 1 1281338532 ns/op
BenchmarkBcrypt15-4 1 2558998327 ns/op
PASS
Run Code Online (Sandbox Code Playgroud)
对于成本为13的bcrypt而言,其花费的时间似乎为0.32纳秒,而成本为14的bcrypt则为1281338532ns或〜1.2秒,我认为这太多了。什么是在当前2018年使用的最佳bcrypt成本。
我不确定这里的Benchmark发生了什么。如果您只是安排这些时间,它会很好地工作,并且可以为您找到正确的答案。
package main
import (
"golang.org/x/crypto/bcrypt"
"time"
)
func main() {
cost := 10
start := time.Now()
bcrypt.GenerateFromPassword([]byte("password"), cost)
end := time.Now()
print(end.Sub(start) / time.Millisecond)
}
Run Code Online (Sandbox Code Playgroud)
对于工作因数10,在我的MacBook Pro上,我得到78毫秒。11的工作因数是154ms,12的工作因数是334ms。因此,正如预期的那样,我们看到的数量大约翻了一番。
目标不是工作因素;这是时间。只要您能忍受,您就想要。根据我的经验(主要是在客户端应用上工作),80-100ms是一个不错的目标,因为与网络请求相比,它对于用户来说是不可检测的,而对于暴力攻击而言则是巨大的(因此默认值为10对我来说是理想的选择。常用)。
如果可以帮助的话,我通常会避免在服务器上运行密码扩展,但是这种规模可以合理地权衡服务器影响力和安全性。请记住,攻击者使用某些东西的速度可能比MacBook Pro快得多,并且可能同时使用多台计算机。由于用户体验的权衡,我选择80-100ms。(当我可以使用时,我在客户端上执行密码扩展,然后在服务器上应用便宜的哈希(例如SHA-256)。)
但是,如果您不经常这样做,或者可以花更多的时间,那么更长的时间当然更好,并且在我的MacBook Pro上,工作系数为14约为1.2s,我当然会出于某些目的接受它。
但是有一个原因仍然是默认值10。这不是一个不合理的价值。
| 归档时间: |
|
| 查看次数: |
6259 次 |
| 最近记录: |