lua-arc4random

Cryptographically secure PRNG for Lua
Log | Files | Refs | README

README.md (1316B)


      1 A Lua wrapper for OpenBSD's arc4random.
      2 
      3 
      4 Requirements
      5 ------------
      6 
      7 lua >= 5.1
      8 
      9 
     10 Copying
     11 -------
     12 
     13 Many of the files in this repository have been taken from OpenBSD's
     14 tree. You should consult individual file headers for specific licensing
     15 information. More broadly, everything here is compatible with the [ISC
     16 license][ISC].
     17 
     18 [ISC]: http://en.wikipedia.org/wiki/ISC_license
     19 
     20 
     21 Installation
     22 ------------
     23 
     24 	$ luarocks install arc4random
     25 
     26 
     27 Usage
     28 -----
     29 
     30 The library provides two methods, `random` and `buf`. `random` is
     31 intended to be a drop in replacement for `math.random`, so it handles
     32 three cases.
     33 
     34 `arc4.random()` returns a random floating point number in the range
     35 [0,1). `arc4.random( n )` and `arc4.random( m, n )` return a random
     36 integer in the range [1,n] and [m,n] respectively. All three cases match
     37 the behavior of `math.random` so you can do `math.random = arc4.random`
     38 and everything will keep working.
     39 
     40 `arc4.buf( n )` returns a string of `n` random characters. It is
     41 suitable for generating private encryption keys and IVs.
     42 
     43 Some example code:
     44 
     45 	local arc4 = require( "arc4random" )
     46 	
     47 	print( arc4.random() )
     48 	print( arc4.random( 3 ) )
     49 	print( arc4.random( -5, 5 ) )
     50 	
     51 	local str = arc4.buf( 16 )
     52 	str = str:gsub( "(.)", function( c )
     53 		return ( "%02x" ):format( string.byte( c ) )
     54 	end )
     55 	print( str )