b64.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/usr/local/bin/lua
  2. -- http://lua-users.org/wiki/BaseSixtyFour
  3. -- working lua base64 codec (c) 2006-2008 by Alex Kloss
  4. -- compatible with lua 5.1
  5. -- http://www.it-rfc.de
  6. -- licensed under the terms of the LGPL2
  7. -- bitshift functions (<<, >> equivalent)
  8. -- shift left
  9. function lsh(value,shift)
  10. return (value*(2^shift)) % 256
  11. end
  12. -- shift right
  13. function rsh(value,shift)
  14. return math.floor(value/2^shift) % 256
  15. end
  16. -- return single bit (for OR)
  17. function bit(x,b)
  18. return (x % 2^b - x % 2^(b-1) > 0)
  19. end
  20. -- logic OR for number values
  21. function lor(x,y)
  22. result = 0
  23. for p=1,8 do result = result + (((bit(x,p) or bit(y,p)) == true) and 2^(p-1) or 0) end
  24. return result
  25. end
  26. -- encryption table
  27. local base64chars = {
  28. [0]='A',
  29. [1]='B',
  30. [2]='C',
  31. [3]='D',
  32. [4]='E',
  33. [5]='F',
  34. [6]='G',
  35. [7]='H',
  36. [8]='I',
  37. [9]='J',
  38. [10]='K',
  39. [11]='L',
  40. [12]='M',
  41. [13]='N',
  42. [14]='O',
  43. [15]='P',
  44. [16]='Q',
  45. [17]='R',
  46. [18]='S',
  47. [19]='T',
  48. [20]='U',
  49. [21]='V',
  50. [22]='W',
  51. [23]='X',
  52. [24]='Y',
  53. [25]='Z',
  54. [26]='a',
  55. [27]='b',
  56. [28]='c',
  57. [29]='d',
  58. [30]='e',
  59. [31]='f',
  60. [32]='g',
  61. [33]='h',
  62. [34]='i',
  63. [35]='j',
  64. [36]='k',
  65. [37]='l',
  66. [38]='m',
  67. [39]='n',
  68. [40]='o',
  69. [41]='p',
  70. [42]='q',
  71. [43]='r',
  72. [44]='s',
  73. [45]='t',
  74. [46]='u',
  75. [47]='v',
  76. [48]='w',
  77. [49]='x',
  78. [50]='y',
  79. [51]='z',
  80. [52]='0',
  81. [53]='1',
  82. [54]='2',
  83. [55]='3',
  84. [56]='4',
  85. [57]='5',
  86. [58]='6',
  87. [59]='7',
  88. [60]='8',
  89. [61]='9',
  90. [62]='-',
  91. [63]='_'
  92. }
  93. -- function encode
  94. -- encodes input string to base64.
  95. function enc(data)
  96. local bytes = {}
  97. local result = ""
  98. for spos=0,string.len(data)-1,3 do
  99. for byte=1,3 do bytes[byte] = string.byte(string.sub(data,(spos+byte))) or 0 end
  100. result = string.format('%s%s%s%s%s',result,base64chars[rsh(bytes[1],2)],base64chars[lor(lsh((bytes[1] % 4),4), rsh(bytes[2],4))] or "=",((#data-spos) > 1) and base64chars[lor(lsh(bytes[2] % 16,2), rsh(bytes[3],6))] or "=",((#data-spos) > 2) and base64chars[(bytes[3] % 64)] or "=")
  101. end
  102. return result
  103. end
  104. -- decryption table
  105. local base64bytes = {
  106. ['A']=0,
  107. ['B']=1,
  108. ['C']=2,
  109. ['D']=3,
  110. ['E']=4,
  111. ['F']=5,
  112. ['G']=6,
  113. ['H']=7,
  114. ['I']=8,
  115. ['J']=9,
  116. ['K']=10,
  117. ['L']=11,
  118. ['M']=12,
  119. ['N']=13,
  120. ['O']=14,
  121. ['P']=15,
  122. ['Q']=16,
  123. ['R']=17,
  124. ['S']=18,
  125. ['T']=19,
  126. ['U']=20,
  127. ['V']=21,
  128. ['W']=22,
  129. ['X']=23,
  130. ['Y']=24,
  131. ['Z']=25,
  132. ['a']=26,
  133. ['b']=27,
  134. ['c']=28,
  135. ['d']=29,
  136. ['e']=30,
  137. ['f']=31,
  138. ['g']=32,
  139. ['h']=33,
  140. ['i']=34,
  141. ['j']=35,
  142. ['k']=36,
  143. ['l']=37,
  144. ['m']=38,
  145. ['n']=39,
  146. ['o']=40,
  147. ['p']=41,
  148. ['q']=42,
  149. ['r']=43,
  150. ['s']=44,
  151. ['t']=45,
  152. ['u']=46,
  153. ['v']=47,
  154. ['w']=48,
  155. ['x']=49,
  156. ['y']=50,
  157. ['z']=51,
  158. ['0']=52,
  159. ['1']=53,
  160. ['2']=54,
  161. ['3']=55,
  162. ['4']=56,
  163. ['5']=57,
  164. ['6']=58,
  165. ['7']=59,
  166. ['8']=60,
  167. ['9']=61,
  168. ['-']=62,
  169. ['_']=63,
  170. ['=']=nil
  171. }
  172. -- function decode
  173. -- decode base64 input to string
  174. function dec(data)
  175. local chars = {}
  176. local result=""
  177. for dpos=0,string.len(data)-1,4 do
  178. for char=1,4 do chars[char] = base64bytes[(string.sub(data,(dpos+char),(dpos+char)) or "=")] end
  179. result = string.format(
  180. '%s%s%s%s',
  181. result,
  182. string.char(lor(lsh(chars[1],2), rsh(chars[2],4))),
  183. (chars[3] ~= nil) and string.char(lor(lsh(chars[2],4),
  184. rsh(chars[3],2))) or "",
  185. (chars[4] ~= nil) and string.char(lor(lsh(chars[3],6) % 192,
  186. (chars[4]))) or ""
  187. )
  188. end
  189. return result
  190. end
  191. -- command line if not called as library
  192. if (arg ~= nil) then
  193. local func = 'enc'
  194. for n,v in ipairs(arg) do
  195. if (n > 0) then
  196. if (v == "-h") then print "base64.lua [-e] [-d] text/data" break
  197. elseif (v == "-e") then func = 'enc'
  198. elseif (v == "-d") then func = 'dec'
  199. else print(_G[func](v)) end
  200. end
  201. end
  202. else
  203. module('base64',package.seeall)
  204. end