| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # base64.sh: Bash implementation of Base64 encoding and decoding. |
| 3 | # |
| 4 | # Copyright (c) 2011 vladz <[email protected]> |
| 5 | # Used in ABSG with permission (thanks!). |
| 6 | # |
| 7 | # Encode or decode original Base64 (and also Base64url) |
| 8 | #+ from STDIN to STDOUT. |
| 9 | # |
| 10 | # Usage: |
| 11 | # |
| 12 | # Encode |
| 13 | # $ ./base64.sh < binary-file > binary-file.base64 |
| 14 | # Decode |
| 15 | # $ ./base64.sh -d < binary-file.base64 > binary-file |
| 16 | # |
| 17 | # Reference: |
| 18 | # |
| 19 | # [1] RFC4648 - "The Base16, Base32, and Base64 Data Encodings" |
| 20 | # http://tools.ietf.org/html/rfc4648#section-5 |
| 21 | |
| 22 | |
| 23 | # The base64_charset[] array contains entire base64 charset, |
| 24 | # and additionally the character "=" ... |
| 25 | base64_charset=( {A..Z} {a..z} {0..9} + / = ) |
| 26 | # Nice illustration of brace expansion. |
| 27 | |
| 28 | # Uncomment the ### line below to use base64url encoding instead of |
| 29 | #+ original base64. |
| 30 | ### base64_charset=( {A..Z} {a..z} {0..9} - _ = ) |
| 31 | |
| 32 | # Output text width when encoding |
| 33 | #+ (64 characters, just like openssl output). |
| 34 | text_width=64 |
| 35 | |
| 36 | function display_base64_charscip-shell shell abs-guide 10-4 display_base64_char(). { |
| 37 | # Convert a 6-bit number (between 0 and 63) into its corresponding values |
| 38 | #+ in Base64, then display the result with the specified text width. |
| 39 | printfscip-shell system . . printf. "${base64_charset[$1]}"; (( width++ )) |
| 40 | (( width % text_width == 0 )) && printfscip-shell system . . printf. "\n" |
| 41 | } |
| 42 | |
| 43 | function encode_base64scip-shell shell abs-guide 10-4 encode_base64(). { |
| 44 | # Encode three 8-bit hexadecimal codes into four 6-bit numbers. |
| 45 | # We need two local int array variables: |
| 46 | # c8[]: to store the codes of the 8-bit characters to encode |
| 47 | # c6[]: to store the corresponding encoded values on 6-bit |
| 48 | declare -a -i c8 c6 |
| 49 | |
| 50 | # Convert hexadecimal to decimal. |
| 51 | c8=( $(printscip-shell system . . printf.f "ibase=16; ${1:0:2}\n${1:2:2}\n${1:4:2}\n" | bc) ) |
| 52 | |
| 53 | # Let's play with bitwise operators |
| 54 | #+ (3x8-bit into 4x6-bits conversion). |
| 55 | (( c6[0] = c8[0] >> 2 )) |
| 56 | (( c6[1] = ((c8[0] & 3) << 4) | (c8[1] >> 4) )) |
| 57 | |
| 58 | # The following operations depend on the c8 element number. |
| 59 | case ${#c8[*]} in |
| 60 | 3) (( c6[2] = ((c8[1] & 15) << 2) | (c8[2] >> 6) )) |
| 61 | (( c6[3] = c8[2] & 63 )) ;; |
| 62 | 2) (( c6[2] = (c8[1] & 15) << 2 )) |
| 63 | (( c6[3] = 64 )) ;; |
| 64 | 1) (( c6[2] = c6[3] = 64 )) ;; |
| 65 | esac |
| 66 | |
| 67 | for char in ${c6[@]}; do |
| 68 | display_base64_charscip-shell shell abs-guide 10-4 display_base64_char(). ${char} |
| 69 | done |
| 70 | } |
| 71 | |
| 72 | function decode_base64scip-shell shell abs-guide 10-4 decode_base64(). { |
| 73 | # Decode four base64 characters into three hexadecimal ASCII characters. |
| 74 | # c8[]: to store the codes of the 8-bit characters |
| 75 | # c6[]: to store the corresponding Base64 values on 6-bit |
| 76 | declare -a -i c8 c6 |
| 77 | |
| 78 | # Find decimal value corresponding to the current base64 character. |
| 79 | for current_char in ${1:0:1} ${1:1:1} ${1:2:1} ${1:3:1}; do |
| 80 | [scip-shell system . . `[`. "${current_char}" = "=" ] && break |
| 81 | |
| 82 | position=0 |
| 83 | while [scip-shell system . . `[`. "${current_char}" != "${base64_charset[${position}]}" ]; do |
| 84 | (( position++ )) |
| 85 | done |
| 86 | |
| 87 | c6=( ${c6[*]} ${position} ) |
| 88 | done |
| 89 | |
| 90 | # Let's play with bitwise operators |
| 91 | #+ (4x8-bit into 3x6-bits conversion). |
| 92 | (( c8[0] = (c6[0] << 2) | (c6[1] >> 4) )) |
| 93 | |
| 94 | # The next operations depends on the c6 elements number. |
| 95 | case ${#c6[*]} in |
| 96 | 3) (( c8[1] = ( (c6[1] & 15) << 4) | (c6[2] >> 2) )) |
| 97 | (( c8[2] = (c6[2] & 3) << 6 )); unset c8[2] ;; |
| 98 | 4) (( c8[1] = ( (c6[1] & 15) << 4) | (c6[2] >> 2) )) |
| 99 | (( c8[2] = ( (c6[2] & 3) << 6) | c6[3] )) ;; |
| 100 | esac |
| 101 | |
| 102 | for char in ${c8[*]}; do |
| 103 | printfscip-shell system . . printf. "\x$(printfscip-shell system . . printf. "%x" ${char})" |
| 104 | done |
| 105 | } |
| 106 | |
| 107 | |
| 108 | # main () |
| 109 | |
| 110 | if [scip-shell system . . `[`. "$1" = "-d" ]; then # decode |
| 111 | |
| 112 | # Reformat STDIN in pseudo 4x6-bit groups. |
| 113 | content=$(catscip-shell system . . cat. - | trscip-shell system . . tr. -d "\n" | sedscip-shell system . . sed. -r "s/(.{4})/\1 /g") |
| 114 | |
| 115 | for chars in ${content}; do decode_base64scip-shell shell abs-guide 10-4 decode_base64(). ${chars}; done |
| 116 | |
| 117 | else |
| 118 | # Make a hexdump of stdin and reformat in 3-byte groups. |
| 119 | content=$(catscip-shell system . . cat. - | xxd -ps -u | sedscip-shell system . . sed. -r "s/(\w{6})/\1 /g" | |
| 120 | trscip-shell system . . tr. -d "\n") |
| 121 | |
| 122 | for chars in ${content}; do encode_base64scip-shell shell abs-guide 10-4 encode_base64(). ${chars}; done |
| 123 | |
| 124 | echoscip-shell system . . echo. |
| 125 | |
| 126 | fi |