esp32.h 805 B

1234567891011121314151617181920212223242526272829303132
  1. // Important: RF *must* be activated on ESP board
  2. // https://techtutorialsx.com/2017/12/22/esp32-arduino-random-number-generation/
  3. #ifdef ESP32
  4. #include <esp_system.h>
  5. #endif
  6. #ifdef ARDUINO
  7. #include <Arduino.h>
  8. #endif
  9. static int
  10. hydro_random_init(void)
  11. {
  12. const char ctx[hydro_hash_CONTEXTBYTES] = { 'h', 'y', 'd', 'r', 'o', 'P', 'R', 'G' };
  13. hydro_hash_state st;
  14. uint16_t ebits = 0;
  15. hydro_hash_init(&st, ctx, NULL);
  16. while (ebits < 256) {
  17. uint32_t r = esp_random();
  18. delay(10);
  19. hydro_hash_update(&st, (const uint32_t *) &r, sizeof r);
  20. ebits += 32;
  21. }
  22. hydro_hash_final(&st, hydro_random_context.state, sizeof hydro_random_context.state);
  23. hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
  24. return 0;
  25. }