deluan
9/9/2016 - 12:33 PM

Testing bcrypt in GoLang

Testing bcrypt in GoLang

///usr/bin/env go run "$0" "$@"; exit;

package main

import (
	"fmt"
	"os"

	"golang.org/x/crypto/bcrypt"
)

func main() {
	if len(os.Args) == 1 {
		println("Usage: crypto.go <word to be encrypted>")
		os.Exit(1)
	}
	p := []byte(os.Args[1])
	h, err := bcrypt.GenerateFromPassword(p, 0)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(string(h))

	if err := bcrypt.CompareHashAndPassword(h, p); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}