Advertisement
Guest User

base64

a guest
Oct 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. -- b64 by PixelToast
  2. -- this one is a bit faster than the one on the lua users wiki
  3. local _tob64={
  4. [0]="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",
  5. "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",
  6. "0","1","2","3","4","5","6","7","8","9","+","/"
  7. }
  8. local function tob64(stxt)
  9. local txt=tostring(stxt)
  10. if not txt then
  11. error("string expected, got "..type(stxt),2)
  12. end
  13. local d,o,d1,d2,d3={string.byte(txt,1,#txt)},""
  14. for l1=1,#txt-2,3 do
  15. d1,d2,d3=d[l1],d[l1+1],d[l1+2]
  16. o=o.._tob64[math.floor(d1/4)].._tob64[((d1%4)*16)+math.floor(d2/16)].._tob64[((d2%16)*4)+math.floor(d3/64)].._tob64[d3%64]
  17. end
  18. local m=#txt%3
  19. if m==1 then
  20. o=o.._tob64[math.floor(d[#txt]/4)].._tob64[((d[#txt]%4)*16)].."=="
  21. elseif m==2 then
  22. o=o.._tob64[math.floor(d[#txt-1]/4)].._tob64[((d[#txt-1]%4)*16)+math.floor(d[#txt]/16)].._tob64[(d[#txt]%16)*4].."="
  23. end
  24. return o
  25. end
  26. local _unb64={
  27. ["A"]=0,["B"]=1,["C"]=2,["D"]=3,["E"]=4,["F"]=5,["G"]=6,["H"]=7,["I"]=8,["J"]=9,["K"]=10,["L"]=11,["M"]=12,["N"]=13,
  28. ["O"]=14,["P"]=15,["Q"]=16,["R"]=17,["S"]=18,["T"]=19,["U"]=20,["V"]=21,["W"]=22,["X"]=23,["Y"]=24,["Z"]=25,
  29. ["a"]=26,["b"]=27,["c"]=28,["d"]=29,["e"]=30,["f"]=31,["g"]=32,["h"]=33,["i"]=34,["j"]=35,["k"]=36,["l"]=37,["m"]=38,
  30. ["n"]=39,["o"]=40,["p"]=41,["q"]=42,["r"]=43,["s"]=44,["t"]=45,["u"]=46,["v"]=47,["w"]=48,["x"]=49,["y"]=50,["z"]=51,
  31. ["0"]=52,["1"]=53,["2"]=54,["3"]=55,["4"]=56,["5"]=57,["6"]=58,["7"]=59,["8"]=60,["9"]=61,["+"]=62,["/"]=63,
  32. }
  33. local function unb64(stxt)
  34. local txt=tostring(stxt)
  35. if not txt then
  36. error("string expected, got "..type(stxt),2)
  37. end
  38. txt=txt:gsub("[^%a%d/%+]","")
  39. local m=#txt%4
  40. if m==1 then
  41. error("invalid b64",2)
  42. end
  43. local o,d1,d2=""
  44. for l1=1,#txt-3,4 do
  45. d1,d2=_unb64[txt:sub(l1+1,l1+1)],_unb64[txt:sub(l1+2,l1+2)]
  46. o=o..string.char((_unb64[txt:sub(l1,l1)]*4)+math.floor(d1/16),((d1%16)*16)+math.floor(d2/4),((d2%4)*64)+_unb64[txt:sub(l1+3,l1+3)])
  47. end
  48. if m==2 then
  49. o=o..string.char((_unb64[txt:sub(-2,-2)]*4)+math.floor(_unb64[txt:sub(-1,-1)]/16))
  50. elseif m==3 then
  51. d1=_unb64[txt:sub(-2,-2)]
  52. o=o..string.char((_unb64[txt:sub(-3,-3)]*4)+math.floor(d1/16),((d1%16)*16)+math.floor(_unb64[txt:sub(-1,-1)]/4))
  53. end
  54. return o
  55. end
  56.  
  57. return { -- because some people complain about the function names
  58. to=tob64,
  59. un=unb64,
  60. from=unb64,
  61. tob64=tob64,
  62. unb64=unb64,
  63. b64=tob64,
  64. ub64=unb64,
  65. fromb64=unb64,
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement