summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/genpasswd.cpp
blob: b0cceff430d4e72d99ef1b8e5e40bf6f78eaf8b1 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/***************************************************************************
 *                                                                         *
 *   copyright (C) 2004 by Michael Buesch                                  *
 *   email: mbuesch@freenet.de                                             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2        *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 ***************************************************************************/

/***************************************************************************
 * copyright (C) 2004 by Ulf Schenk
 * This file is originaly based on version 1.0.1 of pwmanager
 * and was modified to run on embedded devices that run microkde
 *
 * $Id$
 **************************************************************************/  

#include "genpasswd.h"
#include "pwmexception.h"
#include "randomizer.h"


/* how often can a char of the same charset be reused in order */
#define FILTER_MAX_CHARSET_REUSE	3
/* re-randomize all charsets on every iteration (0/1) */
#define RERAND_CHARSET			0


struct staticCharsetStruct
{
	const char *lower;
	const char *upper;
	const char *num;
	const char *special;
	const char *blank;
};

static struct staticCharsetStruct staticCharset = {
	"abcdefghijklmnopqrstuvwxyz",
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
	"0123456789",
	"!\"§$%&/()=?,.-;:_+",
	" "
};


GenPasswd::GenPasswd()
 : length (8)
 , useFilter (true)
{
	dynCharset.setAutoDelete(true);
}

void GenPasswd::setCharset(bool lower,
			   bool upper,
			   bool num,
			   bool special,
			   bool blank,
			   QString user)
{
	unsigned int sanityCheck = 0;
	dynCharset_element *tmpElement;
	dynCharset.clear();
	if (lower) {
		tmpElement = new dynCharset_element;
		tmpElement->refCnt = 0;
		tmpElement->data = staticCharset.lower;
		dynCharset.append(tmpElement);
		++sanityCheck;
	}
	if (upper) {
		tmpElement = new dynCharset_element;
		tmpElement->refCnt = 0;
		tmpElement->data = staticCharset.upper;
		dynCharset.append(tmpElement);
		++sanityCheck;
	}
	if (num) {
		tmpElement = new dynCharset_element;
		tmpElement->refCnt = 0;
		tmpElement->data = staticCharset.num;
		dynCharset.append(tmpElement);
		++sanityCheck;
	}
	if (special) {
		tmpElement = new dynCharset_element;
		tmpElement->refCnt = 0;
		tmpElement->data = staticCharset.special;
		dynCharset.append(tmpElement);
		++sanityCheck;
	}
	if (blank) {
		tmpElement = new dynCharset_element;
		tmpElement->refCnt = 0;
		tmpElement->data = staticCharset.blank;
		dynCharset.append(tmpElement);
	}
	if (!user.isEmpty()) {
		tmpElement = new dynCharset_element;
		tmpElement->refCnt = 0;
		tmpElement->data = user;
		dynCharset.append(tmpElement);
		if (likely(user.length() >= 2))
			++sanityCheck;
	}
	BUG_ON(!sanityCheck);
	rndDynCharset();
}

void GenPasswd::rndDynCharset()
{
	QString tmpData;
	int pos;
	Randomizer *rnd = Randomizer::obj();
	// QPtrList<dynCharset_element>::iterator is not available in QT-3.1
	unsigned int i, cnt = dynCharset.count();
	dynCharset_element *p;
	for (i = 0; i < cnt; ++i) {
		p = dynCharset.at(i);
		PWM_ASSERT(p);
		tmpData = QString::null;
		while (p->data.length()) {
			pos = rnd->genRndInt() % p->data.length();
			tmpData.append(p->data.at(pos));
			p->data.remove(pos, 1);
		}
		p->data = tmpData;
	}
}

QString GenPasswd::gen()
{
	BUG_ON(dynCharset.count() <= 0);
	BUG_ON(length < 1);
	dynCharset_element *curCharset;
	QString ret;
	int i;
	for (i = 0; i < length; ++i) {
		curCharset = selectNextCharset();
#if RERAND_CHARSET != 0
		rndDynCharset();
#endif // RERAND_CHARSET
		ret += genNewRandom(curCharset);
	}
	return ret;
}

GenPasswd::dynCharset_element * GenPasswd::selectNextCharset()
{
	dynCharset_element *ret;
	int numCharsets = dynCharset.count();
	BUG_ON(numCharsets <= 0);
	if (numCharsets == 1)
		return dynCharset.at(0);
	Randomizer *rnd = Randomizer::obj();
	if (useFilter) {
		// find out which charsets are allowed (filtering)
		QPtrList<dynCharset_element> allowedCharsets;
		// QPtrList<dynCharset_element>::iterator is not available in QT-3.1
		unsigned int i, cnt = dynCharset.count();
		dynCharset_element *p;
		for (i = 0; i < cnt; ++i) {
			p = dynCharset.at(i);
			PWM_ASSERT(p);
			if (p->refCnt < FILTER_MAX_CHARSET_REUSE) {
				allowedCharsets.append(p);
			} else {
				p->refCnt = 0;
			}
		}
		int numAllowedCharsets = allowedCharsets.count();
		BUG_ON(numAllowedCharsets <= 0);
		// now get a random charset out of the allowed
		unsigned int randomPos = rnd->genRndUInt() % numAllowedCharsets;
		ret = allowedCharsets.at(randomPos);
		ret->refCnt++;
		return ret;
	}
	// all charsets are allowed here (no filtering). Get a random.
	unsigned int randomPos = rnd->genRndUInt() % numCharsets;
	ret = dynCharset.at(randomPos);
	return ret;
}

QChar GenPasswd::genNewRandom(const dynCharset_element *charset)
{
	Randomizer *rnd = Randomizer::obj();
	int pos = rnd->genRndInt() % charset->data.length();
	return charset->data.at(pos);
}