Skip to content

Instantly share code, notes, and snippets.

@emidoots
Created January 7, 2020 20:08
Show Gist options
  • Save emidoots/92a26de448fd7f0a6f4db61b225a1c76 to your computer and use it in GitHub Desktop.
Save emidoots/92a26de448fd7f0a6f4db61b225a1c76 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
"golang.org/x/crypto/bcrypt"
)
func main() {
pass := generatePassword(1 * 1024)
start := time.Now()
testPasswordLength(pass)
fmt.Println("1 KiB password took", time.Since(start))
fmt.Println("")
pass = generatePassword(1 * 1024 * 1024)
start = time.Now()
testPasswordLength(pass)
fmt.Println("1 MiB password took", time.Since(start))
fmt.Println("")
pass = generatePassword(100 * 1024 * 1024)
start = time.Now()
testPasswordLength(pass)
fmt.Println("100 MiB password took", time.Since(start))
fmt.Println("")
pass = generatePassword(1 * 1024 * 1024 * 1024)
start = time.Now()
testPasswordLength(pass)
fmt.Println("1 GiB password took", time.Since(start))
fmt.Println("")
pass = generatePassword(8 * 1024 * 1024 * 1024)
start = time.Now()
testPasswordLength(pass)
fmt.Println("8 GiB password took", time.Since(start))
fmt.Println("")
}
func testPasswordLength(pass []byte) {
t0 := time.Now()
hp, err := bcrypt.GenerateFromPassword(pass, 0)
if err != nil {
panic(fmt.Sprintf("GenerateFromPassword error: %s", err))
}
fmt.Println("\tGenerateFromPassword:", time.Since(t0))
fmt.Println("\thashed password length:", len(hp))
t1 := time.Now()
if bcrypt.CompareHashAndPassword(hp, pass) != nil {
panic(fmt.Sprintf("%v should hash %s correctly", hp, pass))
}
fmt.Println("\tCompareHashAndPassword:", time.Since(t1))
}
func generatePassword(length int) []byte {
var password []byte
for i := 0; i < length; i++ {
password = append(password, byte(i))
}
return password
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment