lua-users home
lua-l archive

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


On 8 April 2015 at 16:49, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 2015-04-08 21:18 GMT+02:00 Hisham <h@hisham.hm>:
>> On 8 April 2015 at 04:04, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>
>>> math.randomseed(os.clock())
>>> function password()  return (("."):rep(32):gsub(".",
>>>   function() return string.char(math.random(256)-1) end))
>>> end
>>>
>>> Now make two tables: one containing the original keys
>>> and a password for each, the other containing those
>>> passwords as keys with the original values.
>>>
>>> Three times gsub:
>>>   the ${var} forms with the first table,
>>>   the $var forms with the first table,
>>>   the passwords with the second table.
>>>
>>> If you are scared this might not work, better not use
>>> ATMs any more either.
>>
>> 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:

math.randomseed(os.clock())
local dots = ("."):rep(64)
function password()  return "<"..(dots:gsub(".",
  function() return string.char(64+math.random(26)) end))..">"
end
local t1, t2 = {}, {}
for k,v in pairs(variables) do
   local p = password()
   t1[k] = p
   t2[p] = v
end
local function expand_variables(str)
   return str:gsub("%${([A-Za-z0-9_]+)}",
t1):gsub("%$([A-Za-z0-9_]+)", t1):gsub("<"..dots..">", t2)
end

-- Hisham