author | ulf69 <ulf69> | 2004-09-15 17:53:22 (UTC) |
---|---|---|
committer | ulf69 <ulf69> | 2004-09-15 17:53:22 (UTC) |
commit | d3925ba5bd25224bc4a60d3d6a107c464994a1ea (patch) (unidiff) | |
tree | 60f69da1d2b79ee3081e7ef5c09a46470ca6eda0 /pwmanager/pwmanager/genpasswd.cpp | |
parent | ce83a3479d23b9e8a59c745ccd0a0b14f64ef4e8 (diff) | |
download | kdepimpi-d3925ba5bd25224bc4a60d3d6a107c464994a1ea.zip kdepimpi-d3925ba5bd25224bc4a60d3d6a107c464994a1ea.tar.gz kdepimpi-d3925ba5bd25224bc4a60d3d6a107c464994a1ea.tar.bz2 |
initial revision
Diffstat (limited to 'pwmanager/pwmanager/genpasswd.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | pwmanager/pwmanager/genpasswd.cpp | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/genpasswd.cpp b/pwmanager/pwmanager/genpasswd.cpp new file mode 100644 index 0000000..b0cceff --- a/dev/null +++ b/pwmanager/pwmanager/genpasswd.cpp | |||
@@ -0,0 +1,192 @@ | |||
1 | /*************************************************************************** | ||
2 | * * | ||
3 | * copyright (C) 2004 by Michael Buesch * | ||
4 | * email: mbuesch@freenet.de * | ||
5 | * * | ||
6 | * This program is free software; you can redistribute it and/or modify * | ||
7 | * it under the terms of the GNU General Public License version 2 * | ||
8 | * as published by the Free Software Foundation. * | ||
9 | * * | ||
10 | ***************************************************************************/ | ||
11 | |||
12 | /*************************************************************************** | ||
13 | * copyright (C) 2004 by Ulf Schenk | ||
14 | * This file is originaly based on version 1.0.1 of pwmanager | ||
15 | * and was modified to run on embedded devices that run microkde | ||
16 | * | ||
17 | * $Id$ | ||
18 | **************************************************************************/ | ||
19 | |||
20 | #include "genpasswd.h" | ||
21 | #include "pwmexception.h" | ||
22 | #include "randomizer.h" | ||
23 | |||
24 | |||
25 | /* how often can a char of the same charset be reused in order */ | ||
26 | #define FILTER_MAX_CHARSET_REUSE3 | ||
27 | /* re-randomize all charsets on every iteration (0/1) */ | ||
28 | #define RERAND_CHARSET 0 | ||
29 | |||
30 | |||
31 | struct staticCharsetStruct | ||
32 | { | ||
33 | const char *lower; | ||
34 | const char *upper; | ||
35 | const char *num; | ||
36 | const char *special; | ||
37 | const char *blank; | ||
38 | }; | ||
39 | |||
40 | static struct staticCharsetStruct staticCharset = { | ||
41 | "abcdefghijklmnopqrstuvwxyz", | ||
42 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ", | ||
43 | "0123456789", | ||
44 | "!\"§$%&/()=?,.-;:_+", | ||
45 | " " | ||
46 | }; | ||
47 | |||
48 | |||
49 | GenPasswd::GenPasswd() | ||
50 | : length (8) | ||
51 | , useFilter (true) | ||
52 | { | ||
53 | dynCharset.setAutoDelete(true); | ||
54 | } | ||
55 | |||
56 | void GenPasswd::setCharset(bool lower, | ||
57 | bool upper, | ||
58 | bool num, | ||
59 | bool special, | ||
60 | bool blank, | ||
61 | QString user) | ||
62 | { | ||
63 | unsigned int sanityCheck = 0; | ||
64 | dynCharset_element *tmpElement; | ||
65 | dynCharset.clear(); | ||
66 | if (lower) { | ||
67 | tmpElement = new dynCharset_element; | ||
68 | tmpElement->refCnt = 0; | ||
69 | tmpElement->data = staticCharset.lower; | ||
70 | dynCharset.append(tmpElement); | ||
71 | ++sanityCheck; | ||
72 | } | ||
73 | if (upper) { | ||
74 | tmpElement = new dynCharset_element; | ||
75 | tmpElement->refCnt = 0; | ||
76 | tmpElement->data = staticCharset.upper; | ||
77 | dynCharset.append(tmpElement); | ||
78 | ++sanityCheck; | ||
79 | } | ||
80 | if (num) { | ||
81 | tmpElement = new dynCharset_element; | ||
82 | tmpElement->refCnt = 0; | ||
83 | tmpElement->data = staticCharset.num; | ||
84 | dynCharset.append(tmpElement); | ||
85 | ++sanityCheck; | ||
86 | } | ||
87 | if (special) { | ||
88 | tmpElement = new dynCharset_element; | ||
89 | tmpElement->refCnt = 0; | ||
90 | tmpElement->data = staticCharset.special; | ||
91 | dynCharset.append(tmpElement); | ||
92 | ++sanityCheck; | ||
93 | } | ||
94 | if (blank) { | ||
95 | tmpElement = new dynCharset_element; | ||
96 | tmpElement->refCnt = 0; | ||
97 | tmpElement->data = staticCharset.blank; | ||
98 | dynCharset.append(tmpElement); | ||
99 | } | ||
100 | if (!user.isEmpty()) { | ||
101 | tmpElement = new dynCharset_element; | ||
102 | tmpElement->refCnt = 0; | ||
103 | tmpElement->data = user; | ||
104 | dynCharset.append(tmpElement); | ||
105 | if (likely(user.length() >= 2)) | ||
106 | ++sanityCheck; | ||
107 | } | ||
108 | BUG_ON(!sanityCheck); | ||
109 | rndDynCharset(); | ||
110 | } | ||
111 | |||
112 | void GenPasswd::rndDynCharset() | ||
113 | { | ||
114 | QString tmpData; | ||
115 | int pos; | ||
116 | Randomizer *rnd = Randomizer::obj(); | ||
117 | // QPtrList<dynCharset_element>::iterator is not available in QT-3.1 | ||
118 | unsigned int i, cnt = dynCharset.count(); | ||
119 | dynCharset_element *p; | ||
120 | for (i = 0; i < cnt; ++i) { | ||
121 | p = dynCharset.at(i); | ||
122 | PWM_ASSERT(p); | ||
123 | tmpData = QString::null; | ||
124 | while (p->data.length()) { | ||
125 | pos = rnd->genRndInt() % p->data.length(); | ||
126 | tmpData.append(p->data.at(pos)); | ||
127 | p->data.remove(pos, 1); | ||
128 | } | ||
129 | p->data = tmpData; | ||
130 | } | ||
131 | } | ||
132 | |||
133 | QString GenPasswd::gen() | ||
134 | { | ||
135 | BUG_ON(dynCharset.count() <= 0); | ||
136 | BUG_ON(length < 1); | ||
137 | dynCharset_element *curCharset; | ||
138 | QString ret; | ||
139 | int i; | ||
140 | for (i = 0; i < length; ++i) { | ||
141 | curCharset = selectNextCharset(); | ||
142 | #if RERAND_CHARSET != 0 | ||
143 | rndDynCharset(); | ||
144 | #endif // RERAND_CHARSET | ||
145 | ret += genNewRandom(curCharset); | ||
146 | } | ||
147 | return ret; | ||
148 | } | ||
149 | |||
150 | GenPasswd::dynCharset_element * GenPasswd::selectNextCharset() | ||
151 | { | ||
152 | dynCharset_element *ret; | ||
153 | int numCharsets = dynCharset.count(); | ||
154 | BUG_ON(numCharsets <= 0); | ||
155 | if (numCharsets == 1) | ||
156 | return dynCharset.at(0); | ||
157 | Randomizer *rnd = Randomizer::obj(); | ||
158 | if (useFilter) { | ||
159 | // find out which charsets are allowed (filtering) | ||
160 | QPtrList<dynCharset_element> allowedCharsets; | ||
161 | // QPtrList<dynCharset_element>::iterator is not available in QT-3.1 | ||
162 | unsigned int i, cnt = dynCharset.count(); | ||
163 | dynCharset_element *p; | ||
164 | for (i = 0; i < cnt; ++i) { | ||
165 | p = dynCharset.at(i); | ||
166 | PWM_ASSERT(p); | ||
167 | if (p->refCnt < FILTER_MAX_CHARSET_REUSE) { | ||
168 | allowedCharsets.append(p); | ||
169 | } else { | ||
170 | p->refCnt = 0; | ||
171 | } | ||
172 | } | ||
173 | int numAllowedCharsets = allowedCharsets.count(); | ||
174 | BUG_ON(numAllowedCharsets <= 0); | ||
175 | // now get a random charset out of the allowed | ||
176 | unsigned int randomPos = rnd->genRndUInt() % numAllowedCharsets; | ||
177 | ret = allowedCharsets.at(randomPos); | ||
178 | ret->refCnt++; | ||
179 | return ret; | ||
180 | } | ||
181 | // all charsets are allowed here (no filtering). Get a random. | ||
182 | unsigned int randomPos = rnd->genRndUInt() % numCharsets; | ||
183 | ret = dynCharset.at(randomPos); | ||
184 | return ret; | ||
185 | } | ||
186 | |||
187 | QChar GenPasswd::genNewRandom(const dynCharset_element *charset) | ||
188 | { | ||
189 | Randomizer *rnd = Randomizer::obj(); | ||
190 | int pos = rnd->genRndInt() % charset->data.length(); | ||
191 | return charset->data.at(pos); | ||
192 | } | ||