用星号在lua中隐藏密码

在 Lua 中,有没有一种方法可以要求输入密码但用星号隐藏?

我在询问一个控制台应用程序。

原文链接 https://stackoverflow.com/questions/5787796

点赞
stackoverflow用户222815
stackoverflow用户222815

对于Unix系统,请使用 os.execute("stty -echo raw") 来关闭回显并进入原始模式(逐字符输入),使用 os.execute("stty echo cooked") 在完成后将其打开并退出原始模式。在原始模式下,您可以使用 io.stdin:read (1) 获取输入的每个字符,并随时回显星号(使用 io.flush 确保字符立即出现)。您需要自己处理删除和行的末尾。

对于Windows系统,情况要棘手一些。请参考 What would be the Windows batch equivalent for HTML's input type=“password”? 寻找相应解决方案,其中最好的方法似乎是 VB 脚本。

后记

感谢 lhf 指出,为了获得预期的结果,您需要除了输入时的 -echo 以外还需要原始模式和每次输出星号后都要刷新:否则,星号直到行末时才会回显。

2011-04-26 11:36:46
stackoverflow用户1847592
stackoverflow用户1847592

这段代码使用特定于平台的特性,在 Linux 和32位 Windows 上都可以工作。

与 Lua 5.1 和 Lua 5.2 兼容

local控制台

local function enter_password(prompt_message,asterisk_char,max_length)
   - 返回密码字符串
   - “输入”键完成密码
   - “Backspace”键撤消上一个输入的字符
   if not console then
      if(os.getenv'os' or''):lower():find'windows' then
         ------------------ Windows ------------------
         local shift = 10
         -- 创建可执行文件,该文件将(getch()+ shift)作为退出代码返回
         local getch_filespec ='getch.com'
         -- mov AH,8
         -- int 21h
         -- add AL,shift
         -- mov AH,4Ch
         -- int 21h
         local file = assert(io.open(getch_filespec,'wb'))
         file:write(string.char(0xB4,8,0xCD,0x21,4,shift,0xB4,0x4C,0xCD,0x21))
         file:close()
         console ={
            wait_key = function()
               local code_Lua51,_,code_Lua52 = os.execute(getch_filespec)
               local code =(code_Lua52或code_Lua51)-shift
               assert(code> = 0,getch_filespec ..'执行失败'return string.char(code)
            end,
            on_start = function()end,
            on_finish = function()end,
            backspace_key =' \ b'
         }
         -------------------------------------------
      else
         ------------------ Linux ------------------
         console ={
            wait_key = function()
               return io.read(1)
            end,
            on_start = function()
               os.execute'stty -echo raw'
            end,
            on_finish = function()
               os.execute'stty sane'
            end,
            backspace_key =' \ 127'
         }
         -------------------------------------------
      end
   end
   io.write(prompt_message or'')
   io.flush()
   local pwd =''
   console.on_start()
   repeat
      local c = console.wait_key()
      if c == console.backspace_key then
         ifpwd> 0 then
            io.write' \ b \ b'
            pwd = pwd:sub(1,-2)
         end
      elseif c ~ =' \ r'and#pwd <(max_length or 32) then
         io.write(asterisk_char or'*'pwd = pwd..c
      end
      io.flush()
   直到c =='\ r'
   console.on_finish()
   io.write' \ n'
   io.flush()
   返回pwd
end

- 用法示例
local pwd = enter_password'输入密码:'
print'您输入的内容:'..pwd:gsub('%c''?'))
printpwd:byte(1,-1))
2013-04-10 11:58:47
stackoverflow用户19845453
stackoverflow用户19845453

Bug in code, for at least linux implementation. Need to add 'and c ~= nil`:

代码存在错误,在Linux实现中至少需要添加and c ~= nil

elseif c ~= '\\r' and #pwd < (max_length or 32) and c ~= nil then
2022-08-25 13:00:59