BASE64 encoding implemented by LUA

Source: Internet
Author: User
Tags base64 lua
ZZBase64 = {} local string = string Zzbase64.__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) do Zzbase64.__decode[string.byte (v,1)] = K-1 End Function
    Zzbase64.encode (text) Local len = String.len (text) local left = len% 3 len = Len-left local res = {} Local index = 1 for i = 1, len, 3 do local A = String.byte (text, i) Local B = string.byte (text, I + 1) Local c = string.byte (text, i + 2)--num = a<<16 + b<<8 + c Local num = a * 6553 6 + b * + + c for j = 1, 4 do--tmp = num >> ((4-J) * 6) Local TMP = Math.floor (num/(2 ^ ((4-j) * 6))--curpos = tmp&0x3f Loc Al CurPos = tmp% + 1 Res[index] = Zzbase64.__code[curpos] Index = index + 1 end en D if left = = 1 then zzbase64.__left1 (res, index, text, len) ElseIf left = 2 then Zzbase64.__left 2 (res, index, text, len) end return Table.concat (res) End Function zzbase64.__left2 (res, index, text, Len  ) Local NUM1 = String.byte (text, Len + 1) num1 = NUM1 * 1024x768--lshift local num2 = String.byte (text, Len +
    2) num2 = num2 * 4--lshift 2 Local num = num1 + num2 Local TMP1 = Math.floor (num/4096)--rshift 12 Local CurPos = tmp1% + 1 Res[index] = zzbase64.__code[curpos] Local TMP2 = Math.floor (NUM/64) cur Pos = tmp2% + 1 Res[index + 1] = Zzbase64.__code[curpos] CurPos = num% + 1 Res[index + 2] = ZZBase64.
    
    __code[curpos]Res[index + 3] = "=" End Function zzbase64.__left1 (res, index,text, len) Local num = String.byte (text, Len + 1) num = num * tmp = Math.floor (num/64) local CurPos = tmp% + 1 Res[index] = Zzbase64.__code[curp OS] CurPos = num% + 1 Res[index + 1] = Zzbase64.__code[curpos] Res[index + 2] = "=" Res[ind Ex + 3] = "=" End Function Zzbase64.decode (text) Local len = String.len (text) Local left = 0 if string.sub ( Text, len-1) = = "= =" then left = 2 len = len-4 ElseIf string.sub (text, len) = = "=" Then L  EFT = 1 len = len-4 End Local res = {} Local index = 1 Local decode = Zzbase64.__decode for 
        I =1, Len, 4 do local 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&LT;&L t;18 + b<<12 + C&Lt;<6 + d Local num = a * 262144 + b * 4096 + c * + D local e = String.char (num% 256) num = Math.floor (num/256) Local f = string.char (num%) num = Math.floor (num/256) res[in

    DEX] = String.char (num%) Res[index + 1] = f Res[index + 2] = e index = index + 3 end If left = = 1 then zzbase64.__decodeleft1 (res, index, text, len) ElseIf left = 2 then Zzbase64.__dec ODELEFT2 (res, index, text, len) end return Table.concat (res) End Function Zzbase64.__decodeleft1 (res, index, text , len) Local decode = Zzbase64.__decode local 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 * + C LOC Al Num1 = Math.floor (num/1024)% Local num2 = Math.floor (NUM/4)% res[index] = String.char (NUM1) r Es[index + 1] = string.cHar (num2) End Function zzbase64.__decodeleft2 (res, index, text, len) Local decode = Zzbase64.__decode local A = d Ecode[string.byte (text, Len + 1)] Local B = decode[string.byte (text, Len + 2)] Local num = a * + b num = m Ath.floor (NUM/16) Res[index] = String.char (num) End Function zzbase64.test () Local data = "a\193\207=" Loca
    L ABC = zzbase64.encode (data) print (ABC) def = Zzbase64.decode (ABC) If DEF = = Data then print ("Yes")
 End End Zzbase64.test ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.