xref: /PHP-8.2/ext/random/engine_secure.c (revision 3331832b)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Sammy Kaye Powers <me@sammyk.me>                            |
14    |          Go Kudo <zeriyoshi@php.net>                                 |
15    +----------------------------------------------------------------------+
16 */
17 
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 
22 #include "php.h"
23 #include "php_random.h"
24 
25 #include "Zend/zend_exceptions.h"
26 
generate(php_random_status * status)27 static uint64_t generate(php_random_status *status)
28 {
29 	zend_ulong r = 0;
30 
31 	php_random_bytes_throw(&r, sizeof(zend_ulong));
32 
33 	return r;
34 }
35 
range(php_random_status * status,zend_long min,zend_long max)36 static zend_long range(php_random_status *status, zend_long min, zend_long max)
37 {
38 	zend_long result = 0;
39 
40 	php_random_int_throw(min, max, &result);
41 
42 	return result;
43 }
44 
45 const php_random_algo php_random_algo_secure = {
46 	sizeof(zend_ulong),
47 	0,
48 	NULL,
49 	generate,
50 	range,
51 	NULL,
52 	NULL
53 };
54