passgen.c 5.8 KB

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