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
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
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')
function random_alpha (len)
  return random_string(len, alpha_charset)
end
return _ENV
