diff --git a/nselib/stdnse.lua b/nselib/stdnse.lua
index c154e2319..b98c04c40 100644
--- a/nselib/stdnse.lua
+++ b/nselib/stdnse.lua
@@ -252,7 +252,7 @@ end
-- 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.
+-- @param charset Charset that will be used to generate the string. String or table
-- @return A random string of length len consisting of
-- characters from charset if one was provided, otherwise
-- charset defaults to [A-Z] letters.
@@ -261,8 +261,15 @@ function generate_random_string(len, charset)
local ascii_A = 65
local ascii_Z = 90
if charset then
- for i=1,len do
- t[i]=charset[random(#charset)]
+ 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