passgen.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <gui/elements.h>
  4. #include <input/input.h>
  5. #include <notification/notification_messages.h>
  6. #include <stdlib.h>
  7. #include <passgen_icons.h>
  8. #include <core/string.h>
  9. #define PASSGEN_MAX_LENGTH 16
  10. #define PASSGEN_DIGITS "0123456789"
  11. #define PASSGEN_LETTERS_LOW "abcdefghijklmnopqrstuvwxyz"
  12. #define PASSGEN_LETTERS_UP "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  13. #define PASSGEN_SPECIAL "!#$%%^&*.-_"
  14. typedef enum PassGen_Alphabet {
  15. Digits = 1,
  16. Lowercase = 2,
  17. Uppercase = 4,
  18. Special = 8,
  19. DigitsLower = Digits | Lowercase,
  20. DigitsAllLetters = Digits | Lowercase | Uppercase,
  21. Mixed = DigitsAllLetters | Special
  22. } PassGen_Alphabet;
  23. const int AlphabetLevels[] = {Digits, Lowercase, DigitsLower, DigitsAllLetters, Mixed};
  24. const char* AlphabetLevelNames[] = {"1234", "abcd", "ab12", "Ab12", "Ab1#"};
  25. const int AlphabetLevelsCount = sizeof(AlphabetLevels) / sizeof(int);
  26. const NotificationSequence PassGen_Alert_vibro = {
  27. &message_vibro_on,
  28. &message_blue_255,
  29. &message_delay_50,
  30. &message_vibro_off,
  31. NULL,
  32. };
  33. typedef struct {
  34. FuriMessageQueue* input_queue;
  35. ViewPort* view_port;
  36. Gui* gui;
  37. FuriMutex** mutex;
  38. NotificationApp* notify;
  39. char password[PASSGEN_MAX_LENGTH + 1];
  40. // char alphabet[PASSGEN_CHARACTERS_LENGTH + 1];
  41. FuriString* alphabet;
  42. int length;
  43. int level;
  44. } PassGen;
  45. void state_free(PassGen* app) {
  46. gui_remove_view_port(app->gui, app->view_port);
  47. furi_record_close(RECORD_GUI);
  48. view_port_free(app->view_port);
  49. furi_message_queue_free(app->input_queue);
  50. furi_mutex_free(app->mutex);
  51. furi_record_close(RECORD_NOTIFICATION);
  52. furi_string_free(app->alphabet);
  53. free(app);
  54. }
  55. static void input_callback(InputEvent* input_event, void* ctx) {
  56. PassGen* app = ctx;
  57. if(input_event->type == InputTypeShort) {
  58. furi_message_queue_put(app->input_queue, input_event, 0);
  59. }
  60. }
  61. static void render_callback(Canvas* canvas, void* ctx) {
  62. char str_length[8];
  63. PassGen* app = ctx;
  64. furi_check(furi_mutex_acquire(app->mutex, FuriWaitForever) == FuriStatusOk);
  65. canvas_clear(canvas);
  66. canvas_draw_box(canvas, 0, 0, 128, 14);
  67. canvas_set_color(canvas, ColorWhite);
  68. canvas_set_font(canvas, FontPrimary);
  69. canvas_draw_str(canvas, 2, 11, "Password Generator");
  70. canvas_set_color(canvas, ColorBlack);
  71. canvas_draw_str_aligned(canvas, 64, 35, AlignCenter, AlignCenter, app->password);
  72. // Navigation menu:
  73. canvas_set_font(canvas, FontSecondary);
  74. canvas_draw_icon(canvas, 96, 52, &I_Pin_back_arrow_10x8);
  75. canvas_draw_str(canvas, 108, 60, "Exit");
  76. canvas_draw_icon(canvas, 54, 52, &I_Vertical_arrow_7x9);
  77. canvas_draw_str(canvas, 64, 60, AlphabetLevelNames[app->level]);
  78. snprintf(str_length, sizeof(str_length), "Len: %d", app->length);
  79. canvas_draw_icon(canvas, 4, 53, &I_Horizontal_arrow_9x7);
  80. canvas_draw_str(canvas, 15, 60, str_length);
  81. furi_mutex_release(app->mutex);
  82. }
  83. void build_alphabet(PassGen* app) {
  84. PassGen_Alphabet mode = AlphabetLevels[app->level];
  85. if((mode & Digits) != 0) furi_string_cat(app->alphabet, PASSGEN_DIGITS);
  86. if((mode & Lowercase) != 0) furi_string_cat(app->alphabet, PASSGEN_LETTERS_LOW);
  87. if((mode & Uppercase) != 0) furi_string_cat(app->alphabet, PASSGEN_LETTERS_UP);
  88. if((mode & Special) != 0) furi_string_cat(app->alphabet, PASSGEN_SPECIAL);
  89. }
  90. PassGen* state_init() {
  91. PassGen* app = malloc(sizeof(PassGen));
  92. app->length = 8;
  93. app->level = 2;
  94. app->alphabet = furi_string_alloc();
  95. build_alphabet(app);
  96. app->input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  97. app->view_port = view_port_alloc();
  98. app->gui = furi_record_open(RECORD_GUI);
  99. app->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  100. view_port_input_callback_set(app->view_port, input_callback, app);
  101. view_port_draw_callback_set(app->view_port, render_callback, app);
  102. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  103. app->notify = furi_record_open(RECORD_NOTIFICATION);
  104. return app;
  105. }
  106. void generate(PassGen* app) {
  107. int hi = furi_string_size(app->alphabet);
  108. for(int i = 0; i < app->length; i++) {
  109. int x = rand() % hi;
  110. app->password[i] = furi_string_get_char(app->alphabet, x);
  111. }
  112. app->password[app->length] = '\0';
  113. }
  114. void update_password(PassGen* app, bool vibro) {
  115. generate(app);
  116. if(vibro)
  117. notification_message(app->notify, &PassGen_Alert_vibro);
  118. else
  119. notification_message(app->notify, &sequence_blink_blue_100);
  120. view_port_update(app->view_port);
  121. }
  122. int32_t passgenapp(void) {
  123. PassGen* app = state_init();
  124. generate(app);
  125. while(1) {
  126. InputEvent input;
  127. while(furi_message_queue_get(app->input_queue, &input, FuriWaitForever) == FuriStatusOk) {
  128. furi_check(furi_mutex_acquire(app->mutex, FuriWaitForever) == FuriStatusOk);
  129. if(input.type == InputTypeShort) {
  130. switch(input.key) {
  131. case InputKeyBack:
  132. furi_mutex_release(app->mutex);
  133. state_free(app);
  134. return 0;
  135. case InputKeyDown:
  136. if(app->level > 0) {
  137. app->level--;
  138. build_alphabet(app);
  139. update_password(app, false);
  140. } else
  141. notification_message(app->notify, &sequence_blink_red_100);
  142. break;
  143. case InputKeyUp:
  144. if(app->level < AlphabetLevelsCount - 1) {
  145. app->level++;
  146. build_alphabet(app);
  147. update_password(app, false);
  148. } else
  149. notification_message(app->notify, &sequence_blink_red_100);
  150. break;
  151. case InputKeyLeft:
  152. if(app->length > 1) {
  153. app->length--;
  154. update_password(app, false);
  155. } else
  156. notification_message(app->notify, &sequence_blink_red_100);
  157. break;
  158. case InputKeyRight:
  159. if(app->length < PASSGEN_MAX_LENGTH) {
  160. app->length++;
  161. update_password(app, false);
  162. } else
  163. notification_message(app->notify, &sequence_blink_red_100);
  164. break;
  165. case InputKeyOk:
  166. update_password(app, true);
  167. break;
  168. default:
  169. break;
  170. }
  171. }
  172. furi_mutex_release(app->mutex);
  173. }
  174. }
  175. state_free(app);
  176. return 0;
  177. }