1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-27 18:09:01 +00:00

Move stdnse.generate_random_string to new rand.lua

This commit is contained in:
dmiller
2018-09-08 17:07:06 +00:00
parent deea96de7c
commit 26ef852a28
43 changed files with 123 additions and 186 deletions

View File

@@ -30,7 +30,6 @@ local pcall = pcall
local ceil = math.ceil
local max = math.max
local random = math.random
local format = string.format;
local rep = string.rep
@@ -227,38 +226,6 @@ 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. String or table
-- @return A random string of length <code>len</code> consisting of
-- characters from <code>charset</code> if one was provided, otherwise
-- <code>charset</code> defaults to [A-Z] letters.
function generate_random_string(len, charset)
local t = {}
local ascii_A = 65
local ascii_Z = 90
if charset then
if type(charset) == "string" then
for i=1,len do
local r = random(#charset)
t[i] = sub(charset, r, r)
end
else
for i=1,len do
t[i]=charset[random(#charset)]
end
end
else
for i=1,len do
t[i]=char(random(ascii_A,ascii_Z))
end
end
return concat(t)
end
--- Return a wrapper closure around a socket that buffers socket reads into
-- chunks separated by a pattern.
--