feat: Poprawiona obsługa hasła z klawiatury
This commit is contained in:
@ -5,10 +5,8 @@ import (
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"baal.ar76.eu/x/pub/multisql/cfg"
|
||||
"golang.org/x/crypto/scrypt"
|
||||
)
|
||||
|
||||
@ -70,22 +68,22 @@ func Decrypt(key, data []byte) ([]byte, error) {
|
||||
return plaintext, nil
|
||||
}
|
||||
|
||||
func DecryptFile(password, file string) ([]byte, error) {
|
||||
func DecryptFile(password []byte, file string) ([]byte, error) {
|
||||
data, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
salt := data[:32] //
|
||||
data = data[32:]
|
||||
key, _, err := DeriveKey([]byte(password), salt)
|
||||
key, _, err := DeriveKey(password, salt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return Decrypt(key, data)
|
||||
}
|
||||
|
||||
func EncryptFile(password, file string, data []byte) error {
|
||||
key, salt, err := DeriveKey([]byte(password), nil)
|
||||
func EncryptFile(password []byte, file string, data []byte) error {
|
||||
key, salt, err := DeriveKey(password, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -108,17 +106,4 @@ func EncryptFile(password, file string, data []byte) error {
|
||||
return fd.Close()
|
||||
}
|
||||
|
||||
func GetMasterPass(askPass bool) string {
|
||||
password := os.Getenv(cfg.MULTISQLPASS)
|
||||
if password == "" {
|
||||
if !askPass {
|
||||
log.Fatalf("Nieustalone hasło. Użyj flagi -P lub ustaw hasło w zmiennej %s", cfg.MULTISQLPASS)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "Podaj hasło: ")
|
||||
n, e := fmt.Scanln(&password)
|
||||
if n == 0 || e != nil {
|
||||
log.Fatalln("Nie udało się wczytać hasła z konsoli")
|
||||
}
|
||||
}
|
||||
return password
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@ type PassDb struct {
|
||||
cache map[string]*pgpassrow
|
||||
}
|
||||
|
||||
func Load(file string, master string) (*PassDb, error) {
|
||||
func Load(file string, master []byte) (*PassDb, error) {
|
||||
|
||||
data, err := DecryptFile(master, file)
|
||||
if err != nil {
|
||||
|
55
pass/term.go
Normal file
55
pass/term.go
Normal file
@ -0,0 +1,55 @@
|
||||
package pass
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"baal.ar76.eu/x/pub/multisql/cfg"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
func GetMasterPass(askPass bool) []byte {
|
||||
if askPass {
|
||||
res, err := termGetPassword("Podaj hasło")
|
||||
if err != nil {
|
||||
log.Fatalln("Nie udało się wczytać hasła z konsoli")
|
||||
}
|
||||
return res
|
||||
}
|
||||
password := os.Getenv(cfg.MULTISQLPASS)
|
||||
|
||||
if password != "" {
|
||||
return []byte(password)
|
||||
}
|
||||
|
||||
log.Fatalf("Nieustalone hasło. Użyj flagi -P lub ustaw hasło w zmiennej %s", cfg.MULTISQLPASS)
|
||||
return nil
|
||||
}
|
||||
|
||||
func EnterMasterPass() []byte {
|
||||
p1, err := termGetPassword("Podaj nowe hasło (min 8 znaków)")
|
||||
if err != nil {
|
||||
log.Fatalf("Błąd wczytywania hasła: %v", err)
|
||||
}
|
||||
if len(p1) < 8 {
|
||||
log.Fatalf("podano zbyt krótkie hasło")
|
||||
}
|
||||
p2, err := termGetPassword("Powtórz nowe hasło")
|
||||
if err != nil {
|
||||
log.Fatalf("Błąd wczytywania hasła: %v", err)
|
||||
}
|
||||
if !bytes.Equal(p1, p2) {
|
||||
log.Fatalf("podane hasła są różne")
|
||||
}
|
||||
|
||||
return p1
|
||||
}
|
||||
|
||||
func termGetPassword(prompt string) ([]byte, error) {
|
||||
fmt.Fprintf(os.Stderr, "%s: ", prompt)
|
||||
passwd, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
fmt.Fprintln(os.Stderr)
|
||||
return passwd, err
|
||||
}
|
Reference in New Issue
Block a user