unix,windows: Factor out code that generates random bytes to a new func.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2022-06-02 17:05:57 +10:00
parent 5bb2a85d74
commit d2f018bff1
4 changed files with 30 additions and 28 deletions

View File

@@ -32,6 +32,7 @@
#include <sys/time.h>
#include <windows.h>
#include <unistd.h>
#include <bcrypt.h>
HANDLE std_in = NULL;
HANDLE con_out = NULL;
@@ -286,3 +287,10 @@ void mp_hal_delay_ms(mp_uint_t ms) {
msec_sleep((double)ms);
#endif
}
void mp_hal_get_random(size_t n, void *buf) {
NTSTATUS result = BCryptGenRandom(NULL, (unsigned char *)buf, n, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
if (!BCRYPT_SUCCESS(result)) {
mp_raise_OSError(errno);
}
}