diff --git a/nselib/stdnse.lua b/nselib/stdnse.lua index 7ce532a7c..cad26519d 100644 --- a/nselib/stdnse.lua +++ b/nselib/stdnse.lua @@ -135,6 +135,30 @@ function strsplit(pattern, text) return list; end +--- Generate a random string. +-- You can either provide your own charset or the function will use +-- a default one which is [A-Z]. +-- @param len Length of the string we want to generate. +-- @param charset Charset that will be used to generate the string. +-- @return A random string of length len consisting of +-- characters from charset if one was provided, otherwise +-- charset defaults to [A-Z] letters. +function generate_random_string(len, charset) + local t = {} + local ascii_A = 65 + local ascii_Z = 90 + if charset then + for i=1,len do + t[i]=charset[math.random(#charset)] + end + else + for i=1,len do + t[i]=string.char(math.random(ascii_A,ascii_Z)) + end + end + return table.concat(t) +end + --- Return a wrapper closure around a socket that buffers socket reads into -- chunks separated by a pattern. --