Last active
September 4, 2020 08:26
-
-
Save stemid/929ccd93bac3c5420f4d to your computer and use it in GitHub Desktop.
Create encrypted versions of passwords in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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