GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Aug 28, 2014 11:14 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Password generator
PostPosted: Thu Dec 10, 2009 9:40 pm 
Offline

Joined: Thu Dec 10, 2009 9:15 pm
Posts: 1
Hi i am making a password generator based on the concatenation of a master password and a "reminder name".The result is passed through a sha hashing and base64 truncated. The GUI is in GTK and it is aimed at cross platform.

[code=]#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

#define X 0
#define Y 1


static GtkWidget *window, *dialog;
static GtkWidget *entry, *ent_preferences;
static GtkWidget *menu;
static GtkStatusIcon *statusicon;



static gint active = 0;
gchar *master_password;

/* Another callback */
static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}

static gint button_press( GtkWidget *widget,
GdkEvent *event )
{
GdkEventButton *event_button;
gint pos[2];

if (event->type == GDK_BUTTON_PRESS)
{
GtkMenu *menu;
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
menu = GTK_MENU (widget);
event_button = (GdkEventButton *) event;
if (event_button->button == 1)
{
if (active == 0)
{
gboolean dummy;
gint WindowHeight, WindowWidth, WindowPosition[2];
GdkScreen *screen;
gint ScreenHeight, ScreenWidth;

dummy = FALSE;
gtk_widget_show_all(window);
gtk_window_present(GTK_WINDOW(window));
gtk_status_icon_position_menu(menu, &pos[X], &pos[Y], &dummy, statusicon);
gtk_window_move(GTK_WINDOW(window), pos[X],pos[Y]);

gdk_drawable_get_size(window->window ,&WindowWidth, &WindowHeight);

screen = gtk_status_icon_get_screen(statusicon);
ScreenWidth = gdk_screen_get_width(screen);
ScreenHeight = gdk_screen_get_height(screen);
g_print("Screen: %d, %d\nGeometry: %d, %d\n",ScreenWidth, ScreenHeight, WindowWidth, window->allocation.height);

gtk_entry_set_text(GTK_ENTRY(entry),"");
active = 1;
return TRUE;
}
else if(active == 1)
{
gtk_entry_set_text(GTK_ENTRY(entry),"");
gtk_widget_hide(window);
active = 0;
return TRUE;
}
else
return FALSE;
}
else if(event_button->button == 3)
{
if (active == 1)
{
gtk_entry_set_text(GTK_ENTRY(entry),"");
gtk_widget_hide(window);
active = 0;
}
gtk_menu_popup (menu, NULL, NULL, NULL, NULL,
event_button->button, event_button->time);
return TRUE;
}
}
return FALSE;
}
static gint key_press( GtkWidget *widget, GdkEvent *event )
{
GdkEventKey *event_key;
if(event->type == GDK_KEY_PRESS)
{
event_key = (GdkEventKey *) event;
if(event_key->keyval == GDK_Escape)
{
active = 0;
gtk_widget_hide(widget);
return TRUE;
}
}
return FALSE;
}
static gint do_work(GtkWidget *widget, GdkEvent *event )
{
const gchar *entry_text;
gchar *digest;
gchar *base64;
gchar *buffer;
entry_text = gtk_entry_get_text(GTK_ENTRY(widget));
if(entry_text == NULL || strlen(entry_text) == 0 || master_password == NULL)
return FALSE;
buffer = g_strconcat(master_password, entry_text, NULL);
digest = g_compute_checksum_for_string(G_CHECKSUM_SHA256, buffer, -1);
base64 = g_base64_encode((guchar*)digest,strlen(digest));
if( (base64 = g_try_realloc(base64,10)) == NULL)
return FALSE;
base64[9] = '\0';
gtk_entry_set_text(GTK_ENTRY(widget), base64);
gtk_clipboard_set_text(gtk_clipboard_get (GDK_SELECTION_CLIPBOARD), base64, strlen(base64)); //CLIPBOaRD no type
g_free(digest);
g_free(base64);
g_free(buffer);
return TRUE;
}

gchar *get_config_filename()
{
if(master_password != NULL)
g_free(master_password);
return g_strconcat( g_get_user_config_dir(), "/.passgen", NULL);
}

