Skip to content

Instantly share code, notes, and snippets.

@apg
Last active August 5, 2016 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apg/3dfe5af54a927593ad64fb815585ff8d to your computer and use it in GitHub Desktop.
Save apg/3dfe5af54a927593ad64fb815585ff8d to your computer and use it in GitHub Desktop.
Simple tool to get a bcrypt hash with a given cost. `go run bcrypt.go -password foobar -cost 20`
package main
import (
"flag"
"fmt"
"os"
"golang.org/x/crypto/bcrypt"
)
var password = flag.String("password", "", "password to hash")
var hash = flag.String("hash", "", "hash to compare to")
var cost = flag.Int("cost", 10, "password cost")
func main() {
flag.Parse()
if *hash == "" {
h, e := bcrypt.GenerateFromPassword([]byte(*password), *cost)
if e != nil {
fmt.Println("ERR", e)
os.Exit(1)
}
fmt.Println("hash: ", string(h))
} else {
e := bcrypt.CompareHashAndPassword([]byte(*hash), []byte(*password))
if e != nil {
fmt.Println("ERR", e)
os.Exit(1)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment