forked from KolibriOS/kolibrios
258 lines
9.5 KiB
C
258 lines
9.5 KiB
C
|
/* Copyright (C) 2021- Rustem Gimadutdinov (rgimad), GPLv2 */
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <time.h>
|
||
|
#include <kos32sys1.h>
|
||
|
#include <clayer/boxlib.h>
|
||
|
|
||
|
#define WINDOW_WIDTH 400
|
||
|
#define WINDOW_HEIGHT 180
|
||
|
#define EDIT_BOX_PASSWORD_LEN_MAXLEN 16
|
||
|
#define EDIT_BOX_PASSWORD_GENERATED_MAXLEN 256
|
||
|
#define FONT_SIZE_DEFAULT 0x10000000
|
||
|
#define CHECKBOX_ENABLED 2
|
||
|
|
||
|
const char WINDOW_TITLE[] = "PasswordGen v0.1";
|
||
|
|
||
|
struct kolibri_system_colors sys_color_table;
|
||
|
const color_t DRAWTEXT_FLAG_DEFAULT = 0x90000000;
|
||
|
|
||
|
edit_box *edit_box_password_len;
|
||
|
char edit_box_password_len_buf[EDIT_BOX_PASSWORD_LEN_MAXLEN];
|
||
|
|
||
|
edit_box *edit_box_password_generated;
|
||
|
char edit_box_password_generated_buf[EDIT_BOX_PASSWORD_GENERATED_MAXLEN];
|
||
|
|
||
|
check_box *check_box_az;
|
||
|
check_box *check_box_AZ;
|
||
|
check_box *check_box_09;
|
||
|
check_box *check_box_spec_char;
|
||
|
|
||
|
enum MYCOLORS {
|
||
|
GREEN = 0x067D06,
|
||
|
BLUE = 0x0000FF,
|
||
|
RED = 0xFF0000,
|
||
|
BLACK = 0x000000,
|
||
|
WHITE = 0xFFFFFF,
|
||
|
GREY = 0x919191
|
||
|
};
|
||
|
|
||
|
enum BUTTONS {
|
||
|
BTN_QUIT = 1,
|
||
|
BTN_GENERATE = 10,
|
||
|
BTN_COPY = 20
|
||
|
//
|
||
|
};
|
||
|
|
||
|
const char char_set_az[] = "abcdefghijklmnopqrstuvwxyz";
|
||
|
const char char_set_AZ[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||
|
const char char_set_09[] = "0123456789";
|
||
|
const char char_set_spec_char[] = "!@#$%^&*_";
|
||
|
|
||
|
char resulting_char_set[75];
|
||
|
char password_generated[EDIT_BOX_PASSWORD_GENERATED_MAXLEN];
|
||
|
|
||
|
/* ---------------------------------------------------------- */
|
||
|
|
||
|
void generate_password_internal(char *dst, int length, int flag_az, int flag_AZ, int flag_09, int flag_spec_char) {
|
||
|
int i, resulting_char_set_len;
|
||
|
resulting_char_set[0] = '\0';
|
||
|
if (flag_az == 0 && flag_AZ == 0 && flag_09 == 0 && flag_spec_char == 0) { return; }
|
||
|
if (flag_az) { strcat(resulting_char_set, char_set_az); }
|
||
|
if (flag_AZ) { strcat(resulting_char_set, char_set_AZ); }
|
||
|
if (flag_09) { strcat(resulting_char_set, char_set_09); }
|
||
|
if (flag_spec_char) { strcat(resulting_char_set, char_set_spec_char); }
|
||
|
resulting_char_set_len = strlen(resulting_char_set);
|
||
|
//printf("resulting_char_set = %s\n", resulting_char_set
|
||
|
for (i = 0; i < length; i++) {
|
||
|
dst[i] = resulting_char_set[rand() % resulting_char_set_len];
|
||
|
}
|
||
|
dst[length] = '\0';
|
||
|
}
|
||
|
|
||
|
void notify_show(char *text) {
|
||
|
start_app("/sys/@notify", text);
|
||
|
}
|
||
|
|
||
|
void* safe_malloc(size_t size) {
|
||
|
void *p = malloc(size);
|
||
|
if (p == NULL) {
|
||
|
notify_show("'Memory allocation error!' -E");
|
||
|
exit(0);
|
||
|
} else {
|
||
|
return p;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void copy_to_clipboard(char *text) {
|
||
|
char *temp_buffer = safe_malloc(EDIT_BOX_PASSWORD_GENERATED_MAXLEN + 12);
|
||
|
memset(temp_buffer, 0, EDIT_BOX_PASSWORD_GENERATED_MAXLEN);
|
||
|
DATA(char,temp_buffer,4) = 0; /* TEXT */
|
||
|
DATA(char,temp_buffer,8) = 1; /* CP866 */
|
||
|
strncpy(temp_buffer+12, text, EDIT_BOX_PASSWORD_GENERATED_MAXLEN - 1);
|
||
|
kol_clip_set(strlen(text) + 12, temp_buffer);
|
||
|
notify_show("'Copied to clipboard!' -I");
|
||
|
free(temp_buffer);
|
||
|
}
|
||
|
|
||
|
edit_box* create_edit_box(unsigned int width, unsigned int left, unsigned int top,
|
||
|
unsigned int color, unsigned int shift_color, unsigned int focus_border_color,
|
||
|
unsigned int blur_border_color, unsigned int text_color, unsigned int max,
|
||
|
char *text, void *mouse_variable, unsigned int flags)
|
||
|
{
|
||
|
edit_box *eb = (edit_box*)safe_malloc(sizeof(edit_box));
|
||
|
memset(eb, 0, sizeof(edit_box));
|
||
|
eb->width = width;
|
||
|
eb->left = left;
|
||
|
eb->top = top;
|
||
|
eb->color = color;
|
||
|
eb->shift_color = shift_color;
|
||
|
eb->focus_border_color = focus_border_color;
|
||
|
eb->blur_border_color = blur_border_color;
|
||
|
eb->text_color = text_color;
|
||
|
eb->max = max;
|
||
|
eb->text = text;
|
||
|
eb->mouse_variable = mouse_variable;
|
||
|
eb->flags = flags;
|
||
|
return eb;
|
||
|
}
|
||
|
|
||
|
check_box* create_check_box(unsigned int left_s, unsigned int top_s, unsigned int ch_text_margin,
|
||
|
unsigned int color, unsigned int border_color, unsigned int text_color, char *text, unsigned int flags)
|
||
|
{
|
||
|
check_box *cb = (check_box*)safe_malloc(sizeof(check_box));
|
||
|
memset(cb, 0, sizeof(check_box));
|
||
|
cb->left_s = left_s;
|
||
|
cb->top_s = top_s;
|
||
|
cb->ch_text_margin = ch_text_margin;
|
||
|
cb->color = color;
|
||
|
cb->border_color = border_color;
|
||
|
cb->text_color = text_color;
|
||
|
cb->text = text;
|
||
|
cb->flags = flags;
|
||
|
return cb;
|
||
|
}
|
||
|
/* ---------------------------------------------------------- */
|
||
|
|
||
|
void redraw_window() {
|
||
|
pos_t win_pos = get_mouse_pos(0);
|
||
|
begin_draw();
|
||
|
sys_create_window(win_pos.x, win_pos.y, WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, sys_color_table.work_area, 0x14);
|
||
|
|
||
|
edit_box_draw(edit_box_password_len);
|
||
|
|
||
|
draw_text_sys("Password length: ", 15, 34 + 5, 0, DRAWTEXT_FLAG_DEFAULT | sys_color_table.work_text);
|
||
|
draw_text_sys("Characters: ", 15, 68, 0, DRAWTEXT_FLAG_DEFAULT | sys_color_table.work_text);
|
||
|
draw_text_sys("Generated password: ", 15, 102 + 5, 0, DRAWTEXT_FLAG_DEFAULT | sys_color_table.work_text);
|
||
|
|
||
|
check_box_draw2(check_box_az);
|
||
|
check_box_draw2(check_box_AZ);
|
||
|
check_box_draw2(check_box_09);
|
||
|
check_box_draw2(check_box_spec_char);
|
||
|
|
||
|
edit_box_draw(edit_box_password_generated);
|
||
|
|
||
|
define_button(X_W(170,100), Y_H(136,30), BTN_GENERATE, sys_color_table.work_button);
|
||
|
draw_text_sys("Generate!", 182, 136 + 7, 0, DRAWTEXT_FLAG_DEFAULT | sys_color_table.work_button_text);
|
||
|
|
||
|
define_button(X_W(370 - 60, 60), Y_H(136,30), BTN_COPY, sys_color_table.work_button);
|
||
|
draw_text_sys("Copy", 370 - 60 + 15, 136 + 7, 0, DRAWTEXT_FLAG_DEFAULT | sys_color_table.work_button_text);
|
||
|
|
||
|
end_draw();
|
||
|
}
|
||
|
|
||
|
/* create and initialize components */
|
||
|
void create_components() {
|
||
|
edit_box_password_len = create_edit_box(70, 150, 34, WHITE, sys_color_table.work_button, 0, GREY, FONT_SIZE_DEFAULT, EDIT_BOX_PASSWORD_LEN_MAXLEN - 2, edit_box_password_len_buf, NULL, 0/*ed_focus*/);
|
||
|
edit_box_set_text(edit_box_password_len, "10");
|
||
|
|
||
|
check_box_az = create_check_box(X_W(110, 15), Y_H(68,15), 10, WHITE, BLUE, BLACK | FONT_SIZE_DEFAULT, "a-z", CHECKBOX_ENABLED);
|
||
|
init_checkbox2(check_box_az);
|
||
|
|
||
|
check_box_AZ = create_check_box(X_W(170, 15), Y_H(68,15), 10, WHITE, BLUE, BLACK | FONT_SIZE_DEFAULT, "A-Z", CHECKBOX_ENABLED);
|
||
|
init_checkbox2(check_box_AZ);
|
||
|
|
||
|
check_box_09 = create_check_box(X_W(230, 15), Y_H(68,15), 10, WHITE, BLUE, BLACK | FONT_SIZE_DEFAULT, "0-9", CHECKBOX_ENABLED);
|
||
|
init_checkbox2(check_box_09);
|
||
|
|
||
|
check_box_spec_char = create_check_box(X_W(290, 15), Y_H(68,15), 10, WHITE, BLUE, BLACK | FONT_SIZE_DEFAULT, "!@#$%^&*_", 0);
|
||
|
init_checkbox2(check_box_spec_char);
|
||
|
|
||
|
edit_box_password_generated = create_edit_box(200, 170, 102, WHITE, sys_color_table.work_button, 0, GREY, FONT_SIZE_DEFAULT, EDIT_BOX_PASSWORD_GENERATED_MAXLEN - 2, edit_box_password_generated_buf, NULL, 0/*ed_focus*/);
|
||
|
}
|
||
|
|
||
|
void generate_and_show_password() {
|
||
|
int f_az, f_AZ, f_09, f_spec, psw_len = atoi(edit_box_password_len_buf);
|
||
|
if (psw_len == 0 || psw_len >= EDIT_BOX_PASSWORD_GENERATED_MAXLEN - 2) {
|
||
|
notify_show("'Incorrect password length' -E");
|
||
|
} else {
|
||
|
f_az = check_box_az->flags & CHECKBOX_ENABLED;
|
||
|
f_AZ = check_box_AZ->flags & CHECKBOX_ENABLED;
|
||
|
f_09 = check_box_09->flags & CHECKBOX_ENABLED;
|
||
|
f_spec = check_box_spec_char->flags & CHECKBOX_ENABLED;
|
||
|
if (f_az == 0 && f_AZ == 0 && f_09 == 0 && f_spec == 0) {
|
||
|
notify_show("'You must choose at least one character set' -E");
|
||
|
return;
|
||
|
}
|
||
|
generate_password_internal(password_generated, psw_len, f_az, f_AZ, f_09, f_spec);
|
||
|
edit_box_set_text(edit_box_password_generated, password_generated);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main(int argc, const char *argv[]) {
|
||
|
int gui_event; /* variable for storing event */
|
||
|
uint32_t pressed_button = 0; /* code of button pressed in window */
|
||
|
unsigned int keyval; /* for saving pressed key */
|
||
|
|
||
|
srand(time(0)*2/3); /* seeding the pseudo random number generator*/
|
||
|
kolibri_boxlib_init();
|
||
|
get_system_colors(&sys_color_table);
|
||
|
set_event_mask(0xC0000027);
|
||
|
create_components(); /* create and init some visual components */
|
||
|
|
||
|
do
|
||
|
{
|
||
|
gui_event = get_os_event();
|
||
|
switch(gui_event)
|
||
|
{
|
||
|
case KOLIBRI_EVENT_NONE:
|
||
|
break;
|
||
|
case KOLIBRI_EVENT_REDRAW:
|
||
|
redraw_window();
|
||
|
break;
|
||
|
case KOLIBRI_EVENT_MOUSE:
|
||
|
edit_box_mouse(edit_box_password_len);
|
||
|
check_box_mouse2(check_box_az);
|
||
|
check_box_mouse2(check_box_AZ);
|
||
|
check_box_mouse2(check_box_09);
|
||
|
check_box_mouse2(check_box_spec_char);
|
||
|
edit_box_mouse(edit_box_password_generated);
|
||
|
break;
|
||
|
case KOLIBRI_EVENT_KEY:
|
||
|
keyval = get_key().val;
|
||
|
edit_box_key(edit_box_password_len, keyval);
|
||
|
edit_box_key(edit_box_password_generated, keyval);
|
||
|
break;
|
||
|
case KOLIBRI_EVENT_BUTTON:
|
||
|
pressed_button = get_os_button();
|
||
|
switch (pressed_button)
|
||
|
{
|
||
|
case BTN_GENERATE:
|
||
|
generate_and_show_password();
|
||
|
redraw_window();
|
||
|
break;
|
||
|
|
||
|
case BTN_COPY:
|
||
|
copy_to_clipboard(edit_box_password_generated_buf);
|
||
|
redraw_window();
|
||
|
break;
|
||
|
|
||
|
case BTN_QUIT:
|
||
|
exit(0);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
} while(1);
|
||
|
exit(0);
|
||
|
}
|