passgen.c 7.1 KB

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