summaryrefslogtreecommitdiffstats
path: root/nselib/rand.lua
blob: 2591807f5e5ea7a2ccb08e50cf2a1af9dbe02fb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
--- Functions for generating random data
--
-- The strings generated here are not cryptographically secure, but they should
-- be sufficient for most purposes.
--
-- @author Daniel Miller
-- @copyright Same as Nmap--See https://nmap.org/book/man-legal.html
-- @class module
-- @name rand

local require = require
local have_openssl, openssl = pcall(require, "openssl")

local math = require "math"
local random = math.random

local string = require "string"
local byte = string.byte
local char = string.char
local sub = string.sub

local table = require "table"
local concat = table.concat

local type = type
local _ENV = {}

local get_random_bytes
if have_openssl then
  get_random_bytes = openssl.rand_pseudo_bytes
else
  get_random_bytes = require "nmap".get_random_bytes
end

--- Generate a random string.
--
-- You can either provide your own charset or the function will generate random
-- bytes, which may include null bytes.
-- @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, or random bytes otherwise.
random_string = function(len, charset)
  local t = {}
  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
    return get_random_bytes(len)
  end
  return concat(t)
end
local random_string = random_string

--- Generate a charset that can be passed to <code>random_string</code>
--
-- @param left_bound The lower bound character or byte value of the set
-- @param right_bound The upper bound character or byte value of the set
-- @param charset Optional, a charset table to augment. By default a new charset is created.
-- @return A charset table
function charset(left_bound, right_bound, charset)
  local t = charset or {}
  left_bound = type(left_bound)=="string" and byte(left_bound) or left_bound
  right_bound = type(right_bound)=="string" and byte(right_bound) or right_bound
  if left_bound > right_bound then
    return t
  end
  for i=left_bound,right_bound do
    t[#t+1] = char(i)
  end
  return t
end
local charset = charset

local alpha_charset = charset('a', 'z')
--- Generate a random alpha word
--
-- Convenience wrapper around <code>random_string</code> to generate a random
-- string of lowercase alphabetic characters.
-- @param len The length of word to return
-- @return A string of random characters between 'a' and 'z' inclusive.
function random_alpha (len)
  return random_string(len, alpha_charset)
end

return _ENV