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.

107 lines
1.8 KiB

  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "syscall"
  7. "golang.org/x/crypto/ssh/terminal"
  8. "fenc/Encryption"
  9. )
  10. func getPassword() (string, error) {
  11. var (
  12. bytePassword []byte
  13. e error
  14. )
  15. fmt.Print("Enter Password: ")
  16. bytePassword, e = terminal.ReadPassword(int(syscall.Stdin))
  17. if e != nil {
  18. return "", e
  19. }
  20. return string(bytePassword), nil
  21. }
  22. func main() {
  23. var (
  24. files []string
  25. decrypt *bool
  26. read *bool
  27. edit *bool
  28. plaintext string
  29. password string
  30. e error
  31. )
  32. flag.Usage = func() {
  33. fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [OPTIONS]... [FILES]...\n", os.Args[0])
  34. fmt.Println("\t-d: Decrypt files")
  35. fmt.Println("\t-r: Decrypt and read files to stdout")
  36. fmt.Println("\t-e: Edit encrypted file")
  37. }
  38. decrypt = flag.Bool("d", false, "Decrypt the file(s)")
  39. read = flag.Bool("r", false, "Read file to stdout")
  40. edit = flag.Bool("e", false, "Edit encrypted file")
  41. flag.Parse()
  42. files = flag.Args()
  43. if len(files) == 0 {
  44. flag.Usage()
  45. fmt.Println("Files cannot be null")
  46. os.Exit(1)
  47. }
  48. password, e = getPassword()
  49. if e != nil {
  50. fmt.Println(e.Error())
  51. os.Exit(1)
  52. }
  53. if *decrypt {
  54. for _, file := range files {
  55. e = Encryption.DecryptFile(password, file)
  56. if e != nil {
  57. fmt.Println(e.Error())
  58. os.Exit(1)
  59. }
  60. }
  61. os.Exit(0)
  62. }
  63. if *read {
  64. for _, file := range files {
  65. plaintext, e = Encryption.DecryptAndReadFile(password, file)
  66. if e != nil {
  67. fmt.Println(e.Error())
  68. os.Exit(1)
  69. }
  70. fmt.Println(file)
  71. fmt.Println(plaintext)
  72. }
  73. os.Exit(0)
  74. }
  75. if *edit {
  76. for _, file := range files {
  77. e = Encryption.EditEncryptedFile(password, file)
  78. if e != nil {
  79. fmt.Println(e.Error())
  80. os.Exit(1)
  81. }
  82. }
  83. os.Exit(0)
  84. }
  85. for _, file := range files {
  86. e = Encryption.EncryptFile(password, file)
  87. if e != nil {
  88. fmt.Println(e.Error())
  89. os.Exit(1)
  90. }
  91. }
  92. }