54.152.38.154 |    

Navigation

Google Advertisement

Gelegentlich benötigt man unter Unix base64 kodierte Zeichenfolgen (wie im RFC2045 beschrieben) oder man möchte bereits base64 kodierte Zeichenfolgen wieder dekodieren. Das Mittel zum Zweck ist wie so oft Perl, insbesondere das Perlmodul MIME::Base64. Das Modul ermöglicht es Strings oder Dateien von und nach base64 zu kodieren bzw. dekodieren.

base64.sh
  1. #!/bin/bash
  2. # *****************************************************************
  3. # file: base64.sh
  4. # date: 2007-11-01 13:00
  5. # author: Marko Schulz - <info@tuxnet24.de>
  6. # description: convert string whith base64.
  7. # *****************************************************************
  8. #
  9. # 
  10. # SYNOPSIS
  11. #
  12. # This program action encode a logo.png (binary) to STDOUT (in this case to logo64.txt)
  13. # tux@earth:~$ ./base64.sh -a encode -f ./logo.png > logo64.txt
  14. #
  15. # This program action decode a logo64.txt (ASCII) to STDOUT (in this case to logo.png)
  16. # tux@earth:~$ ./base64.sh -a decode -f ./logo64.txt > logo.png
  17. #
  18. #
  19. # *****************************************************************
  20. # This function display error and usage and end this program...
  21.  
  22. function f_alert() {
  23.  
  24. local error=$1
  25. echo -e "\n$( basename $0): convert string whith base64\n"
  26. [ -n "$error" ] && echo -e "\a${error}\n"
  27. echo -e "Usage: $( basename $0 ) -a {encode|decode} -f </PATH/TO/FILE> [ -h ]\n"
  28. echo -e "\t-a the action for ecode or decode"
  29. echo -e "\t-f path to fie name"
  30. echo -e "\t-h display this screen\n"
  31. exit 1
  32.  
  33. }
  34.  
  35. # *****************************************************************
  36. # get the cmd arguments...
  37.  
  38. while getopts f:a:h Optionen 2>/dev/null; do
  39.     case $Optionen in
  40.         f) pFile=$OPTARG  ;;
  41.         a) pAction=$OPTARG ;;
  42.         h) f_alert ;;
  43.         *) f_alert "ERROR: Invalid argument" ;;
  44.     esac
  45. done
  46.  
  47. # *****************************************************************
  48. # program action...
  49.  
  50. # error if no file defined...
  51. [ ! -f "$pFile" ] && f_alert "ERROR: No input file defined"
  52.  
  53. case "$pAction" in
  54. 	encode)
  55. 		perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' < $pFile
  56. 	;;
  57. 	decode)
  58. 		perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)' < $pFile
  59. 	;;
  60. 	*)
  61. 		f_alert "ERROR: Invalid argument for action"
  62. 	;;
  63. esac
  64.  
  65. # *****************************************************************
  66. # EOF
Parsed in 0.006 seconds at 319.94 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 1 users online.

Tags Cloud

Latest snippets
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)
STDERR
(Bash::Function)
radius
(CSS)
shadow
(CSS)
rotate
(CSS)
create_function
(JavaScript::Snippets)