From 18b602bbca2d283e7ca5ec9194d9ed3ef9cc52c6 Mon Sep 17 00:00:00 2001 From: Th3maz1ng Date: Sun, 23 Apr 2023 20:29:42 +0200 Subject: [PATCH] Implemented a random number generating function which returns a 6 digit random number --- app/app_lib/app_utils.c | 21 +++++++++++++++++++++ app/app_lib/app_utils.h | 2 ++ 2 files changed, 23 insertions(+) diff --git a/app/app_lib/app_utils.c b/app/app_lib/app_utils.c index ce983a6..308a531 100644 --- a/app/app_lib/app_utils.c +++ b/app/app_lib/app_utils.c @@ -1,4 +1,6 @@ #include "app_utils.h" +#include +#include "wm_crypto_hard.h" static uint32_t millis_cnt = 0; @@ -52,4 +54,23 @@ void ms_delay(uint32_t ms) while(tls_timer_read(timer_id) < ms); tls_timer_destroy(timer_id); +} + +uint32_t random_gen_6_digit(void) +{ + unsigned char random_buf[6] = {0}; + uint32_t output_num = 0; + + tls_crypto_random_init(0x19031998, CRYPTO_RNG_SWITCH_16); + tls_crypto_random_bytes(random_buf, sizeof random_buf); + tls_crypto_random_stop(); + + for(uint8_t i = 0; i < sizeof random_buf; i++) + { + // Ensures the last digit is not 0 + if(i == (sizeof random_buf) - 1 && random_buf[i] % 10 == 0)random_buf[i]++; + output_num += (random_buf[i] % 10) * pow(10, i); + } + + return output_num; } \ No newline at end of file diff --git a/app/app_lib/app_utils.h b/app/app_lib/app_utils.h index 2be832c..f339a3c 100644 --- a/app/app_lib/app_utils.h +++ b/app/app_lib/app_utils.h @@ -11,4 +11,6 @@ void us_delay(uint32_t us); void ms_delay(uint32_t ms); +uint32_t random_gen_6_digit(void); + #endif //APP_UTILS_H \ No newline at end of file