Advertisement
osmarks

Lolcrypt

Sep 23rd, 2018
2,477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.13 KB | None | 0 0
  1. local function chars(str)
  2.     local pos = 1
  3.     return function()
  4.         if pos <= #str  then
  5.             local pos_was = pos
  6.             pos = pos + 1
  7.             return str:sub(pos_was, pos_was), pos_was
  8.         end
  9.     end
  10. end
  11.  
  12. -- from lua users wiki - magic
  13. local function unpackbits(num, width)
  14.     local fl = {}
  15.     local rem
  16.     for i = 1,width do
  17.         num,rem = math.modf(num/2)
  18.         fl[#fl+1] = rem>=0.5
  19.     end
  20.     return fl
  21. end
  22.  
  23. local function permutation(str, ix)
  24.     local bits = unpackbits(ix, #str)
  25.     local this = ""
  26.     for char, idx in chars(str) do
  27.         local newchar = char:lower()
  28.         if bits[idx] then newchar = char:upper() end
  29.         this = this .. newchar
  30.     end
  31.     return this
  32. end
  33.  
  34. local function caps_permutations(str)
  35.     local combinations = math.pow(2, #str) - 1
  36.     local ret = {}
  37.     for i = 0, combinations do
  38.         table.insert(ret, permutation(str, i))
  39.     end
  40.     return ret
  41. end
  42.  
  43. local function nybbles(byte)
  44.     return bit.brshift(bit.band(0xF0, byte), 4), bit.band(0x0F, byte)
  45. end
  46.  
  47. local function rpt(t, x)
  48.     local out = {}
  49.     for i = 1, x do
  50.         for _, v in pairs(t) do
  51.             table.insert(out, v)
  52.         end
  53.     end
  54.     return out
  55. end
  56.  
  57. local function invert(t)
  58.     local out = {}
  59.     for k, v in pairs(t) do
  60.         out[v] = k
  61.     end
  62.     return out
  63. end
  64.  
  65. local function sanify(t)
  66.     local ix = 0
  67.     local out = {}
  68.     for _, v in pairs(t) do
  69.         out[ix] = v
  70.         ix = ix + 1
  71.     end
  72.     return out
  73. end
  74.  
  75. local dictionary = caps_permutations "lol"
  76. for k, v in pairs({
  77.     " ",
  78.     ".",
  79.     "?",
  80.     "!",
  81.     "#",
  82.     ",",
  83.     ";",
  84.     ":"
  85. }) do table.insert(dictionary, v) end
  86. dictionary = sanify(dictionary)
  87. local inverse_dictionary = invert(dictionary)
  88.  
  89. local function encode(str)
  90.     local out = ""
  91.     for char in chars(str) do
  92.         local hi, lo = nybbles(string.byte(char))
  93.         out = out .. dictionary[hi] .. dictionary[lo]
  94.     end
  95.     return out
  96. end
  97.  
  98. local function tokenize(str)
  99.     local in_lol = ""
  100.     local toks = {}
  101.     for char, index in chars(str) do
  102.         local lowered = char:lower()
  103.         if in_lol ~= "" then -- if we have a current lol, push lol to the tokens stack and clear it if we get a L
  104.             in_lol = in_lol .. char
  105.             if lowered == "l" then
  106.                 table.insert(toks, in_lol)
  107.                 in_lol = ""
  108.             elseif lowered == "o" then
  109.             else error "Invalid character in LOL" end
  110.         else
  111.             if lowered == "l" then
  112.                 in_lol = char
  113.             else
  114.                 table.insert(toks, char)
  115.             end
  116.         end
  117.     end
  118.     return toks
  119. end
  120.  
  121. local function decode_one(tok)
  122.     local d = inverse_dictionary[tok]
  123.     if not d then error("Invalid token in loltext: " .. tostring(tok)) end
  124.     return d
  125. end
  126.  
  127. local function decode_pair(t1, t2)
  128.     local hi, lo = decode_one(t1), decode_one(t2)
  129.     local n = bit.bor(bit.blshift(hi, 4), lo)
  130.     return string.char(n)
  131. end
  132.  
  133. local function decode(str)
  134.     local toks = tokenize(str)
  135.     local out = ""
  136.     while true do
  137.         local t1, t2 = table.remove(toks, 1), table.remove(toks, 1)
  138.         if not t1 or not t2 then
  139.             break
  140.         else
  141.             out = out .. decode_pair(t1, t2)
  142.         end
  143.     end
  144.     return out
  145. end
  146.  
  147. local function repeat_function(f, times)
  148.     return function(data, times_)
  149.         local times = times_ or times
  150.         local d = data
  151.         for i = 1, times do
  152.             d = f(d)
  153.         end
  154.         return d
  155.     end
  156. end
  157.  
  158. return { encode = repeat_function(encode, 1), decode = repeat_function(decode, 1) }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement