Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions builtin-functions/kphp-light/stdlib/math-functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ function levenshtein ($str1 ::: string, $str2 ::: string) ::: int;

function lcg_value() ::: float;

function random_bytes($length ::: int) ::: string | false;

/** @kphp-extern-func-info interruptible */
function uniqid ($prefix ::: string = '', $more_entropy ::: bool = false) ::: string;

Expand All @@ -187,5 +189,3 @@ define('PHP_ROUND_HALF_ODD', 123423146);

/** @kphp-extern-func-info stub generation-required */
function random_int($l ::: int, $r ::: int) ::: int | false;
/** @kphp-extern-func-info stub generation-required */
function random_bytes($length ::: int) ::: string | false;
31 changes: 31 additions & 0 deletions runtime-light/stdlib/math/random-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
#include <random>
#include <utility>

#if defined(__APPLE__)
#include <stdlib.h>
#else
#include <sys/random.h>
#endif

#include "runtime-common/core/runtime-core.h"
#include "runtime-common/stdlib/math/random-functions.h"
#include "runtime-light/coroutine/task.h"
Expand Down Expand Up @@ -83,6 +89,15 @@ inline int64_t lcg_modmult(int64_t a, int64_t b, int64_t c, int64_t m, int64_t s
return res;
}

inline int64_t secure_rand_buf(void* const buf, size_t length) noexcept {
#if defined(__APPLE__)
arc4random_buf(buf, length);
return 0;
#else
return getrandom(buf, length, GRND_NONBLOCK);
#endif
}

// Analogue of unix's `gettimeofday`
// Returns seconds elapsed since Epoch, and milliseconds elapsed from the last second.
inline std::pair<std::chrono::seconds, std::chrono::microseconds> system_seconds_and_micros() noexcept {
Expand Down Expand Up @@ -126,6 +141,22 @@ inline double f$lcg_value() noexcept {
return static_cast<double>(z) * random_impl_::lcg_value_coef;
}

inline Optional<string> f$random_bytes(int64_t length) noexcept {
if (length < 1) [[unlikely]] {
kphp::log::warning("argument #1 ($length) must be greater than 0");
return false;
}

string str{static_cast<string::size_type>(length), false};

if (random_impl_::secure_rand_buf(static_cast<void*>(str.buffer()), static_cast<size_t>(length)) == -1) {
kphp::log::warning("source of randomness cannot be found");
return false;
}

return str;
}

inline kphp::coro::task<string> f$uniqid(string prefix = string{}, bool more_entropy = false) noexcept {
if (!more_entropy) {
co_await f$usleep(1);
Expand Down
2 changes: 1 addition & 1 deletion tests/phpt/dl/385_random_bytes.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ok php8 k2_skip
@ok php8
<?php

function test_random_bytes() {
Expand Down
Loading