package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"syscall"
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
"gitlab.com/tovijaeschke/FileEncryption/Encryption"
|
|
)
|
|
|
|
func getPassword() (string, error) {
|
|
var (
|
|
bytePassword []byte
|
|
e error
|
|
)
|
|
fmt.Print("Enter Password: ")
|
|
bytePassword, e = terminal.ReadPassword(int(syscall.Stdin))
|
|
if e != nil {
|
|
return "", e
|
|
}
|
|
return string(bytePassword), nil
|
|
}
|
|
|
|
func main() {
|
|
var (
|
|
files []string
|
|
decrypt *bool
|
|
read *bool
|
|
edit *bool
|
|
|
|
plaintext string
|
|
password string
|
|
e error
|
|
)
|
|
|
|
flag.Usage = func() {
|
|
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [OPTIONS]... [FILES]...\n", os.Args[0])
|
|
fmt.Println("\t-d: Decrypt files")
|
|
fmt.Println("\t-r: Decrypt and read files to stdout")
|
|
fmt.Println("\t-e: Edit encrypted file")
|
|
}
|
|
|
|
decrypt = flag.Bool("d", false, "Decrypt the file(s)")
|
|
read = flag.Bool("r", false, "Read file to stdout")
|
|
edit = flag.Bool("e", false, "Edit encrypted file")
|
|
flag.Parse()
|
|
files = flag.Args()
|
|
|
|
if len(files) == 0 {
|
|
flag.Usage()
|
|
fmt.Println("Files cannot be null")
|
|
os.Exit(1)
|
|
}
|
|
|
|
password, e = getPassword()
|
|
if e != nil {
|
|
fmt.Println(e.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
if *decrypt {
|
|
for _, file := range files {
|
|
e = Encryption.DecryptFile(password, file)
|
|
if e != nil {
|
|
fmt.Println(e.Error())
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
os.Exit(0)
|
|
}
|
|
|
|
if *read {
|
|
for _, file := range files {
|
|
plaintext, e = Encryption.DecryptAndReadFile(password, file)
|
|
if e != nil {
|
|
fmt.Println(e.Error())
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println(file)
|
|
fmt.Println(plaintext)
|
|
}
|
|
os.Exit(0)
|
|
}
|
|
|
|
if *edit {
|
|
for _, file := range files {
|
|
e = Encryption.EditEncryptedFile(password, file)
|
|
if e != nil {
|
|
fmt.Println(e.Error())
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
os.Exit(0)
|
|
}
|
|
|
|
|
|
for _, file := range files {
|
|
e = Encryption.EncryptFile(password, file)
|
|
if e != nil {
|
|
fmt.Println(e.Error())
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
}
|