lua-users home
lua-l archive

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


On 1 August 2011 09:43, Luiz Henrique de Figueiredo
<lhf@tecgraf.puc-rio.br> wrote:
>> The following code
>>
>>     if (hash(io.read())==hashed_password) then ...
>>
>> suffers from the drawback that what you type appears on the screen.
>
> Try this:
>
> io.write("password: ")
> io.flush()
> os.execute("stty -echo")
> password=io.read()
> os.execute("stty sane")
> if (hash(password)==hashed_password) then ...
>

In Prosody we have this little function based on the above (it's not
pretty but I've yet for someone to complain it doesn't work):

function getpass()
        local stty_ret = os.execute("stty -echo 2>/dev/null");
        if stty_ret ~= 0 then
                io.write("\027[08m"); -- ANSI 'hidden' text attribute
        end
        local ok, pass = pcall(io.read, "*l");
        if stty_ret == 0 then
                os.execute("stty sane");
        else
                io.write("\027[00m");
        end
        io.write("\n");
        if ok then
                return pass;
        end
end

Regards,
Matthew