[c/win32] generatemdp : générateur de mot de passe

Description

Bon, et bien aujourd'hui il pleut en Belgique (étonant?) et je n'ai pas l'inspiration, mais j'ai tout de même envie de coder. Alors voila, j'ai (vite) fait un petit générateur de mot de passe. Rien d'exeptionnel.Mais cela évitera à certains j'espère de mettre le nom de leur chien en guise de mdp.

Source / Exemple :


/*

  • Create by : deck_bsd.
  • Date of creation : 29/05/2006.
  • TASK : Generator of password.
  • /
#include <windows.h> #include <time.h> #define IDM_QUIT 11 #define IDM_HELP 12 /* Declare Windows procedure */ LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK PropoProc(HWND hwDlg, UINT message, WPARAM wParam, LPARAM lParam); char * GenerateMDPaln(int iNbr,char * szPasswd); char * GenerateMDPn(int iNbr,char * szPasswd); char * GenerateMDPa(int iNbr,char * szPasswd); char szClassName[ ] = "WindowsApp"; /* Variables globales */ HINSTANCE GlobalHInstance; static HWND hwMDP, hwNbr; static HWND hwButtonGenerate , hwButtonAl , hwButtonAln , hwButtonN; int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) { HWND hwnd; /* This is the handle for our window */ MSG messages; /* Here messages to the application are saved */ WNDCLASSEX wincl; /* Data structure for the windowclass */ HMENU hmMain,hmQuit,hmHelp; /* The Window structure */ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */ wincl.style = CS_DBLCLKS; /* Catch double-clicks */ wincl.cbSize = sizeof (WNDCLASSEX); /* Use default icon and mouse-pointer */ wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC_ARROW); wincl.lpszMenuName = NULL; /* No menu */ wincl.cbClsExtra = 0; /* No extra bytes after the window class */ wincl.cbWndExtra = 0; /* structure or the window instance */ wincl.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1); if (!RegisterClassEx (&wincl)) return 0; hmQuit = CreateMenu(); AppendMenu(hmQuit,MF_STRING,IDM_QUIT,"Quit"); hmMain = CreateMenu(); AppendMenu(hmMain,MF_POPUP,(UINT)hmQuit,"&File"); hmHelp = CreateMenu(); AppendMenu(hmHelp,MF_STRING,IDM_HELP,"About ..."); AppendMenu(hmMain,MF_POPUP,(UINT)hmHelp,"&?"); hwnd = CreateWindowEx ( 0, /* Extended possibilites for variation */ szClassName, /* Classname */ "GenerateMDP", /* Title Text */ WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, /* default window */ CW_USEDEFAULT, /* Windows decides the position */ CW_USEDEFAULT, /* where the window ends up on the screen */ 200, /* The programs width */ 170, /* and height in pixels */ HWND_DESKTOP, /* The window is a child-window to desktop */ hmMain, /* No menu */ hThisInstance, /* Program Instance handler */ NULL /* No Window Creation data */ ); ShowWindow (hwnd, nFunsterStil); GlobalHInstance = hThisInstance; while (GetMessage (&messages, NULL, 0, 0)) { if(messages.message == WM_KEYDOWN){ switch(messages.wParam){ case VK_RETURN : int iNbr=0; char szNbr[2+1]; SetFocus(hwButtonGenerate); GetWindowText(hwNbr,szNbr,3); iNbr = atoi(szNbr); if(!iNbr){ MessageBox(hwnd,"Please, the number of characters must be > 0","Error",MB_OK | MB_ICONINFORMATION); break; } char * szPasswd; szPasswd = (char*)malloc(sizeof(char)*(iNbr+1)); if(szPasswd == NULL){ MessageBox(NULL,"Function : malloc()","Error",MB_OK | MB_ICONERROR); SetFocus(hwNbr); break; } _beep(1000,200); if((SendMessage(hwButtonN,BM_GETCHECK,0,0)) == BST_CHECKED) SetWindowText(hwMDP,GenerateMDPn(iNbr,szPasswd)); if((SendMessage(hwButtonAln,BM_GETCHECK,0,0)) == BST_CHECKED) SetWindowText(hwMDP,GenerateMDPaln(iNbr,szPasswd)); if((SendMessage(hwButtonAl,BM_GETCHECK,0,0)) == BST_CHECKED) SetWindowText(hwMDP,GenerateMDPa(iNbr,szPasswd)); free(szPasswd); break; } } TranslateMessage(&messages); DispatchMessage(&messages); } return messages.wParam; } LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HWND hwLabelNbr; int iNbr=0; char szNbr[2+1]; switch (message) { case WM_CREATE : hwLabelNbr = CreateWindow("static","Nbr of characters :",WS_CHILD | WS_VISIBLE, 20,20,150,20,hwnd,NULL,GlobalHInstance,NULL); hwNbr = CreateWindow("edit","0",WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER | ES_NUMBER, 150,20,20,20,hwnd,NULL,GlobalHInstance,NULL); hwMDP = CreateWindow("edit","",WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | ES_READONLY, 20,45,150,20,hwnd,NULL,GlobalHInstance,NULL); hwButtonGenerate = CreateWindow("button","Generate",WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 60,100,70,20,hwnd,(HMENU)1,GlobalHInstance,NULL); hwButtonAln = CreateWindow("button","Alphanum",WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, 20,70,75,20,hwnd,(HMENU)2,GlobalHInstance,NULL); hwButtonN = CreateWindow("button","Num",WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, 95,70,40,20,hwnd,(HMENU)3,GlobalHInstance,NULL); hwButtonAl = CreateWindow("button","Alpha",WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, 145,70,60,20,hwnd,(HMENU)4,GlobalHInstance,NULL); HFONT PoliceTahoma,PoliceComic; PoliceTahoma = CreateFont(14,0,0,0,0,0,0,0,0,0,0,0,0,"tahoma"); PoliceComic = CreateFont(17,0,0,0,0,0,TRUE,0,0,0,0,0,0,"comic sans MS"); SendMessage(hwMDP,WM_SETFONT,(long)PoliceTahoma,0); SendMessage(hwNbr,WM_SETFONT,(long)PoliceTahoma,0); SendMessage(hwButtonGenerate,WM_SETFONT,(long)PoliceTahoma,0); PoliceTahoma = CreateFont(13,0,0,0,0,0,0,0,0,0,0,0,0,"tahoma"); SendMessage(hwButtonAln,WM_SETFONT,(long)PoliceTahoma,0); SendMessage(hwButtonN,WM_SETFONT,(long)PoliceTahoma,0); SendMessage(hwButtonAl,WM_SETFONT,(long)PoliceTahoma,0); SendMessage(hwLabelNbr,WM_SETFONT,(long)PoliceComic,0); SetFocus(hwNbr); break; case WM_COMMAND : switch(HIWORD(wParam)){ case BN_CLICKED : switch(LOWORD(wParam)){ case 1 : GetWindowText(hwNbr,szNbr,3); iNbr = atoi(szNbr); if(!iNbr){ MessageBox(hwnd,"Please, the number of characters must be > 0","Error",MB_OK | MB_ICONINFORMATION); SetFocus(hwNbr); break; } char * szPasswd; szPasswd = (char*)malloc(sizeof(char)*(iNbr+1)); if(szPasswd == NULL){ MessageBox(NULL,"Function : malloc()","Error",MB_OK | MB_ICONERROR); } _beep(1000,200); if((SendMessage(hwButtonN,BM_GETCHECK,0,0)) == BST_CHECKED) SetWindowText(hwMDP,GenerateMDPn(iNbr,szPasswd)); if((SendMessage(hwButtonAln,BM_GETCHECK,0,0)) == BST_CHECKED) SetWindowText(hwMDP,GenerateMDPaln(iNbr,szPasswd)); if((SendMessage(hwButtonAl,BM_GETCHECK,0,0)) == BST_CHECKED) SetWindowText(hwMDP,GenerateMDPa(iNbr,szPasswd)); free(szPasswd); break; case 2 : SendMessage(hwButtonN,BM_SETCHECK,(long)BST_UNCHECKED,0); SendMessage(hwButtonAl,BM_SETCHECK,(long)BST_UNCHECKED,0); break; case 3 : SendMessage(hwButtonAln,BM_SETCHECK,(long)BST_UNCHECKED,0); SendMessage(hwButtonAl,BM_SETCHECK,(long)BST_UNCHECKED,0); break; case 4 : SendMessage(hwButtonAln,BM_SETCHECK,(long)BST_UNCHECKED,0); SendMessage(hwButtonN,BM_SETCHECK,(long)BST_UNCHECKED,0); break; case 11 : PostQuitMessage(0); break; case 12 : HGLOBAL hgMemory; LPDLGTEMPLATE dltDlgBox; LPWORD lpwWord; LPWSTR lpwsUnicode; hgMemory = GlobalAlloc(GPTR,512); if(!hgMemory) break; dltDlgBox = (LPDLGTEMPLATE) hgMemory; dltDlgBox->style = WS_CAPTION | WS_POPUP | WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX | DS_MODALFRAME; dltDlgBox->cx=210; dltDlgBox->cy=100; dltDlgBox->x=GetSystemMetrics(SM_CXSCREEN)/6; dltDlgBox->y=GetSystemMetrics(SM_CYSCREEN)/6; lpwWord = (LPWORD) (dltDlgBox + 1); lpwsUnicode = (LPWSTR) (lpwWord + 2); MultiByteToWideChar(CP_ACP,0,"A propos de :",-1,lpwsUnicode,128); DialogBoxIndirect(GlobalHInstance,dltDlgBox,0,(DLGPROC)PropoProc); GlobalFree(hgMemory); break; } break; } break; case WM_DESTROY: PostQuitMessage (0); break; default: return DefWindowProc (hwnd, message, wParam, lParam); } return 0; } /* TASK : génère un code alphanumérique. */ char * GenerateMDPaln(int iNbr,char * szPasswd){ int iCount; BOOL boSwitch; unsigned int uiGen; srand(time(NULL)); for(iCount=0; iCount < iNbr ; iCount++){ boSwitch = rand()%2; switch(boSwitch){ case 0 :uiGen = (34+ (rand()%91));break; case 1 :uiGen = (48+ (rand()%9));break; } szPasswd[iCount] = uiGen; } szPasswd[iNbr] = '\0'; return szPasswd; } /* TASK : Génère un code numérique. */ char * GenerateMDPn(int iNbr,char * szPasswd){ int iCount; unsigned int uiGen; srand(time(NULL)); for(iCount = 0 ; iCount < iNbr ; iCount++){ uiGen = (48+(rand()%9)); szPasswd[iCount] = uiGen; } szPasswd[iNbr] = '\0'; return szPasswd; } /* TASK : Génère un code alphabétique. */ char * GenerateMDPa(int iNbr,char * szPasswd){ int iCount; BOOL boSwitch; unsigned int uiGen; srand(time(NULL)); for(iCount = 0 ; iCount < iNbr ; iCount++){ boSwitch = rand()%2; switch(boSwitch){ case 0 : uiGen = (34+(rand()%13));break; case 1 : uiGen = (58+(rand()%67));break; } szPasswd[iCount] = uiGen; } szPasswd[iNbr] = '\0'; return szPasswd; } LRESULT CALLBACK PropoProc(HWND hwDlg, UINT message, WPARAM wParam, LPARAM lParam){ HWND hwGroup,hwButtonOk,hwLabelInfo; HWND hwButtonSite,hwButtonMail; switch(message){ case WM_INITDIALOG : hwGroup = CreateWindow("button","GenerateMDP",WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 10,10,400,160,hwDlg,NULL,GlobalHInstance,NULL); hwButtonOk = CreateWindow("button","OK",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 16,146,30,20,hwDlg,(HMENU)111,GlobalHInstance,NULL); hwLabelInfo = CreateWindow("static","Créer par : Deck_bsd \n\nVersion : 1.0 \n\nLicense : Freeware",WS_CHILD | WS_VISIBLE, 120,30,150,90,hwDlg,NULL,GlobalHInstance,NULL); hwButtonSite = CreateWindow("button","http://deck-bsd.eurower.net",WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 120,115,200,20,hwDlg,(HMENU)1111,GlobalHInstance,NULL); hwButtonMail = CreateWindow("button","deck_bsd01@yahoo.fr",WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 120,140,200,20,hwDlg,(HMENU)11111,GlobalHInstance,NULL); HFONT PoliceComic; HFONT PoliceVerdana; PoliceComic = CreateFont(15,0,0,0,0,0,0,0,0,0,0,0,0,"comic sans MS"); PoliceVerdana = CreateFont(15,0,0,0,0,0,0,0,0,0,0,0,0,"verdana"); SendMessage(hwGroup,WM_SETFONT,(long)PoliceComic,0); SendMessage(hwButtonOk,WM_SETFONT,(long)PoliceComic,0); SendMessage(hwLabelInfo,WM_SETFONT,(long)PoliceVerdana,0); SendMessage(hwButtonSite,WM_SETFONT,(long)PoliceVerdana,0); SendMessage(hwButtonMail,WM_SETFONT,(long)PoliceVerdana,0); break; case WM_COMMAND : if(HIWORD(wParam) == BN_CLICKED){ switch(LOWORD(wParam)){ case 111 : EndDialog(hwDlg,0); break; case 1111 : ShellExecute(NULL,"open","http://deck-bsd.eurower.net",0,NULL,SW_MAXIMIZE); break; case 11111 : ShellExecute(NULL,"open","mailto:deck_bsd01@yahoo.fr",0,NULL,SW_MAXIMIZE); break; } } break; case WM_CLOSE : EndDialog(hwDlg,0); break; } return 0; }

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.