implement password getter

get password via hidden user input or raw piped stdin
This commit is contained in:
akulij 2025-02-26 18:22:06 +00:00
parent 8e5fcfb1be
commit 6c40441f70

View File

@ -1,10 +1,14 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"io"
"os"
"time" "time"
"unsafe" "unsafe"
"errors"
"golang.org/x/term"
"crypto/sha256" "crypto/sha256"
"golang.org/x/crypto/curve25519" "golang.org/x/crypto/curve25519"
@ -17,12 +21,12 @@ type X25519Identity struct {
} }
func main() { func main() {
var passphrase string passbytes, err := getPasswordBytes()
if err != nil {
fmt.Printf("Failed to get password, error: %s\n", err)
}
fmt.Print("Enter password: ") sum := sha256.Sum256(passbytes)
fmt.Scanln(&passphrase)
sum := sha256.Sum256([]byte(passphrase))
fmt.Printf("Password hash: %x\n", sum) fmt.Printf("Password hash: %x\n", sum)
k, err := newX25519IdentityFromScalar(sum[:]) k, err := newX25519IdentityFromScalar(sum[:])
@ -37,6 +41,17 @@ func main() {
fmt.Printf("%s\n", k) fmt.Printf("%s\n", k)
} }
func getPasswordBytes() ([]byte, error) {
if term.IsTerminal(int(os.Stdin.Fd())) {
fmt.Print("Enter password: ")
passbytes, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
return passbytes, err
} else {
return io.ReadAll(os.Stdin)
}
}
// almost a copy of private function in age/x25519.go // almost a copy of private function in age/x25519.go
func newX25519IdentityFromScalar(secretKey []byte) (*age.X25519Identity, error) { func newX25519IdentityFromScalar(secretKey []byte) (*age.X25519Identity, error) {
if len(secretKey) != curve25519.ScalarSize { if len(secretKey) != curve25519.ScalarSize {