Added a function to generate a random 6 digit number, this number could be used as the bluetooth pairing code

This commit is contained in:
Th3maz1ng 2023-04-22 13:20:15 +02:00
parent 2206b6f2fb
commit a550c52c39
2 changed files with 28 additions and 1 deletions

View File

@ -1,5 +1,6 @@
#include "app_utils.h" #include "app_utils.h"
#include "app_log.h" #include "app_log.h"
#include "wm_crypto_hard.h""
static uint32_t _elapsed_ms = 0; static uint32_t _elapsed_ms = 0;
@ -70,3 +71,22 @@ uint32_t elapsed_ms(void)
{ {
return _elapsed_ms; return _elapsed_ms;
} }
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;
}

View File

@ -31,4 +31,11 @@ void ms_increment(uint32_t increment);
*/ */
uint32_t elapsed_ms(void); uint32_t elapsed_ms(void);
/**
* @brief Generates a 6 digit random number using the hardware RNG IP.
*
* @return uint32_t the 6 digit random number generated
*/
uint32_t random_gen_6_digit(void);
#endif //APP_UTILS_H #endif //APP_UTILS_H