duanbi2003 2018-05-22 12:57
浏览 100
已采纳

2018年使用哪种bcrypt成本?

Update

Actually it seems the benchmark was incorrectly setup I have followed the resource shared by user @Luke Joshua Park and now it works.

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)
}

Output:

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

Old incorrect benchmark

I have a small set on benchmark test in golang and am curios of to what is a recommended bcrypt cost to use as of May 2018.

This is my benchrmark file:

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)
}

and this is HashPassword() func inside 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
}

The current output is:

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

It seems that for a bcrypt with cost of 13 the time it takes is 0.32 nanoseconds, and for cost 14 the time is 1281338532ns or ~1.2 seconds Which I believe is too much. What do is the best bcrypt cost to use for the current year 2018.

  • 写回答

1条回答 默认 最新

  • drwiupraq047311240 2018-05-22 14:18
    关注

    I'm not certain what's going on with Benchmark here. If you just time these, it works fine, and you can work out the right answer for you.

    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)
    }
    

    For a work factor of 10, on my MacBook Pro I get 78ms. A work factor of 11 is 154ms, and 12 is 334ms. So we're seeing roughly doubling, as expected.

    The goal is not a work factor; it's a time. You want as long as you can live with. In my experience (mostly working on client apps), 80-100ms is a nice target because compared to a network request it's undetectable to the user, while being massive in terms of brute-force attacks (so the default of 10 is ideal for my common use).

    I generally avoid running password stretching on servers if I can help it, but this scale can be a reasonable trade-off between server impact and security. Remember that attackers may use something dramatically faster than a MacBook Pro, and may use multiple machines in parallel; I pick 80-100ms because of user experience trade-offs. (I perform password stretching on the client when I can get away with it, and then apply a cheap hash like SHA-256 on the server.)

    But if you don't do this very often, or can spend more time on it, then longer is of course better, and on my MacBook Pro a work factor of 14 is about 1.2s, which I would certainly accept for some purposes.

    But there's a reason that 10 is still the default. It's not an unreasonable value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥30 数字信号处理实验报告
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要