Skip to content

Instantly share code, notes, and snippets.

@fsmv
Last active October 9, 2022 21:15
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fsmv/02c636d4da58106f113049ee45a62f50 to your computer and use it in GitHub Desktop.
Save fsmv/02c636d4da58106f113049ee45a62f50 to your computer and use it in GitHub Desktop.
A runnable go script that creates an http basic auth password hash compatible with .htaccess (this is just the password part, you have to add username:<hash>)
/*?sr/bin/env go run "$0" "$@"; exit $? #*/
// This is actually not a shebang, the first line is both valid shell script and valid go code
// Just run: chmod +x pass.go; ./pass.go
package main
import (
"bufio"
"crypto/sha256"
"encoding/base64"
"fmt"
"log"
"os"
"strings"
)
func main() {
fmt.Printf("Type your password (not hidden) then press enter: ")
rawPw, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
log.Fatal(err)
}
password := strings.TrimSpace(rawPw)
hash := sha256.Sum256([]byte(password))
fmt.Printf("{SHA}%v", base64.StdEncoding.EncodeToString(hash[:]))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment