Advertisement
Tatantyler

Base64

Apr 28th, 2013
1,333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.88 KB | None | 0 0
  1. -- Base64 Encoder / Decoder
  2. -- By KillaVanilla
  3.  
  4. local alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  5.  
  6. local function sixBitToBase64(input)
  7.     return string.sub(alphabet, input+1, input+1)
  8. end
  9.  
  10. local function base64ToSixBit(input)
  11.     for i=1, 64 do
  12.         if input == string.sub(alphabet, i, i) then
  13.             return i-1
  14.         end
  15.     end
  16. end
  17.  
  18. local function octetToBase64(o1, o2, o3)
  19.     local i1 = sixBitToBase64(bit.brshift(bit.band(o1, 0xFC), 2))
  20.     local i2 = "A"
  21.     local i3 = "="
  22.     local i4 = "="
  23.     if o2 then
  24.         i2 = sixBitToBase64(bit.bor( bit.blshift(bit.band(o1, 3), 4), bit.brshift(bit.band(o2, 0xF0), 4) ))
  25.         if not o3 then
  26.             i3 = sixBitToBase64(bit.blshift(bit.band(o2, 0x0F), 2))
  27.         else
  28.             i3 = sixBitToBase64(bit.bor( bit.blshift(bit.band(o2, 0x0F), 2), bit.brshift(bit.band(o3, 0xC0), 6) ))
  29.         end
  30.     else
  31.         i2 = sixBitToBase64(bit.blshift(bit.band(o1, 3), 4))
  32.     end
  33.     if o3 then
  34.         i4 = sixBitToBase64(bit.band(o3, 0x3F))
  35.     end
  36.    
  37.     return i1..i2..i3..i4
  38. end
  39.  
  40. -- octet 1 needs characters 1/2
  41. -- octet 2 needs characters 2/3
  42. -- octet 3 needs characters 3/4
  43.  
  44. local function base64ToThreeOctet(s1)
  45.     local c1 = base64ToSixBit(string.sub(s1, 1, 1))
  46.     local c2 = base64ToSixBit(string.sub(s1, 2, 2))
  47.     local c3 = 0
  48.     local c4 = 0
  49.     local o1 = 0
  50.     local o2 = 0
  51.     local o3 = 0
  52.     if string.sub(s1, 3, 3) == "=" then
  53.         c3 = nil
  54.         c4 = nil
  55.     elseif string.sub(s1, 4, 4) == "=" then
  56.         c3 = base64ToSixBit(string.sub(s1, 3, 3))
  57.         c4 = nil
  58.     else
  59.         c3 = base64ToSixBit(string.sub(s1, 3, 3))
  60.         c4 = base64ToSixBit(string.sub(s1, 4, 4))
  61.     end
  62.     o1 = bit.bor( bit.blshift(c1, 2), bit.brshift(bit.band( c2, 0x30 ), 4) )
  63.     if c3 then
  64.         o2 = bit.bor( bit.blshift(bit.band(c2, 0x0F), 4), bit.brshift(bit.band( c3, 0x3C ), 2) )
  65.     else
  66.         o2 = nil
  67.     end
  68.     if c4 then
  69.         o3 = bit.bor( bit.blshift(bit.band(c3, 3), 6), c4 )
  70.     else
  71.         o3 = nil
  72.     end
  73.     return o1, o2, o3
  74. end
  75.  
  76. local function splitIntoBlocks(bytes)
  77.     local blockNum = 1
  78.     local blocks = {}
  79.     for i=1, #bytes, 3 do
  80.         blocks[blockNum] = {bytes[i], bytes[i+1], bytes[i+2]}
  81.         --[[
  82.         if #blocks[blockNum] < 3 then
  83.             for j=#blocks[blockNum]+1, 3 do
  84.                 table.insert(blocks[blockNum], 0)
  85.             end
  86.         end
  87.         ]]
  88.         blockNum = blockNum+1
  89.     end
  90.     return blocks
  91. end
  92.  
  93. function encode(bytes)
  94.     local blocks = splitIntoBlocks(bytes)
  95.     local output = ""
  96.     for i=1, #blocks do
  97.         output = output..octetToBase64( unpack(blocks[i]) )
  98.     end
  99.     return output
  100. end
  101.  
  102. function decode(str)
  103.     local bytes = {}
  104.     local blocks = {}
  105.     local blockNum = 1
  106.     for i=1, #str, 4 do
  107.         blocks[blockNum] = string.sub(str, i, i+3)
  108.         blockNum = blockNum+1
  109.     end
  110.     for i=1, #blocks do
  111.         local o1, o2, o3 = base64ToThreeOctet(blocks[i])
  112.         table.insert(bytes, o1)
  113.         table.insert(bytes, o2)
  114.         table.insert(bytes, o3)
  115.     end
  116.     -- Remove padding:
  117.     --[[
  118.     for i=#bytes, 1, -1 do
  119.         if bytes[i] ~= 0 then
  120.             break
  121.         else
  122.             bytes[i] = nil
  123.         end
  124.     end
  125.     ]]
  126.     return bytes
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement