lua-resty-random

bungle 太忙了,所以这个是分支

$ opm get xiangnanscu/lua-resty-random

lua-resty-random

lua-resty-random 是一个用于 OpenResty 的随机数库。

此库仅适用于具有 OpenSSL 支持的 OpenResty(或带有 Lua 模块的 Nginx)。

使用 lua-resty-random 的 Hello World

    local random = require "resty.random"
    -- Get two random bytes
    local bytes = random.bytes(2)
    -- Get two random bytes hexadecimal encoded
    local hxbts = random.bytes(2, 'hex')
    -- Get random number
    local numbr = random.number(1, 10)
    -- Get random token (by default uses A-Z, a-z, and 0-9 chars)
    local token = random.token(10)

关于内部实现

对于随机字节,lua-resty-random 使用 OpenSSL 的 RAND_bytes,该函数在使用 OpenSSL 编译的 OpenResty(或 Nginx)中包含。对于随机数,该库使用 Lua 的 math.randommath.randomseed。请注意,在 LuaJIT 环境中,LuaJIT 使用周期为 2^223 的 Tausworthe PRNG 来实现 math.randommath.randomseed。十六进制转储使用 ngx_hex_dump 实现。

Lua API

string random.bytes(len, format)

使用 OpenSSL RAND_bytes 返回 len 个随机字节。如果希望随机字节以十六进制编码,则可以选择传递 "hex" 作为格式参数。

示例

    local random = require "resty.random"
    print(random.bytes(10))
    print(random.bytes(10, "hex")

number random.number(min, max, reseed)

返回 minmax 之间的随机数(包括 minmax)。如果希望重新播种随机数生成器,则可以选择传递 true 作为 reseed 参数(通常不需要,并且在执行 require "resty.random" 时会播种一次随机数生成器)。

示例

    local random = require "resty.random"
    print(random.number(1, 10))
    print(random.number(1, 10, true))

string random.token(len, chars, sep)

返回由字符组成的随机令牌(默认情况下,它使用 A-Z、a-z 和 0-9 作为字符)。您还可以使用 sep 参数传递一个字符串作为分隔符。

示例

    local random = require "resty.random"
    print(random.token(10))
    print(random.token(10, "ABCD"))
    print(random.token(10, { "A", "B", "C", "D" }))
    print(random.token(10, { "Ford", "Audi", "Mustang", "A6" }, " "))

许可证

lua-resty-random 使用双条款 BSD 许可证。

    Copyright (c) 2013, Aapo Talvensaari
    All rights reserved.
    
    Redistribution and use in source and binary forms, with or without modification,
    are permitted provided that the following conditions are met:
    
    * Redistributions of source code must retain the above copyright notice, this
      list of conditions and the following disclaimer.
    
    * Redistributions in binary form must reproduce the above copyright notice, this
      list of conditions and the following disclaimer in the documentation and/or
      other materials provided with the distribution.
    
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
    ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
    ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

作者

Nan Xiang(@xiangnanscu)

许可证

2bsd

版本