Pure Lua base64 encoder/decoder. Works with Lua 5.1+ and LuaJIT. Fallbacks to pure Lua bit operations if bit/bit32/native bit operators are not available.
local base64 = require'base64'
local str = 'Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.'
local b64str = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4='
local encoded = base64.encode( str )
local decoded = base64.decode( b64str )
assert( str == decoded )
assert( b64str == encoded )
Encodes str
string using encoder
table. By default uses table with +
as
char for 62, /
as char for 63 and =
as padding char. You can specify custom
encoder. For this you could use base64.makeencoder
. If you are encoding large
chunks of text (or another highly redundant data) it's possible to highly
increase performace (for text approx. x2 gain) by using usecache = true
. For
binary data like images using cache decreasing performance.
Decodes str
string using decoder
table. Default decoder uses same chars as
default encoder.
Make custom encoding table
Make custom decoding table
luarocks install base64