An ESP32-based Redis-watcher and info-displayer https://rpjios.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
934 B

  1. #include "zw_otp.h"
  2. #include "zw_logging.h"
  3. extern String gHostname;
  4. // the fudge table is use strictly to make a linear sequence appear
  5. // non-linear over a short observation time period. security by obscurity ftw!
  6. static const uint8_t fudgeTable[] = {42, 69, 3, 18, 25, 12, 51, 93, 54, 76};
  7. static const uint8_t fudgeTableLen = 10;
  8. // to generate anew, use ./scripts/otp-generate.pl
  9. bool otpCheck(uint16_t otp)
  10. {
  11. auto div = (unsigned long)1e6 * 60 * OTP_WINDOW_MINUTES;
  12. auto now = micros();
  13. if (now < div)
  14. {
  15. dprint("Can't calculate OTPs yet, must be running for at least %d minutes\n", OTP_WINDOW_MINUTES);
  16. return false;
  17. }
  18. auto internalChecker = micros() / div;
  19. internalChecker += fudgeTable[internalChecker % fudgeTableLen];
  20. for (int i = 0; i < gHostname.length(); i++)
  21. {
  22. internalChecker += gHostname.charAt(i);
  23. }
  24. return (uint16_t)internalChecker == otp;
  25. }