Compare commits

..

No commits in common. "master" and "v0.2.2" have entirely different histories.

6 changed files with 88 additions and 54 deletions

View File

@ -119,6 +119,8 @@ multisql encrypt pgpass.sec pgpass
Program zapyta o nowe hasło a następnie zaszyfruje treść pliku pgpass i zapisze go do pliku pgpass.sec. Program zapyta o nowe hasło a następnie zaszyfruje treść pliku pgpass i zapisze go do pliku pgpass.sec.
> Uwaga: plik pgpass.sec zostanie napisany bez pytania.
W konfiguracji należy ustawić plik wartość `"Passfile"` na `pgpass.sec`. W konfiguracji należy ustawić plik wartość `"Passfile"` na `pgpass.sec`.
Zaszyfrowany plik można odszyfrować i zapisać do pliku jawnego lub podejrzeć: Zaszyfrowany plik można odszyfrować i zapisać do pliku jawnego lub podejrzeć:
@ -152,9 +154,4 @@ Windows, powershell:
$env:MULTISQLPASS = "abc" $env:MULTISQLPASS = "abc"
``` ```
Użycie opcji `-P` powoduje, że zmienna środowiskowa jest ignorowana i hasło pobierane jest z klawiatury. Użycie opcji `-P` powoduje, że zmienna środowiskowa jest ignorowana.
> Uwaga: Od wersji 0.2.3 wykorzystywany jest format szyfrowania
> [age-encryption](https://age-encryption.org/).
> Plik haseł można więc szyfrować i deszyftować również narzędziem
> `age` (https://github.com/FiloSottile/age/releases/tag/v1.0.0)

View File

@ -129,8 +129,7 @@ plik zaszyfrować:
multsql encrypt pgpass.encrypted pgpass multsql encrypt pgpass.encrypted pgpass
Przy użyciu (odszyfrowaniu) pliku, hasło jest pobierane ze zmiennej Hasło jest pobierane ze zmiennej środowiskowej %s lub z klawiatury, jeśli użyto opcji -P.
środowiskowej %s lub z klawiatury, jeśli użyto opcji -P.
`, MULTISQLPASS) `, MULTISQLPASS)

View File

@ -87,7 +87,6 @@ func encryption(args []string, params cfg.Parameters) {
err = pass.EncryptFile(password, args[1], plain) err = pass.EncryptFile(password, args[1], plain)
if err != nil { if err != nil {
_ = os.Remove(args[1]) // jeśli błąd to usuń plik
log.Fatalf("Błąd szyfrowania pliku: %v", err) log.Fatalf("Błąd szyfrowania pliku: %v", err)
} }
case "decrypt": case "decrypt":

7
go.mod
View File

@ -3,11 +3,8 @@ module baal.ar76.eu/x/pub/multisql
go 1.19 go 1.19
require ( require (
filippo.io/age v1.0.0 golang.org/x/crypto v0.3.0
golang.org/x/term v0.2.0 golang.org/x/term v0.2.0
) )
require ( require golang.org/x/sys v0.2.0 // indirect
golang.org/x/crypto v0.3.0 // indirect
golang.org/x/sys v0.2.0 // indirect
)

2
go.sum
View File

@ -1,5 +1,3 @@
filippo.io/age v1.0.0 h1:V6q14n0mqYU3qKFkZ6oOaF9oXneOviS3ubXsSVBRSzc=
filippo.io/age v1.0.0/go.mod h1:PaX+Si/Sd5G8LgfCwldsSba3H1DDQZhIhFGkhbHaBq8=
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A= golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=

View File

@ -1,65 +1,109 @@
package pass package pass
import ( import (
"bytes" "crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt" "fmt"
"io"
"os" "os"
"filippo.io/age" "golang.org/x/crypto/scrypt"
"filippo.io/age/armor"
) )
func DeriveKey(password, salt []byte) ([]byte, []byte, error) {
if salt == nil {
salt = make([]byte, 32)
if _, err := rand.Read(salt); err != nil {
return nil, nil, err
}
}
key, err := scrypt.Key(password, salt, 32768, 8, 1, 32)
if err != nil {
return nil, nil, fmt.Errorf("błąd generowania klucza: %w", err)
}
return key, salt, nil
}
func Encrypt(key, data []byte) ([]byte, error) {
blockCipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(blockCipher)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = rand.Read(nonce); err != nil {
return nil, err
}
ciphertext := gcm.Seal(nonce, nonce, data, nil)
return ciphertext, nil
}
func Decrypt(key, data []byte) ([]byte, error) {
blockCipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(blockCipher)
if err != nil {
return nil, err
}
nonce, ciphertext := data[:gcm.NonceSize()], data[gcm.NonceSize():]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}
func DecryptFile(password []byte, file string) ([]byte, error) { func DecryptFile(password []byte, file string) ([]byte, error) {
identity, err := age.NewScryptIdentity(string(password)) data, err := os.ReadFile(file)
if err != nil { if err != nil {
return nil, fmt.Errorf("błąd przetwarzania hasła: %w", err) return nil, err
} }
fd, err := os.Open(file) salt := data[:32] //
data = data[32:]
key, _, err := DeriveKey(password, salt)
if err != nil { if err != nil {
return nil, fmt.Errorf("błąd otwarcia pliku: %w", err) return nil, err
} }
defer fd.Close() return Decrypt(key, data)
in := armor.NewReader(fd)
r, err := age.Decrypt(in, identity)
if err != nil {
return nil, fmt.Errorf("błąd deszyfrowania #01: %w", err)
}
out := &bytes.Buffer{}
if _, err := io.Copy(out, r); err != nil {
return nil, fmt.Errorf("błąd deszyfrowania #02: %v", err)
}
return out.Bytes(), nil
} }
func EncryptFile(password []byte, file string, data []byte) error { func EncryptFile(password []byte, file string, data []byte) error {
recipient, err := age.NewScryptRecipient(string(password)) key, salt, err := DeriveKey(password, nil)
if err != nil { if err != nil {
return fmt.Errorf("błąd przetwarzania hasła: %w", err) return err
} }
fd, err := os.OpenFile(file, os.O_WRONLY | os.O_CREATE | os.O_EXCL, 0o600) encrypted, err := Encrypt(key, data)
if err != nil { if err != nil {
return fmt.Errorf("błąd tworzenia pliku: %w", err) return err
} }
defer fd.Close() fd, err := os.Create(file)
out := armor.NewWriter(fd) // armor
defer out.Close()
w, err := age.Encrypt(out, recipient)
if err != nil { if err != nil {
return fmt.Errorf("błąd szyfrowania #01: %w", err) return err
} }
_, err = w.Write(data) _, err = fd.Write(salt)
if err != nil { if err != nil {
return fmt.Errorf("błąd szyfrowania #02: %w", err) return err
} }
err = w.Close() _, err = fd.Write(encrypted)
if err != nil { if err != nil {
return fmt.Errorf("błąd szyfrowania #03: %w", err) return err
} }
return nil return fd.Close()
} }