You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.2 KiB

6 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "os/exec"
  8. "strings"
  9. "github.com/fatih/color"
  10. )
  11. func main() {
  12. if len(os.Args) <= 1 {
  13. color.Red("not enough arguments")
  14. os.Exit(0)
  15. }
  16. filename := os.Args[1]
  17. _, err := readFile(filename)
  18. var out []byte
  19. if err == nil {
  20. //file exists
  21. //open the current existing file, decrypts it, and save it to tempfile
  22. //decrypt file
  23. out, err = exec.Command("bash", "-c", "openssl aes-256-cbc -d -a -in "+filename+" -out tempfile").Output()
  24. if err != nil {
  25. cmd := exec.Command("bash", "-c", "rm tempfile")
  26. cmd.Run()
  27. log.Fatal(err)
  28. }
  29. fmt.Println(out)
  30. }
  31. //open or create a tempfile to edit
  32. cmd := exec.Command("bash", "-c", "vim tempfile")
  33. cmd.Stdout = os.Stdout
  34. cmd.Stdin = os.Stdin
  35. cmd.Stderr = os.Stderr
  36. cmd.Run()
  37. if !strings.Contains(filename, ".encvim") {
  38. filename = filename + ".encvim"
  39. }
  40. //encrypt the file
  41. out, err = exec.Command("bash", "-c", "openssl aes-256-cbc -a -salt -in tempfile -out "+filename).Output()
  42. if err != nil {
  43. log.Fatal(err)
  44. }
  45. fmt.Println(out)
  46. //delete tempfile
  47. cmd = exec.Command("bash", "-c", "rm tempfile")
  48. cmd.Run()
  49. fmt.Println(filename)
  50. }
  51. func readFile(path string) (string, error) {
  52. b, err := ioutil.ReadFile(path)
  53. return string(b), err
  54. }