package main
import (
"log"
"time"
"golang.org/x/crypto/bcrypt"
)
func main() {
password := "secret"
for cost := 10; cost < 20; cost++ {
start := time.Now()
hashPwd(password, cost)
log.Println("cost =", cost, " 耗时:", time.Since(start))
}
}
// 加密
func hashPwd(password string, cost int) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), cost)
return string(hash), err
}
结果如下:
2023/04/18 16:55:22 cost = 10 耗时: 73.259042ms
2023/04/18 16:55:23 cost = 11 耗时: 144.186666ms
2023/04/18 16:55:23 cost = 12 耗时: 296.26375ms
2023/04/18 16:55:23 cost = 13 耗时: 609.700625ms
2023/04/18 16:55:25 cost = 14 耗时: 1.207150375s
2023/04/18 16:55:27 cost = 15 耗时: 2.39822525s
2023/04/18 16:55:32 cost = 16 耗时: 4.847635708s
2023/04/18 16:55:42 cost = 17 耗时: 9.725984375s
2023/04/18 16:56:01 cost = 18 耗时: 19.40903275s
2023/04/18 16:56:40 cost = 19 耗时: 38.541072833s