22 Password generator class
33
44 This is a javascript version of my PasswordGen PHP library accessible here:
5- https://github.com/lopeax /passwordGen
5+ https://github.com/zeraphie /passwordGen
66
77 This is written in ES2015 so requires a transpiler such as babel to compile
88
@@ -149,6 +149,7 @@ class PasswordGen {
149149 */
150150 static arrayKeySearch ( needles , haystack ) {
151151 let i = 0 , length = needles . length ;
152+
152153 while ( i < length ) {
153154 for ( let item in haystack ) {
154155 if ( haystack . hasOwnProperty ( item ) ) {
@@ -157,8 +158,10 @@ class PasswordGen {
157158 }
158159 }
159160 }
161+
160162 i ++ ;
161163 }
164+
162165 return false ;
163166 }
164167
@@ -178,9 +181,11 @@ class PasswordGen {
178181
179182 let range = max - min + 1 ;
180183 let max_range = 256 ;
184+
181185 if ( byteArray [ 0 ] >= Math . floor ( max_range / range ) * range ) {
182186 return this . randomInteger ( min , max ) ;
183187 }
188+
184189 return min + ( byteArray [ 0 ] % range ) ;
185190 } else {
186191 throw `Sorry the maximum is too large\n` +
@@ -223,13 +228,16 @@ class PasswordGen {
223228 * @return PasswordGen this The current instance of PasswordGen
224229 */
225230 setLength ( value = 0 ) {
231+ value = parseInt ( value ) ;
232+
226233 if (
227- value === parseInt ( value )
228- &&
234+ typeof value === 'number'
235+ &&
229236 value >= this . constructor . MINIMUMLENGTH
230237 ) {
231238 this . passwordLength = value ;
232239 }
240+
233241 return this ;
234242 }
235243
@@ -248,6 +256,7 @@ class PasswordGen {
248256 console . log ( this . errorTooLong ( 'keyspace' ) ) ;
249257 }
250258 }
259+
251260 return this ;
252261 }
253262
@@ -266,11 +275,12 @@ class PasswordGen {
266275 and if any of the characters in it
267276 are in the CHARACTERSETS array's keys
268277 --------------------------------------*/
278+ // console.log(sets.split(''), this.constructor.CHARACTERSETS);
269279 if (
270280 typeof sets !== 'string'
271- ||
272- this . constructor . arrayKeySearch (
273- sets , this . constructor . CHARACTERSETS
281+ ||
282+ ! this . constructor . arrayKeySearch (
283+ sets . split ( '' ) , this . constructor . CHARACTERSETS
274284 )
275285 ) {
276286 sets = this . constructor . DEFAULTSETS ;
@@ -302,11 +312,13 @@ class PasswordGen {
302312 */
303313 generatePassword ( ) {
304314 let password = '' ;
315+
305316 for ( let i = 0 ; i < this . passwordLength ; i ++ ) {
306317 password += this . keyspace . split ( '' ) [
307318 this . constructor . randomInteger ( 0 , this . keyspace . length - 1 )
308- ] ;
319+ ] ;
309320 }
321+
310322 return password ;
311323 }
312324
0 commit comments