Skip to content

Instantly share code, notes, and snippets.

@toastdriven
Created April 5, 2012 07:17
Show Gist options
  • Save toastdriven/2308716 to your computer and use it in GitHub Desktop.
Save toastdriven/2308716 to your computer and use it in GitHub Desktop.
-- Base64-encoding
-- Sourced from http://en.wikipedia.org/wiki/Base64
-- BSD licensed
require('math')
local index_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
function to_binary(integer)
local remaining = tonumber(integer)
local bin_bits = ''
for i = 7, 0, -1 do
local current_power = math.pow(2, i)
if remaining >= current_power then
bin_bits = bin_bits .. '1'
remaining = remaining - current_power
else
bin_bits = bin_bits .. '0'
end
end
return bin_bits
end
function from_binary(bin_bits)
return tonumber(bin_bits, 2)
end
function to_base64(to_encode)
local bit_pattern = ''
local encoded = ''
local trailing = ''
for i = 1, string.len(to_encode) do
bit_pattern = bit_pattern .. to_binary(string.byte(string.sub(to_encode, i, i)))
end
-- Check the number of bytes. If it's not evenly divisible by three,
-- zero-pad the ending & append on the correct number of ``=``s.
if math.mod(string.len(bit_pattern), 3) == 2 then
trailing = '=='
bit_pattern = bit_pattern .. '0000000000000000'
elseif math.mod(string.len(bit_pattern), 3) == 1 then
trailing = '='
bit_pattern = bit_pattern .. '00000000'
end
for i = 1, string.len(bit_pattern), 6 do
local byte = string.sub(bit_pattern, i, i+5)
local offset = tonumber(from_binary(byte))
encoded = encoded .. string.sub(index_table, offset+1, offset+1)
end
return string.sub(encoded, 1, -1 - string.len(trailing)) .. trailing
end
function from_base64(to_decode)
local bit_pattern = ''
local decoded = ''
for i = 1, string.len(to_decode) do
local char = string.sub(to_decode, i, i)
if char ~= '=' then
local offset, _ = string.find(index_table, char)
if offset == nil then
error("Invalid character '" .. char .. "' found.")
end
bit_pattern = bit_pattern .. string.sub(to_binary(offset-1), 3)
end
end
for i = 1, string.len(bit_pattern), 8 do
local byte = string.sub(bit_pattern, i, i+7)
decoded = decoded .. string.char(from_binary(byte))
end
return decoded
end
require('base64')
print(to_base64('Man')) -- 'TWFu'
print(to_base64('leasure.')) -- 'bGVhc3VyZS4='
print(to_base64('pleasure.')) -- 'cGxlYXN1cmUu'
print(to_base64('easure.')) -- 'ZWFzdXJlLg=='
print(to_base64('sure.')) -- 'c3VyZS4='
print(from_base64('TWFu')) -- 'Man'
print(from_base64('bGVhc3VyZS4=')) -- 'leasure.'
print(from_base64('cGxlYXN1cmUu')) -- 'pleasure.'
print(from_base64('ZWFzdXJlLg==')) -- 'easure.'
print(from_base64('c3VyZS4=')) -- 'sure.'
@toastdriven
Copy link
Author

Done to make myself better for being unable to do much of anything with the DCPU-16 spec Notch released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment