1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 12:41:29 +00:00

Standardize random string generation on stdnse.generate_random_string

This commit is contained in:
dmiller
2015-02-25 05:06:08 +00:00
parent f6733b2d89
commit db717c7543
10 changed files with 15 additions and 119 deletions

View File

@@ -146,25 +146,3 @@ function init()
stdnse.debug1("Testing %d usernames.", #usernames)
return nil
end
---
-- Uses openssl.rand_pseudo_bytes (if available, os.time() if not) and base64.enc
-- to produce a randomish string of at least 11 alphanumeric chars.
-- @return String
function randomstring()
local rnd, s, l, _
local status, openssl = pcall(require, "openssl")
if status then
rnd = openssl.rand_pseudo_bytes
end
s = rnd and rnd(8) or tostring( os.time() )
-- increase the length of the string by 0 to 7 chars
_, l = bin.unpack(">C", s, 8) -- eighth byte should be safe for os.time() too
s = l%8 > 0 and s .. s:sub(1,l%8) or s
-- base 64 encode and replace any non alphanum chars (with 'n' for nmap!)
s = base64.enc(s):sub(1,-2):gsub("%W", "n")
return s
end