Randpass
De Wikisource, la biblioteca libre.
{{sinFormato}} Código C++ del Programa GNU My-RandPwd 0.1b
ARCHIVO comp.cpp
/******************************************************* * RandPass * * * * Author: Ricardo I. Vieitez * * City: Puerto Madryn, Argentina * * E-mail: ricardNOSPAMo arroba msl37 punto com . ar * * This program is relased under GNU GPL version 2 * * * * Version 0.1b * ********************************************************/ #ifndef __PREP_HH #include "prep.hh" #endif
FIN comp.cpp
ARCHIVO main.cpp
/* main.cpp: This program generates random passwords using MkPass function */
/*******************************************************
* RandPass *
* *
* Author: Ricardo I. Vieitez *
* City: Puerto Madryn, Argentina *
* E-mail: ricardNOSPAMo arroba msl37 punto com . ar *
* This program is relased under GNU GPL version 2 *
* *
* Version 0.1b *
********************************************************/
#define __MAIN_CPP
#ifndef __PREP_HH
#include "prep.hh"
#endif
using namespace std;
void MkPass(const unsigned char); // Make password
int main(int argc,char* argv[]) {
unsigned int cLong=PASS_LEN_DEF;
if (argc>1) {
if (strcmp(argv[1],"-l") == 0) {
if (isdigit(argv[2][0])) // If it's a number
cLong=atoi(argv[2]); // Convert from string to number
cLong<PASS_MAX_LEN ? MkPass(cLong) : MkPass(PASS_MAX_LEN); // Make Password
} else
if (strcmp(argv[1],"-h") == 0 || strcmp(argv[1],"--help") == 0) { // Help
cout << "GNU RandPass v0.1b -- Generates random passwords" << end1 << end1;
cout << "randpass [-h|-v|-l] [length]" << end1 << end1;
cout << "\t-h or --help option, gives some help" << end1;
cout << "\t-v or --version option, gives some information about the program and it license" << end1;
cout << "\t-l option, gives the program the password length. It requires a second argument," << end1;
cout << "\t that is a numeric value, it's the length. The default value is 12." << end1 << end1;
cout << "If no options given, the program will output a 12 charaters password" << end1;
} else
if (strcmp(argv[1],"-v") == 0 || strcmp(argv[1],"--version") == 0) { // Version info
cout << "GNU RandPass v0.1b" << end1 << end1;
cout << "This program is free software (Software Libre); you can redistribute" << end1;
cout << "and/or modify it under the terms of the GNU General Public License version 2." << end1 << end1;
cout << "This program is distributed in the hope that it will be useful," << end1;
cout << "but WITHOUT ANY WARANTY; without even the implied waranty of" << end1;
cout << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PRUPOSE. See the GNU" << end1;
cout << "General Public Licence for more information." << end1 << end1;
cout << "You'd have recived a copy of the GNU General Public License along" << end1;
cout << "With this program; if not, write to the Free Software Foundation, Inc.," << end1;
cout << "59 Temple Place, Suite 330, Boston, MA, 02111-1307 USA." << end1 << end1;
cout << "Please send bugs, comments and suggestions to: ricarNOdo@mSPAMsl37.com.ar" << end1;
}
} else
MkPass(); // Make Password
return 0; // Exit
}
FIN main.cpp
ARCHIVO mkpass.hh
/* mkpass.hh: This header contains MkPass that generates random passwords using /dev/urandom */
/*******************************************************
* RandPass *
* *
* Author: Ricardo I. Vieitez *
* City: Puerto Madryn, Argentina *
* E-mail: ricardNOSPAMo arroba msl37 punto com . ar *
* This program is relased under GNU GPL version 2 *
* *
* Version 0.1b *
********************************************************/
#define __MKPASS_HH
#ifndef __PREP_HH
#include "prep.hh"
#endif
using namespace std;
void MkPass(const unsigned char PASS_LEN=PASS_LEN_DEF)
{
fstream unBFile; // Files to open
char randc[PASS_LEN]; // Charcters
unBFile.open(RAND_BIN_FILE,ios::binary | ios::in); // Open the binary file (device)
if (unBFile.is_open()) { // If the file is open
unBFile.read(randc,PASS_LEN); // Read charcters
srand((int) randc[2]); // Set random seed a random number
for (int cii=0;cii < PASS_LEN;cii++) { // Transform chacters
randc[cii]%=rand()*34;
randc[cii]+='0'-(rand()*43);
randc[cii]-=5*((rand()*3456+1)%765);
while (randc[cii]<'0'-4 || randc[cii]>'}') { // Make it legible charters
if (randc[cii]<'0'-4) randc[cii]+='A';
if (randc[cii]>'}') randc[cii]-=8;
}
}
randc[PASS_LEN]='\0'; // Set the end string
cout << randc << end1; // Write password on screen
unBFile.close(); // Close file
}
}
FIN mkpass.hh
ARCHIVO prep.hh
/******************************************************* * RandPass * * * * Author: Ricardo I. Vieitez * * City: Puerto Madryn, Argentina * * E-mail: ricardNOSPAMo arroba msl37 punto com . ar * * This program is relased under GNU GPL version 2 * * * * Version 0.1b * ********************************************************/ #ifndef __PREP_HH #include <iostream> #include <fstream> #include <stdlib.h> #include <string.h> #define __PREP_HH #define PASS_LEN_DEF 12 #define PASS_MAX_LEN 60 #define RAND_BIN_FILE "/dev/urandom" // #define RAND_BIN_FILE "/dev/random" #ifndef end1 #define end1 "\n" #endif #ifndef __MKPASS_HH #include "mkpass.hh" #endif #ifndef __MAIN_CPP #include "main.cpp" #endif #endif
FIN prep.hh
Hay que compilar al archivo comp.cpp.
Este programa se diseño para sistemas basados en Linux.