passgen.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #define PASSGEN_MAX_LENGTH 16
  9. #define PASSGEN_CHARACTERS_LENGTH (26 * 4)
  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. int length;
  42. int level;
  43. } PassGen;
  44. void state_free(PassGen* app) {
  45. gui_remove_view_port(app->gui, app->view_port);
  46. furi_record_close(RECORD_GUI);
  47. view_port_free(app->view_port);
  48. furi_message_queue_free(app->input_queue);
  49. furi_mutex_free(app->mutex);
  50. furi_record_close(RECORD_NOTIFICATION);
  51. free(app);
  52. }
  53. static void input_callback(InputEvent* input_event, void* ctx) {
  54. PassGen* app = ctx;
  55. if(input_event->type == InputTypeShort) {
  56. furi_message_queue_put(app->input_queue, input_event, 0);
  57. }
  58. }
  59. static void render_callback(Canvas* canvas, void* ctx) {
  60. char str_length[8];
  61. PassGen* app = ctx;
  62. furi_check(furi_mutex_acquire(app->mutex, FuriWaitForever) == FuriStatusOk);
  63. canvas_clear(canvas);
  64. canvas_draw_box(canvas, 0, 0, 128, 14);
  65. canvas_set_color(canvas, ColorWhite);
  66. canvas_set_font(canvas, FontPrimary);
  67. canvas_draw_str(canvas, 2, 11, "Password Generator");
  68. canvas_set_color(canvas, ColorBlack);
  69. canvas_draw_str_aligned(canvas, 64, 35, AlignCenter, AlignCenter, app->password);
  70. // Navigation menu:
  71. canvas_set_font(canvas, FontSecondary);
  72. canvas_draw_icon(canvas, 96, 52, &I_Pin_back_arrow_10x8);
  73. canvas_draw_str(canvas, 108, 60, "Exit");
  74. canvas_draw_icon(canvas, 54, 52, &I_Vertical_arrow_7x9);
  75. canvas_draw_str(canvas, 64, 60, AlphabetLevelNames[app->level]);
  76. snprintf(str_length, sizeof(str_length), "Len: %d", app->length);
  77. canvas_draw_icon(canvas, 4, 53, &I_Horizontal_arrow_9x7);
  78. canvas_draw_str(canvas, 15, 60, str_length);
  79. furi_mutex_release(app->mutex);
  80. }
  81. void build_alphabet(PassGen* app) {
  82. PassGen_Alphabet mode = AlphabetLevels[app->level];
  83. app->alphabet[0] = '\0';
  84. if((mode & Digits) != 0) strcat(app->alphabet, PASSGEN_DIGITS);
  85. if((mode & Lowercase) != 0) strcat(app->alphabet, PASSGEN_LETTERS_LOW);
  86. if((mode & Uppercase) != 0) strcat(app->alphabet, PASSGEN_LETTERS_UP);
  87. if((mode & Special) != 0) strcat(app->alphabet, PASSGEN_SPECIAL);
  88. }
  89. PassGen* state_init() {
  90. PassGen* app = malloc(sizeof(PassGen));
  91. app->length = 8;
  92. app->level = 2;
  93. build_alphabet(app);
  94. app->input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  95. app->view_port = view_port_alloc();
  96. app->gui = furi_record_open(RECORD_GUI);
  97. app->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  98. view_port_input_callback_set(app->view_port, input_callback, app);
  99. view_port_draw_callback_set(app->view_port, render_callback, app);
  100. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  101. app->notify = furi_record_open(RECORD_NOTIFICATION);
  102. return app;
  103. }
  104. void generate(PassGen* app) {
  105. int hi = strlen(app->alphabet);
  106. for(int i = 0; i < app->length; i++) {
  107. int x = rand() % hi;
  108. app->password[i] = app->alphabet[x];
  109. }
  110. app->password[app->length] = '\0';
  111. }
  112. void update_password(PassGen* app, bool vibro) {
  113. generate(app);
  114. if(vibro)
  115. notification_message(app->notify, &PassGen_Alert_vibro);
  116. else
  117. notification_message(app->notify, &sequence_blink_blue_100);
  118. view_port_update(app->view_port);
  119. }
  120. int32_t passgenapp(void) {
  121. PassGen* app = state_init();
  122. generate(app);
  123. while(1) {
  124. InputEvent input;
  125. while(furi_message_queue_get(app->input_queue, &input, FuriWaitForever) == FuriStatusOk) {
  126. furi_check(furi_mutex_acquire(app->mutex, FuriWaitForever) == FuriStatusOk);
  127. if(input.type == InputTypeShort) {
  128. switch(input.key) {
  129. case InputKeyBack:
  130. furi_mutex_release(app->mutex);
  131. state_free(app);
  132. return 0;
  133. case InputKeyDown:
  134. if(app->level > 0) {
  135. app->level--;
  136. build_alphabet(app);
  137. update_password(app, false);
  138. } else
  139. notification_message(app->notify, &sequence_blink_red_100);
  140. break;
  141. case InputKeyUp:
  142. if(app->level < AlphabetLevelsCount - 1) {
  143. app->level++;
  144. build_alphabet(app);
  145. update_password(app, false);
  146. } else
  147. notification_message(app->notify, &sequence_blink_red_100);
  148. break;
  149. case InputKeyLeft:
  150. if(app->length > 1) {
  151. app->length--;
  152. update_password(app, false);
  153. } else
  154. notification_message(app->notify, &sequence_blink_red_100);
  155. break;
  156. case InputKeyRight:
  157. if(app->length < PASSGEN_MAX_LENGTH) {
  158. app->length++;
  159. update_password(app, false);
  160. } else
  161. notification_message(app->notify, &sequence_blink_red_100);
  162. break;
  163. case InputKeyOk:
  164. update_password(app, true);
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. furi_mutex_release(app->mutex);
  171. }
  172. }
  173. state_free(app);
  174. return 0;
  175. }