b64converter.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/python
  2. #
  3. # b64converter:
  4. # Utility to convert to and from base64 encodings as a plaintext file.
  5. # This program may be used either combined with a graphical utility or
  6. # standalone as a CLI utility.
  7. #
  8. # Copyright (C) 2015 Vitor S <https://notabug.org/kzimmermann>
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, either version 3 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. import os
  24. import sys
  25. import base64 as b64
  26. def encode(infilename):
  27. '''
  28. Takes infile and encodes it into a plaintext base64 file, adding '.txt'
  29. to its original filename in the process
  30. '''
  31. with file(infilename, 'rb') as infile:
  32. with file(infilename + '.txt', 'w') as outfile:
  33. b64.encode(infile, outfile)
  34. print "Encoded %s to %s" % (infilename, infilename + '.txt')
  35. def decode(infilename):
  36. '''
  37. Takes base64-encoded infile and decodes it to its original format. It will
  38. attempt to remove any '.txt' from the filename.
  39. '''
  40. decoded_filename = infilename.split(".txt")[0]
  41. with file(infilename, 'r') as infile:
  42. with file(decoded_filename, 'wb') as outfile:
  43. b64.decode(infile, outfile)
  44. print "Decoded %s to %s" % (infilename, infilename.split('.txt')[0])
  45. # Auxilliary functions declared only if we're running in CLI mode:
  46. if __name__ == "__main__":
  47. def shuffle(args):
  48. "Takes sys.argv and processes it to encode/decode function input"
  49. args.reverse()
  50. args.pop()
  51. args.pop()
  52. args.reverse()
  53. def getopts(args):
  54. "Returns a string containing the decision from parsing options"
  55. if "-h" in args:
  56. return "help"
  57. elif "-e" in args and "-d" in args:
  58. return "error"
  59. elif "-e" in args:
  60. return "encode"
  61. elif "-d" in args:
  62. return "decode"
  63. else:
  64. return "error"
  65. def helper():
  66. print "%s - a base64 decoder/encoder of files" % sys.argv[0]
  67. print "USAGE: %s [-e|-d|-h] FILE1 FILE2 ... FILEN" % sys.argv[0]
  68. print "Where:"
  69. print " -e: encodes FILES to base64 .txt files"
  70. print " -d: decodes base64 .txt FILES to their original encoding"
  71. print " -h: displays this help message"
  72. command = getopts(sys.argv)
  73. progname = sys.argv[0] # required. Read on for why...
  74. if command == "error":
  75. print "Error: invalid inputs"
  76. helper()
  77. sys.exit(1)
  78. if command == "help":
  79. helper()
  80. sys.exit(0)
  81. elif command == "encode":
  82. shuffle(sys.argv)
  83. for item in sys.argv:
  84. try:
  85. encode(item)
  86. except IOError:
  87. print "File not found: '%s'" % item
  88. sys.exit(1)
  89. if len(sys.argv) == 1:
  90. print "1 file encoded."
  91. else:
  92. print "%s files encoded." % len(sys.argv)
  93. sys.exit(0)
  94. elif command == "decode":
  95. shuffle(sys.argv)
  96. for item in sys.argv:
  97. # If you don't check for this thing, you may overwrite this very
  98. # program! And if you don't have a backup, it can be *very*
  99. # annoying.
  100. try:
  101. decode(item)
  102. except IOError:
  103. print "File not found: '%s'" % item
  104. sys.exit(1)
  105. if len(sys.argv) == 1:
  106. print "1 file decoded."
  107. else:
  108. print "%s files decoded." % len(sys.argv)
  109. print "You may want to use the UNIX `file' command to find the format"
  110. sys.exit(0)
  111. else:
  112. print "Unknown error received. Exiting..."