Skip to content

Commit 985bdac

Browse files
committed
Release v0.0.1
1 parent feb301b commit 985bdac

File tree

7 files changed

+50
-37
lines changed

7 files changed

+50
-37
lines changed

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2025 luau-project [https://github.com/luau-project/lua-uuid](https://github.com/luau-project/lua-cryptorandom)
3+
Copyright (c) 2025 luau-project [https://github.com/luau-project/lua-cryptorandom](https://github.com/luau-project/lua-cryptorandom)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,35 @@
44

55
## Overview
66

7-
**lua-cryptorandom** is a lightweight, native library for Lua aimed to generate cryptographically-secure pseudo-random bytes and numbers, using trusted sources of randomness provided by the operating system.
7+
**lua-cryptorandom** is a lightweight, native library for Lua aimed to generate cryptographically secure pseudo random numbers, using trusted sources of randomness provided by the operating system.
88

9-
* On Unix-like distributions, it uses the ```OpenSSL``` library to generate random bytes and numbers;
9+
* On Unix-like distributions, it uses the ```OpenSSL``` library to generate random numbers;
1010
* On Windows, it uses the WINAPI ```bcrypt``` library;
1111
* On macOS / iOS, it uses the ```Security``` framework.
1212

1313
> [!NOTE]
1414
>
1515
> ```lua-cryptorandom``` is implemented in C, and also compiles as C++.
1616
17+
## Use cases
18+
19+
Many security operations rely on the high-quality of randomization services to avoid reproducibility and remain resistant to reverse engineering:
20+
21+
* randomized password generation;
22+
* nonces (numbers used once) generation;
23+
* initialization vectors;
24+
* salts in passwords before hashing;
25+
* tokenization (*token generation*) to represent sensitive data;
26+
* secure random sampling in statistical analysis.
27+
1728
## Table of Contents
1829

1930
* [Installation](#installation)
2031
* [Methods](#methods)
2132
* [bytes](#bytes)
2233
* [integer](#integer)
2334
* [number](#number)
24-
* [seed](#seed)
35+
* [take](#take)
2536
* [Change log](#change-log)
2637
* [Future works](#future-works)
2738

@@ -58,7 +69,7 @@ luarocks install lua-cryptorandom
5869

5970
> [!IMPORTANT]
6071
>
61-
> For each method below, always check whether the first returned value is ```nil``` or not. When the first value is ```nil```, there was an underlying error generating random values. It can fail because no trusted random source is available or the trusted source temporarily fail to provide sufficient randomness material.
72+
> For each method below, always check whether the first returned value is ```nil``` or not. When the first value is ```nil```, there was an underlying error generating random values. It can fail because no trusted random source is available or the trusted entropy source temporarily fail to provide sufficient randomness material.
6273
6374
### bytes
6475

@@ -67,8 +78,8 @@ luarocks install lua-cryptorandom
6778
* *Parameters*:
6879
* *n*: the number of bytes to generate
6980
* *Return*: ```table | nil``` as first value, and ```nil | number``` as the second.
70-
1. ```table | nil```: a table containing ```n``` bytes on success, or ```nil``` when an error occurred;
71-
2. ```nil | integer```: an error code that is set to ```nil``` on success, or an ```integer``` representing the code used by the underlying library (```OpenSSL``` on Unix, ```bcrypt``` on Windows and ```Security``` framework on macOS / iOS).
81+
* ```table | nil```: a table containing ```n``` bytes on success, or ```nil``` when an error occurred;
82+
* ```nil | integer```: an error code that is set to ```nil``` on success, or an ```integer``` representing the code used by the underlying library (```OpenSSL``` on Unix, ```bcrypt``` on Windows and ```Security``` framework on macOS / iOS).
7283
* *Remark*: Here, a byte is meant as an integer in the range 0 - 255.
7384
* *Usage*:
7485

@@ -97,9 +108,9 @@ luarocks install lua-cryptorandom
97108
* *Description*: Generates a random integer
98109
* *Signature*: ```integer()```
99110
* *Return*: ```integer | nil``` as first value, and ```nil | integer``` as the second.
100-
1. ```integer | nil```: the generated integer on success, or ```nil``` when an error occurred;
101-
2. ```nil | integer```: an error code that is set to ```nil``` on success, or an ```integer``` representing the code used by the underlying library (```OpenSSL``` on Unix, ```bcrypt``` on Windows and ```Security``` framework on macOS / iOS).
102-
* *Remark*: The generated integer can be any valid Lua integer, and such integer can span up to 64 bits. Use this function when you need a potentially large integer. For smaller integers, see [seed](#seed).
111+
* ```integer | nil```: the generated integer on success, or ```nil``` when an error occurred;
112+
* ```nil | integer```: an error code that is set to ```nil``` on success, or an ```integer``` representing the code used by the underlying library (```OpenSSL``` on Unix, ```bcrypt``` on Windows and ```Security``` framework on macOS / iOS).
113+
* *Remark*: The generated integer can be any valid Lua integer, and such integer can span up to 64 bits. Use this function when you need a potentially large integer. For smaller integers, see [take](#take).
103114
* *Usage*:
104115

105116
```lua
@@ -119,8 +130,8 @@ luarocks install lua-cryptorandom
119130
* *Description*: Generates a random float number
120131
* *Signature*: ```number()```
121132
* *Return*: ```number | nil``` as first value, and ```nil | integer``` as the second.
122-
1. ```number | nil```: the generated float number on success, or ```nil``` when an error occurred;
123-
2. ```nil | integer```: an error code that is set to ```nil``` on success, or an ```integer``` representing the code used by the underlying library (```OpenSSL``` on Unix, ```bcrypt``` on Windows and ```Security``` framework on macOS / iOS).
133+
* ```number | nil```: the generated float number on success, or ```nil``` when an error occurred;
134+
* ```nil | integer```: an error code that is set to ```nil``` on success, or an ```integer``` representing the code used by the underlying library (```OpenSSL``` on Unix, ```bcrypt``` on Windows and ```Security``` framework on macOS / iOS).
124135
* *Usage*:
125136

126137
```lua
@@ -135,25 +146,25 @@ luarocks install lua-cryptorandom
135146
end
136147
```
137148

138-
### seed
149+
### take
139150

140-
* *Description*: Generates a random seed integer
141-
* *Signature*: ```seed()```
151+
* *Description*: Generates a random integer that spans at least 16 bits in size, but usually 32 bits in these-days-computers.
152+
* *Signature*: ```take()```
142153
* *Return*: ```integer | nil``` as first value, and ```nil | integer``` as the second.
143-
1. ```integer | nil```: the generated integer on success, or ```nil``` when an error occurred;
144-
2. ```nil | integer```: an error code that is set to ```nil``` on success, or an ```integer``` representing the code used by the underlying library (```OpenSSL``` on Unix, ```bcrypt``` on Windows and ```Security``` framework on macOS / iOS).
154+
* ```integer | nil```: the generated integer on success, or ```nil``` when an error occurred;
155+
* ```nil | integer```: an error code that is set to ```nil``` on success, or an ```integer``` representing the code used by the underlying library (```OpenSSL``` on Unix, ```bcrypt``` on Windows and ```Security``` framework on macOS / iOS).
145156
* *Remark*: The generated integer has, at least, 16 bits in size, but it is usually a 32 bits integer in these-day-computers. The returned integer has the ```int``` data type in C. To generate potentially large integers, see [integer](#integer).
146157
* *Usage*:
147158

148159
```lua
149160
local random = require("lua-cryptorandom")
150161

151-
local seed, err = random.seed()
162+
local take, err = random.take()
152163

153-
if (seed == nil) then
164+
if (take == nil) then
154165
print("error code: ", err)
155166
else
156-
print("seed: ", seed)
167+
print("take: ", take)
157168
end
158169
```
159170

rockspecs/lua-cryptorandom-0.0.1-1.rockspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ source = {
88

99
description = {
1010
homepage = "https://github.com/luau-project/lua-cryptorandom",
11-
summary = [[Generate cryptographically-secure pseudo-random bytes and numbers for Lua]],
11+
summary = [[Generate cryptographically secure pseudo random numbers for Lua]],
1212
detailed = [=[
13-
lua-cryptorandom is a lightweight, native library for Lua aimed to generate cryptographically-secure pseudo-random bytes and numbers, using trusted sources of randomness provided by the operating system.
13+
lua-cryptorandom is a lightweight, native library for Lua aimed to generate cryptographically secure pseudo random numbers, using trusted sources of randomness provided by the operating system.
1414
1515
Visit the repository for more information.]=],
1616
license = "MIT"

rockspecs/lua-cryptorandom-dev-1.rockspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ source = {
77

88
description = {
99
homepage = "https://github.com/luau-project/lua-cryptorandom",
10-
summary = [[Generate cryptographically-secure pseudo-random bytes and numbers for Lua]],
10+
summary = [[Generate cryptographically secure pseudo random numbers for Lua]],
1111
detailed = [=[
12-
lua-cryptorandom is a lightweight, native library for Lua aimed to generate cryptographically-secure pseudo-random bytes and numbers, using trusted sources of randomness provided by the operating system.
12+
lua-cryptorandom is a lightweight, native library for Lua aimed to generate cryptographically secure pseudo random numbers, using trusted sources of randomness provided by the operating system.
1313
1414
Visit the repository for more information.]=],
1515
license = "MIT"

samples/readonly.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
local random = require("lua-cryptorandom")
22

3+
-- all the fields of this library are read-only
4+
35
if (not pcall(function() random.bytes = 0 end)) then
46
print("Not allowed to change bytes function")
57
end
68

7-
if (not pcall(function() random.seed = 0 end)) then
8-
print("Not allowed to change seed function")
9+
if (not pcall(function() random.take = 0 end)) then
10+
print("Not allowed to change take function")
911
end
1012

1113
if (not pcall(function() random.integer = 0 end)) then
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
local random = require("lua-cryptorandom")
22

3-
local seed, err = random.seed()
3+
local take, err = random.take()
44

5-
if (seed == nil) then
5+
if (take == nil) then
66
print("error code: ", err)
77
else
8-
print("seed: ", seed)
8+
print("take: ", take)
99
end

src/lua-cryptorandom.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static int lua_cryptorandom_bytes(lua_State *L)
9090
return 2;
9191
}
9292

93-
static int lua_cryptorandom_seed(lua_State *L)
93+
static int lua_cryptorandom_take(lua_State *L)
9494
{
9595
unsigned char buffer[sizeof(int)];
9696

@@ -102,8 +102,8 @@ static int lua_cryptorandom_seed(lua_State *L)
102102
}
103103
else
104104
{
105-
int *seed_ptr = (int *)buffer;
106-
lua_pushinteger(L, *seed_ptr);
105+
int *take_ptr = (int *)buffer;
106+
lua_pushinteger(L, *take_ptr);
107107
lua_pushnil(L);
108108
}
109109

@@ -122,8 +122,8 @@ static int lua_cryptorandom_integer(lua_State *L)
122122
}
123123
else
124124
{
125-
lua_Integer *seed_ptr = (lua_Integer *)buffer;
126-
lua_pushinteger(L, *seed_ptr);
125+
lua_Integer *take_ptr = (lua_Integer *)buffer;
126+
lua_pushinteger(L, *take_ptr);
127127
lua_pushnil(L);
128128
}
129129

@@ -142,8 +142,8 @@ static int lua_cryptorandom_number(lua_State *L)
142142
}
143143
else
144144
{
145-
lua_Number *seed_ptr = (lua_Number *)buffer;
146-
lua_pushnumber(L, *seed_ptr);
145+
lua_Number *take_ptr = (lua_Number *)buffer;
146+
lua_pushnumber(L, *take_ptr);
147147
lua_pushnil(L);
148148
}
149149

@@ -158,7 +158,7 @@ static int lua_cryptorandom_new_index(lua_State *L)
158158

159159
static const luaL_Reg lua_cryptorandom_public_functions[] = {
160160
{"bytes", lua_cryptorandom_bytes},
161-
{"seed", lua_cryptorandom_seed},
161+
{"take", lua_cryptorandom_take},
162162
{"integer", lua_cryptorandom_integer},
163163
{"number", lua_cryptorandom_number},
164164
{NULL, NULL}

0 commit comments

Comments
 (0)