password generator


Posted: settembre 1st, 2009 | Author: Francesco Apollonio | Filed under: Programmazione | Tags: , , , , | 1 Comment »


Mi serviva una nuova password, così, preso da un’attacco di fancazzismo, ho creato questo piccolo programma.

Una volta scaricato il codice sorgente non vi resta che compilarlo con il comando:

gcc passGenerator.c -o passGenerator

dove passGenerator.c è il nome del file contenente i sorgenti. Il suo utilizzo è semplice, se lanciato senza parametri genera una password di 10 caratteri con 5 lettere (maiuscole e minuscole), 3 numeri e 2 caratteri speciali:

$ ./passGenerator
Generating 10 chars random password ( 5 alpha 3 numeric 2 special ) ...
Im5[U5uS2=

Se lanciato con un parametro numerico genera una password della dimensione passata come argomento:

$ ./passGenerator 15
Generating 15 chars random password ( 8 alpha 4 numeric 3 special ) ...
9&WcD{mT9rN0M3#

Le percentuali di numeri e caratteri speciali possono essere modificate all’interno del sorgente cambiando i valori di:

#define PERC_SPECIAL_CHARS 20
#define PERC_NUMBERS_CHARS 40

Qui c’è il codice:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include
#include
#include
#include 
 
#define DEFAULT_PASS_LENGTH 10
#define PERC_SPECIAL_CHARS 20
#define PERC_NUMBERS_CHARS 40
#define MIX_TIMES 20
 
char getNumber ();
char getSpec ();
char getAlph ();
void mixString(char*, int);
 
int main (int argc, char **argv)
{
	char *pass;
	int j, i=33, randNo, passLength = DEFAULT_PASS_LENGTH, specChars=0, percSpecChars = PERC_SPECIAL_CHARS;
	int numChars = 0, percNumChars = PERC_NUMBERS_CHARS;
 
	srand(time(NULL));
 
	if (argc >= 2)
		passLength = atoi(argv[1]);
 
	pass = malloc(passLength + 1);
	specChars = ((float) passLength / 100) * percSpecChars;
	numChars = ((float) (passLength - specChars) / 100) * percNumChars;
 
	printf ("Generating %d chars random password ( %d alpha %d numeric %d special ) ...\n", passLength, passLength-specChars-numChars, numChars, specChars);
 
	i = 0;
	for (j = 0; j < numChars; j++) {
		pass[i] = getNumber();
		i++;
	}
	for (j = 0; j < specChars; j++) {
		pass[i] = getSpec();
		i++;
	}
	for (j = 0; j < (passLength - specChars - numChars); j++) {
		pass[i] = getAlph();
		i++;
	}
	pass[i] = '\0';
 
	mixString(pass, passLength);
 
	printf("%s\n", pass);
}
 
void mixString(char* string, int length) {
	int i,n; 	char c;
	for (i=0;i 47)
		randNo += 10;
	if (randNo > 64)
		randNo += 26;
	if (randNo > 96)
		randNo += 26;
	return (char) randNo;
}
 
char getAlph () {
	int randNo = 65 + rand() % 52;
	if (randNo > 90)
		randNo += 6;
	return (char) randNo;
}

Spero vi sia utile.


One Comment on “password generator”

  1. 1 password generator | Feedelissimo said at 18:55 on settembre 1st, 2009:

    [...] password generator Scritto da:Francesco Apollonio il 2009-09-01 [...]


Leave a Reply

  • Articoli Recenti:

  • Tag

  • Categorie

  • Meta

  • Licenza Creative Commons

    Copyright © 2012, | ldlabs.org – Blog is proudly powered by WordPress All rights Reserved | Theme by Ryan McNair modified By Francesco Apollonio

    Smilla Magazine Il Bloggatore iwinuxfeed.altervista.org Feedelissimo - tecnologia WebShake - tecnologia