Guest User

Untitled

a guest
Jan 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. bold=$(tput bold)
  4. red=$(tput setaf 1)
  5. yellow=$(tput setaf 3)
  6. nocolor=$(tput sgr0)
  7.  
  8. bold_red=${bold}${red}
  9. bold_yellow=${bold}${yellow}
  10.  
  11. print_usage_and_exit()
  12. {
  13. echo "Usage: $0 [-o output_directory] filename_to_decrypt"
  14. exit 1
  15. }
  16.  
  17. print_error_and_exit()
  18. {
  19. echo "${bold_red}$1 Exit code = $2.${nocolor}" 1>&2
  20. exit "$2"
  21. }
  22.  
  23. while getopts ":o:" option
  24. do
  25. case "${option}" in
  26. o)
  27. given_output_directory=${OPTARG}
  28. ;;
  29. h | *)
  30. print_usage_and_exit
  31. ;;
  32. esac
  33. done
  34.  
  35. shift $((OPTIND - 1))
  36.  
  37. [ "$#" -eq 0 ] &&
  38. print_usage_and_exit
  39.  
  40. [ "$#" -gt 1 ] &&
  41. print_error_and_exit "Multiple files are not supported." 2
  42.  
  43. [ ! -f "$1" ] &&
  44. print_error_and_exit "The given argument is not an existing file." 3
  45.  
  46. input_filename="$1"
  47.  
  48. [ ! -r "$input_filename" ] &&
  49. print_error_and_exit "Input file is not readable by you." 4
  50.  
  51. input_filepath=$(dirname "$input_filename")
  52.  
  53. if [ -z ${given_output_directory} ]
  54. then
  55. output_directory="$input_filepath"
  56. else
  57. output_directory="$given_output_directory"
  58. fi
  59.  
  60. [ ! -w "$output_directory" ] &&
  61. print_error_and_exit "Destination directory is not writable by you." 5
  62.  
  63. filename_extracted_from_path=$(basename "$input_filename")
  64. filename_without_enc_extension="${filename_extracted_from_path%.enc}"
  65.  
  66. if [ "$filename_extracted_from_path" = "$filename_without_enc_extension" ]
  67. then
  68. # the file has a different than .enc extension or no extension at all
  69. # what we do now, is that we append .dec extention to the file name
  70. output_filename="$output_directory/$filename_extracted_from_path".dec
  71. else
  72. # the file has the .enc extension
  73. # what we do now, is that we use the file name without .enc extension
  74. output_filename="$output_directory/$filename_without_enc_extension"
  75. fi
  76.  
  77. [ -f "$output_filename" ] &&
  78. print_error_and_exit "Destination file exists." 6
  79.  
  80. if ! pv -W "$input_filename" | openssl enc -aes-256-cbc -md sha256 -salt -out "$output_filename" -d 2> /dev/null
  81. then
  82. [ -f "$output_filename" ] && rm "$output_filename"
  83.  
  84. print_error_and_exit "Decryption failed." 7
  85. else
  86. echo "${bold_yellow}Decryption successful.${nocolor}"
  87.  
  88. exit 0
  89. fi
Add Comment
Please, Sign In to add comment