纯Lua实现的Base64,识货的拿去

ZZBase64 = {}local string = stringZZBase64.__code = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P','Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f','g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v','w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',};ZZBase64.__decode = {}for k,v in pairs(ZZBase64.__code) doZZBase64.__decode[string.byte(v,1)] = k – 1endfunction ZZBase64.encode(text)local len = string.len(text)local left = len % 3len = len – leftlocal res = {}local index = 1for i = 1, len, 3 dolocal a = string.byte(text, i )local b = string.byte(text, i + 1)local c = string.byte(text, i + 2)– num = a<<16 + b<<8 + clocal num = a * 65536 + b * 256 + cfor j = 1, 4 do–tmp = num >> ((4 -j) * 6)local tmp = math.floor(num / (2 ^ ((4-j) * 6)))–curPos = tmp&0x3flocal curPos = tmp % 64 + 1res[index] = ZZBase64.__code[curPos]index = index + 1endendif left == 1 thenZZBase64.__left1(res, index, text, len)elseif left == 2 thenZZBase64.__left2(res, index, text, len)endreturn table.concat(res)endfunction ZZBase64.__left2(res, index, text, len)local num1 = string.byte(text, len + 1)num1 = num1 * 1024 –lshift 10local num2 = string.byte(text, len + 2)num2 = num2 * 4 –lshift 2local num = num1 + num2local tmp1 = math.floor(num / 4096) –rShift 12local curPos = tmp1 % 64 + 1res[index] = ZZBase64.__code[curPos]local tmp2 = math.floor(num / 64)curPos = tmp2 % 64 + 1res[index + 1] = ZZBase64.__code[curPos]curPos = num % 64 + 1res[index + 2] = ZZBase64.__code[curPos]res[index + 3] = "=" endfunction ZZBase64.__left1(res, index,text, len)local num = string.byte(text, len + 1)num = num * 16tmp = math.floor(num / 64)local curPos = tmp % 64 + 1res[index ] = ZZBase64.__code[curPos]curPos = num % 64 + 1res[index + 1] = ZZBase64.__code[curPos]res[index + 2] = "="res[index + 3] = "=" endfunction ZZBase64.decode(text)local len = string.len(text)local left = 0if string.sub(text, len – 1) == "==" thenleft = 2len = len – 4elseif string.sub(text, len) == "=" thenleft = 1len = len – 4endlocal res = {}local index = 1local decode = ZZBase64.__decodefor i =1, len, 4 dolocal a = decode[string.byte(text,i )]local b = decode[string.byte(text,i + 1)]local c = decode[string.byte(text,i + 2)]local d = decode[string.byte(text,i + 3)]–num = a<<18 + b<<12 + c<<6 + dlocal num = a * 262144 + b * 4096 + c * 64 + dlocal e = string.char(num % 256)num = math.floor(num / 256)local f = string.char(num % 256)num = math.floor(num / 256)res[index ] = string.char(num % 256)res[index + 1] = fres[index + 2] = eindex = index + 3endif left == 1 thenZZBase64.__decodeLeft1(res, index, text, len)elseif left == 2 thenZZBase64.__decodeLeft2(res, index, text, len)endreturn table.concat(res)endfunction ZZBase64.__decodeLeft1(res, index, text, len)local decode = ZZBase64.__decodelocal a = decode[string.byte(text, len + 1)]local b = decode[string.byte(text, len + 2)]local c = decode[string.byte(text, len + 3)]local num = a * 4096 + b * 64 + clocal num1 = math.floor(num / 1024) % 256local num2 = math.floor(num / 4) % 256res[index] = string.char(num1)res[index + 1] = string.char(num2)endfunction ZZBase64.__decodeLeft2(res, index, text, len)local decode = ZZBase64.__decodelocal a = decode[string.byte(text, len + 1)]local b = decode[string.byte(text, len + 2)]local num = a * 64 + bnum = math.floor(num / 16)res[index] = string.char(num)endfunction ZZBase64.test()local data = "a\193\207="local abc = ZZBase64.encode(data)print(abc)def = ZZBase64.decode(abc)if def == data thenprint("yes")endendZZBase64.test()

,奢侈地享受旅不问人,行随己意的潇洒。

纯Lua实现的Base64,识货的拿去

相关文章:

你感兴趣的文章:

标签云: