1 | -- Lua 5.1+ base64 v3.0 (c) 2009 by Alex Kloss <alexthkloss@web.de> |
2 | -- licensed under the terms of the LGPL2 |
3 | |
4 | -- character table string |
5 | local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' |
6 | |
7 | -- encoding |
8 | function encode64(data) |
9 | return ((data:gsub('.', function(x) |
10 | local r,b='',x:byte() |
11 | for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end |
12 | return r; |
13 | end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x) |
14 | if (#x < 6) then return '' end |
15 | local c=0 |
16 | for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end |
17 | return b:sub(c+1,c+1) |
18 | end)..({ '', '==', '=' })[#data%3+1]) |
19 | end |
20 | |
21 | -- decoding |
22 | function decode64(data) |
23 | data = string.gsub(data, '[^'..b..'=]', '') |
24 | return (data:gsub('.', function(x) |
25 | if (x == '=') then return '' end |
26 | local r,f='',(b:find(x)-1) |
27 | for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end |
28 | return r; |
29 | end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x) |
30 | if (#x ~= 8) then return '' end |
31 | local c=0 |
32 | for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end |
33 | return string.char(c) |
34 | end)) |
35 | end |
test run test run with input download show line numbers
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Recognizer | Recognition Result | Visualize | Recalc |
---|---|---|---|
#308 | 1194 | [visualize] |
Snippet ID: | #179 |
Snippet name: | base 64 encoder+decoder |
Eternal ID of this version: | #179/1 |
Text MD5: | aee054b83f5c8700cfeb70b42f2588e2 |
Author: | stefan |
Category: | general lua functions |
Type: | Lua code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2014-01-16 04:14:32 |
Source code size: | 1194 bytes / 35 lines |
Pitched / IR pitched: | Yes / Yes |
Views / Downloads: | 1031 / 14114 |
Referenced in: | [show references] |