lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2015-04-08 22:33 GMT+02:00 Hisham <h@hisham.hm>:

>>> Haha, nice. :) The passwords would have to have some sort of constant
>>> marker for us to be able to perform the third gsub, right?
>>
>> Not really. One is searching for exact 32-byte strings.
>
> Yes, but how to find them all with a single gsub? It won't backtrack
> character by character. I had to add a marker, like this:

I see what you mean. But I'd rather make the first 16 bytes random, but
the same for each password, and the next 16 bytes different
for each, with the pattern having dots for the last 16 bytes, and wrap
everything in a function factory.

math.randomseed(os.clock())

function urim(n)  return ("."):rep(n):gsub(".",
  function() return string.char(94+math.random(151)) end)
end

function expand(variables)
   local t1, t2 = {}, {}
   local head = urim(16)
   for k,v in pairs(variables) do
      local p = head..urim(16)
      t1[k] = p
      t2[p] = v
   end
   return function (str)
      return str:gsub("%${([A-Za-z0-9_]+)}",t1):
                 gsub("%$([A-Za-z0-9_]+)", t1):
                 gsub(head..("."):rep(16), t2)
      end
end

expand_variables = expand(variables)

Of course, if it is given the input is valid UTF-8, the whole
problem becomes very much easier.