Advertisement
Treyzania

CC base64 (try 2)

Feb 11th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. -- Base64-encoding
  2. -- Sourced from http://en.wikipedia.org/wiki/Base64
  3. --require('math')
  4.  
  5.  
  6. local __author__ = 'Daniel Lindsley'
  7. local __version__ = 'scm-1'
  8. local __license__ = 'BSD'
  9.  
  10.  
  11. local index_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  12.  
  13.  
  14. function to_binary(integer)
  15.     local remaining = tonumber(integer)
  16.     local bin_bits = ''
  17.  
  18.     for i = 7, 0, -1 do
  19.         local current_power = math.pow(2, i)
  20.  
  21.         if remaining >= current_power then
  22.             bin_bits = bin_bits .. '1'
  23.             remaining = remaining - current_power
  24.         else
  25.             bin_bits = bin_bits .. '0'
  26.         end
  27.     end
  28.  
  29.     return bin_bits
  30. end
  31.  
  32. function from_binary(bin_bits)
  33.     return tonumber(bin_bits, 2)
  34. end
  35.  
  36.  
  37. function to_base64(to_encode)
  38.     local bit_pattern = ''
  39.     local encoded = ''
  40.     local trailing = ''
  41.  
  42.     for i = 1, string.len(to_encode) do
  43.         bit_pattern = bit_pattern .. to_binary(string.byte(string.sub(to_encode, i, i)))
  44.     end
  45.  
  46.     -- Check the number of bytes. If it's not evenly divisible by three,
  47.     -- zero-pad the ending & append on the correct number of ``=``s.
  48.     if math.mod(string.len(bit_pattern), 3) == 2 then
  49.         trailing = '=='
  50.         bit_pattern = bit_pattern .. '0000000000000000'
  51.     elseif math.mod(string.len(bit_pattern), 3) == 1 then
  52.         trailing = '='
  53.         bit_pattern = bit_pattern .. '00000000'
  54.     end
  55.  
  56.     for i = 1, string.len(bit_pattern), 6 do
  57.         local byte = string.sub(bit_pattern, i, i+5)
  58.         local offset = tonumber(from_binary(byte))
  59.         encoded = encoded .. string.sub(index_table, offset+1, offset+1)
  60.     end
  61.  
  62.     return string.sub(encoded, 1, -1 - string.len(trailing)) .. trailing
  63. end
  64.  
  65.  
  66. function from_base64(to_decode)
  67.     local padded = to_decode:gsub("%s", "")
  68.     local unpadded = padded:gsub("=", "")
  69.     local bit_pattern = ''
  70.     local decoded = ''
  71.  
  72.     for i = 1, string.len(unpadded) do
  73.         local char = string.sub(to_decode, i, i)
  74.         local offset, _ = string.find(index_table, char)
  75.         if offset == nil then
  76.              error("Invalid character '" .. char .. "' found.")
  77.         end
  78.  
  79.         bit_pattern = bit_pattern .. string.sub(to_binary(offset-1), 3)
  80.     end
  81.  
  82.     for i = 1, string.len(bit_pattern), 8 do
  83.         local byte = string.sub(bit_pattern, i, i+7)
  84.         decoded = decoded .. string.char(from_binary(byte))
  85.     end
  86.  
  87.     local padding_length = padded:len()-unpadded:len()
  88.  
  89.     if (padding_length == 1 or padding_length == 2) then
  90.         decoded = decoded:sub(1,-2)
  91.     end
  92.     return decoded
  93. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement