【问题标题】:how can you make a random password generator in lua?如何在 lua 中制作随机密码生成器?
【发布时间】:2022-09-23 02:19:28
【问题描述】:
 local function generator()

    local capital_letters = {\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\", \"K\", \"L\", \"M\", \"N\", \"O\", \"P\", \"Q\", \"R\", \"S\", \"T\", \"U\", \"V\", \"W\", \"X\", \"Y\", \"Z\"}
    local low_letters = {\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\", \"k\", \"l\", \"m\", \"n\", \"o\", \"p\", \"q\", \"r\", \"s\", \"t\", \"u\", \"v\", \"w\", \"x\", \"y\", \"z\"}
    local numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}


    local Random_capital_letters = math.random(26)
    local Random_low_letters = math.random(26)
    local Random_numbers = math.random(10)
    local length = 10
    print(\"this is your generatet password: \"..Random_capital_letters, Random_low_letters, Random_numbers[length])
    math.randomseed(os.time())
end


generator()

它只是一直给我一个错误,如果有人可以帮助我,那就太酷了!

  • 您正在...索引一个您应该用作索引的数字???应该是capital_letters[math.random(#capital_letters)] 等。
  • 当您遇到错误时,您应该始终将其包含在您的帖子中,这样我们就更容易“教你如何钓鱼”
  • 我没有看到构造字符串的实际循环。您需要循环 x 次(所需字符串的长度)并在每次循环中选择一个随机字符,然后将其附加到最终字符串。
  • 另请注意,math.randomseed(os.time()) 应在使用之前调用,math.random 方法 - 否则它们是非常可预测的。在同一张纸条上使用os.time()可能如果调用速度足够快,则会产生相同的输出。

标签: random lua passwords generator


【解决方案1】:
  1. 在使用math.random之前必须初始化随机种子
  2. 最好使用表的长度(capital_letterslow_lettersnumbers)来使用math.random 函数来选择一个值并创建您的密码。
  3. 10 值不能在numbers 表中
  4. 创建密码需要一个循环:从 1 迭代到 length,并且在每一步中,在 capital_letterslow_lettersnumbers 表中选择一个随机值。

    改编自您的 Lua 代码的工作版本:

    local function generator()
    
      local capital_letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
      local low_letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
      local numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    
      math.randomseed(os.time())
    
      local length = 10
    
      local pass = ""
      local choice = 0
    
      for _ = 1, length do
        choice = math.random(3)
    
        -- Capital letters
        if choice == 1 then
          pass = pass .. capital_letters[math.random(#capital_letters)]
        -- Low letters
        elseif choice == 2 then
          pass = pass .. low_letters[math.random(#low_letters)]
        -- Numbers
        else
          pass = pass .. numbers[math.random(#numbers)]
        end
      end
    
      print(pass)
    end
    
    generator()
    

【讨论】:

    【解决方案2】:

    Lua 有一个字符串库。
    所有字符串都有作为方法附加的字符串库函数。
    因此,可以直接使用未定义的字符串来获取方法。
    示例从中获取字母/数字/符号是多么简单......

    -- pwgen.lua
    local function pwgen(count)
    -- Set the Seek
    math.randomseed(math.random(os.time(), 9223372036854775807)) 
    -- Roll the Dice before really using/choosing a random number
    for i=1,math.random(20) do math.random() end
    -- Main Loop
    for i = 1, count do
    -- Output to stdout
     io.write(('').char(math.random(47, 122)))
    end
    -- No Return Value
    end
    
    pwgen(32) -- Example output 32 random Chars
    
    return pwgen -- If you wanna: pwgen = require('pwgen')
    

    示例输出...

    3dsU0SN8azp^VZiceS9O@qoP_n>?mEaz
    

    【讨论】:

      猜你喜欢
      • 2012-11-06
      • 2012-07-28
      • 2018-11-01
      • 1970-01-01
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多