cryptdecode.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Package cryptdecode provides the cryptdecode command.
  2. package cryptdecode
  3. import (
  4. "errors"
  5. "fmt"
  6. "github.com/rclone/rclone/backend/crypt"
  7. "github.com/rclone/rclone/cmd"
  8. "github.com/rclone/rclone/fs"
  9. "github.com/rclone/rclone/fs/config/flags"
  10. "github.com/spf13/cobra"
  11. )
  12. // Options set by command line flags
  13. var (
  14. Reverse = false
  15. )
  16. func init() {
  17. cmd.Root.AddCommand(commandDefinition)
  18. cmdFlags := commandDefinition.Flags()
  19. flags.BoolVarP(cmdFlags, &Reverse, "reverse", "", Reverse, "Reverse cryptdecode, encrypts filenames", "")
  20. }
  21. var commandDefinition = &cobra.Command{
  22. Use: "cryptdecode encryptedremote: encryptedfilename",
  23. Short: `Cryptdecode returns unencrypted file names.`,
  24. Long: `Returns unencrypted file names when provided with a list of encrypted file
  25. names. List limit is 10 items.
  26. If you supply the ` + "`--reverse`" + ` flag, it will return encrypted file names.
  27. use it like this
  28. rclone cryptdecode encryptedremote: encryptedfilename1 encryptedfilename2
  29. rclone cryptdecode --reverse encryptedremote: filename1 filename2
  30. Another way to accomplish this is by using the ` + "`rclone backend encode` (or `decode`)" + ` command.
  31. See the documentation on the [crypt](/crypt/) overlay for more info.
  32. `,
  33. Annotations: map[string]string{
  34. "versionIntroduced": "v1.38",
  35. },
  36. Run: func(command *cobra.Command, args []string) {
  37. cmd.CheckArgs(2, 11, command, args)
  38. cmd.Run(false, false, command, func() error {
  39. fsInfo, _, _, config, err := fs.ConfigFs(args[0])
  40. if err != nil {
  41. return err
  42. }
  43. if fsInfo.Name != "crypt" {
  44. return errors.New("the remote needs to be of type \"crypt\"")
  45. }
  46. cipher, err := crypt.NewCipher(config)
  47. if err != nil {
  48. return err
  49. }
  50. if Reverse {
  51. return cryptEncode(cipher, args[1:])
  52. }
  53. return cryptDecode(cipher, args[1:])
  54. })
  55. },
  56. }
  57. // cryptDecode returns the unencrypted file name
  58. func cryptDecode(cipher *crypt.Cipher, args []string) error {
  59. output := ""
  60. for _, encryptedFileName := range args {
  61. fileName, err := cipher.DecryptFileName(encryptedFileName)
  62. if err != nil {
  63. output += fmt.Sprintln(encryptedFileName, "\t", "Failed to decrypt")
  64. } else {
  65. output += fmt.Sprintln(encryptedFileName, "\t", fileName)
  66. }
  67. }
  68. fmt.Print(output)
  69. return nil
  70. }
  71. // cryptEncode returns the encrypted file name
  72. func cryptEncode(cipher *crypt.Cipher, args []string) error {
  73. output := ""
  74. for _, fileName := range args {
  75. encryptedFileName := cipher.EncryptFileName(fileName)
  76. output += fmt.Sprintln(fileName, "\t", encryptedFileName)
  77. }
  78. fmt.Print(output)
  79. return nil
  80. }