gboolean save_config()
{
//STILL CLEAR TEXT
gchar *filename;
const gchar *tmpbuf;
gboolean success = TRUE;
filename = get_config_filename();
tmpbuf = gtk_entry_get_text(GTK_ENTRY(ent_preferences));
if(tmpbuf == NULL || strlen(tmpbuf) == 0)
success = FALSE;
else
master_password = g_strdup(tmpbuf);
printf("%s\t%s",filename,master_password);
if ( !success || !g_file_set_contents(filename, master_password, -1, NULL))
success = FALSE;//ERROR
gtk_widget_destroy(dialog);
g_free(filename);

return success;
}

static gint preferences_dialog( GtkWidget *widget, GdkEvent *event )
{
GtkWidget *label, *content_area;
/* Create the widgets */
dialog = gtk_dialog_new_with_buttons ("Password Generator Preferences",
GTK_WINDOW(window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_OK,
GTK_RESPONSE_NONE,
NULL);
content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));

label = gtk_label_new ("Insert the Master Password.\nThis will be stored in your computer.");
gtk_label_set_width_chars(GTK_LABEL(label),24);
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);

ent_preferences = gtk_entry_new();
gtk_entry_set_visibility(GTK_ENTRY(ent_preferences), FALSE);
gtk_entry_set_max_length(GTK_ENTRY(ent_preferences), 99);
gtk_entry_set_width_chars(GTK_ENTRY(ent_preferences), 99);

g_signal_connect(ent_preferences, "activate", G_CALLBACK(save_config), NULL);
/* Ensure that the dialog box is destroyed when the user responds. */
g_signal_connect(dialog, "response", G_CALLBACK(save_config), NULL);

/* Add the label, and show everything we've added to the dialog. */
gtk_container_add (GTK_CONTAINER (content_area), label);
gtk_container_add (GTK_CONTAINER (content_area), ent_preferences);
gtk_widget_show_all (dialog);
return TRUE;
}


gboolean load_config()
{
//ALWAYS TRUE BECAUSE SIGNALS DONT GIVE BACK ANY ERROR. FIXME
gchar *filename;
filename = get_config_filename();
if(!g_file_test (filename, G_FILE_TEST_EXISTS))
preferences_dialog(NULL, NULL);
else
g_file_get_contents(filename, &master_password, NULL, NULL);
g_free(filename);
//printf("%s",master_password);
return TRUE;
}

int main( int argc, char *argv[])
{
gtk_init (&argc, &argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
statusicon = gtk_status_icon_new_from_stock(GTK_STOCK_ABOUT);
entry = gtk_entry_new();
menu = gtk_menu_new();

GtkWidget *menuQuit;
GtkWidget *menuPreferences;
GtkWidget *d_preferences;

d_preferences = gtk_dialog_new();

gtk_container_add(GTK_CONTAINER(window),entry);
gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
gtk_object_set( GTK_OBJECT(window), "skip-taskbar-hint", TRUE,NULL);
gtk_window_set_keep_above (GTK_WINDOW (window), TRUE);
gtk_window_set_decorated (GTK_WINDOW (window), FALSE);
gtk_window_set_skip_pager_hint (GTK_WINDOW (window), TRUE);
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
gtk_window_stick (GTK_WINDOW (window));
gtk_window_present(GTK_WINDOW(window));

//gtk_status_icon_set_tooltip(statusicon, "Insert the site name");
gtk_entry_set_max_length(GTK_ENTRY(entry), 24);
gtk_entry_set_width_chars(GTK_ENTRY(entry), 24);
gtk_object_set( GTK_OBJECT(entry), "can-default", TRUE,NULL);

menuQuit = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
gtk_menu_shell_append (GTK_MENU_SHELL(menu), menuQuit);
menuPreferences = gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, NULL);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuPreferences);
gtk_widget_show_all(menu);
load_config();


g_signal_connect_swapped (G_OBJECT(statusicon), "button_press_event", G_CALLBACK (button_press), menu);
g_signal_connect(G_OBJECT(entry), "key_press_event", G_CALLBACK (key_press), NULL);
g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(do_work), NULL);
g_signal_connect(G_OBJECT(menuQuit), "activate", G_CALLBACK(destroy), NULL);
g_signal_connect(G_OBJECT(menuPreferences), "activate", G_CALLBACK(preferences_dialog), NULL);


gtk_main ();


return 1;
}
[/code]
The problem is when ran in Windows OS some things don't seem to work correctly. Namely the window skip pager-hint property and window placement. The window placement is supposed to be just above or below (whichever the case) the system tray icon but i can't seem to place it in that position. I would like to know your suggestions.

This project is based in a prototype i developed in python. It's in my site pneves


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group