Skip to content

Instantly share code, notes, and snippets.

@stemid
Last active September 4, 2020 08:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stemid/929ccd93bac3c5420f4d to your computer and use it in GitHub Desktop.
Save stemid/929ccd93bac3c5420f4d to your computer and use it in GitHub Desktop.
Create encrypted versions of passwords in go
// build: go build password.go
// run: ./password -password='clear text'
// use HISTCONTROL=ignoreboth and indent commands with one space to avoid
// saving passwords in your shell history.
package main
import (
"os"
"fmt"
"flag"
"golang.org/x/crypto/bcrypt"
)
func main() {
password := flag.String("password", "", "a password in clear text")
flag.Parse()
encrypted_password, err := EncryptPassword(*password)
if err != nil {
fmt.Println("error: ", err)
}
fmt.Println("clear: ", *password)
fmt.Println("crypt: ", encrypted_password)
os.Exit(0)
}
func EncryptPassword(password string) (string, error) {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), 10)
if err != nil {
return "", err
}
return string(hashedPassword), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment