MCPcopy Create free account
hub / github.com/RandomAPI/Randomuser.me-Node / genPassword

Method genPassword

api/1.2/api.js:325–377  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

323 }
324
325 genPassword() {
326 if (this.password.length === 0) {
327 return randomItem(this.datasets.common.passwords);
328 }
329
330 let charsets = {
331 special: " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
332 upper: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
333 lower: "abcdefghijklmnopqrstuvwxyz",
334 number: "0123456789"
335 };
336
337 // Parse sections
338 let sections = ["special", "upper", "lower", "number"];
339 let matches = this.password.split(',').filter(val => sections.indexOf(val) !== -1)
340
341 if (matches.length === 0) {
342 return randomItem(this.datasets.common.passwords);
343 }
344
345 matches = matches.filter((v,i,self) => self.indexOf(v) === i);
346
347 // Construct charset to choose from
348 let charset = "";
349 matches.forEach(match => {
350 charset += charsets[match];
351 });
352
353 let length = this.password.split(',').slice(-1)[0];
354
355 // Range
356 let min, max;
357 if (length.indexOf('-') !== -1) {
358 let range = length.split('-').map(Number);
359 min = Math.min(...range);
360 max = Math.max(...range);
361 } else {
362 min = Number(Number(length));
363 max = min;
364 }
365 min = min > 64 || min < 1 || min === undefined || isNaN(min) ? 8 : min;
366 max = max > 64 || max < 1 || max === undefined || isNaN(max) || max < min ? 64 : max;
367
368 let passLen = range(min, max);
369
370 // Generate password
371 let password = "";
372 for (let i = 0; i < passLen; i++) {
373 password += String(charset[range(0, charset.length-1)]);
374 }
375
376 return password;
377 }
378}
379
380function random(mode, length) {

Callers 1

generateMethod · 0.95

Calls 2

randomItemFunction · 0.70
rangeFunction · 0.70

Tested by

no test coverage detected