password-generator
Advanced tools
Comparing version 1.0.1 to 2.0.0
{ | ||
"name": "password-generator", | ||
"description": "Memorable password generator. For the command line, Node.js and browsers.", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"main": "lib/password-generator.js", | ||
@@ -13,2 +13,3 @@ "license": "MIT", | ||
"browser", | ||
"crypto", | ||
"security" | ||
@@ -15,0 +16,0 @@ ], |
@@ -79,5 +79,30 @@ /* | ||
function rand(min, max) { | ||
return Math.floor(Math.random() * (max - min) + min); | ||
var key, value, arr = new Uint8Array(max); | ||
getRandomValues(arr); | ||
for (key in arr) { | ||
if (arr.hasOwnProperty(key)) { | ||
value = arr[key]; | ||
if (value > min && value < max) { | ||
return value; | ||
} | ||
} | ||
} | ||
return rand(min, max); | ||
} | ||
function getRandomValues(buf) { | ||
if (root.crypto && root.crypto.getRandomValues) { | ||
root.crypto.getRandomValues(buf); | ||
} else if (typeof root.msCrypto === "object" && typeof root.msCrypto.getRandomValues === 'function') { | ||
root.msCrypto.getRandomValues(buf); | ||
} else if (module.exports === password && typeof require !== "undefined") { | ||
var bytes = require("crypto").randomBytes(buf.length); | ||
buf.set(bytes); | ||
} else { | ||
throw new Error("No secure random number generator available."); | ||
} | ||
} | ||
((typeof exports !== 'undefined') ? exports : root)[localName] = password; | ||
@@ -84,0 +109,0 @@ if (typeof exports !== 'undefined') { |
@@ -1,2 +0,2 @@ | ||
/*! password-generator - v1.0.0 (2015-05-22) | ||
/*! password-generator - v1.0.1 (2015-09-24) | ||
* ----------------- | ||
@@ -6,2 +6,2 @@ * Copyright(c) 2011-2015 Bermi Ferrer <bermi@bermilabs.com> | ||
*/ | ||
(function(e){function o(e,t){return Math.floor(Math.random()*(t-e)+e)}var t,n,r,i,s;r=/[a-zA-Z]$/,s=/[aeiouAEIOU]$/,n=/[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]$/,t=e.localPasswordGeneratorLibraryName||"generatePassword",i=function(e,t,r,i){var u="",a,f,l=[];e==null&&(e=10),t==null&&(t=!0),r==null&&(r=/\w/),i==null&&(i="");if(!t){for(f=33;126>f;f+=1)u=String.fromCharCode(f),u.match(r)&&l.push(u);if(!l.length)throw new Error("Could not find characters that match the password pattern "+r+". Patterns must match individual "+"characters, not the password as a whole.")}while(i.length<e)t?(i.match(n)?r=s:r=n,a=o(33,126),u=String.fromCharCode(a)):u=l[o(0,l.length)],t&&(u=u.toLowerCase()),u.match(r)&&(i=""+i+u);return i},(typeof exports!="undefined"?exports:e)[t]=i,typeof exports!="undefined"&&typeof module!="undefined"&&module.exports&&(module.exports=i)})(this); | ||
(function(e){function o(e,t){var n,r,i=new Uint8Array(t);u(i);for(n in i)if(i.hasOwnProperty(n)){r=i[n];if(r>e&&r<t)return r}return o(e,t)}function u(t){if(e.crypto&&e.crypto.getRandomValues)e.crypto.getRandomValues(t);else if(typeof e.msCrypto=="object"&&typeof e.msCrypto.getRandomValues=="function")e.msCrypto.getRandomValues(t);else{if(module.exports!==i||typeof require=="undefined")throw new Error("No secure random number generator available.");var n=require("crypto").randomBytes(t.length);t.set(n)}}var t,n,r,i,s;r=/[a-zA-Z]$/,s=/[aeiouAEIOU]$/,n=/[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]$/,t=e.localPasswordGeneratorLibraryName||"generatePassword",i=function(e,t,r,i){var u="",a,f,l=[];e==null&&(e=10),t==null&&(t=!0),r==null&&(r=/\w/),i==null&&(i="");if(!t){for(f=33;126>f;f+=1)u=String.fromCharCode(f),u.match(r)&&l.push(u);if(!l.length)throw new Error("Could not find characters that match the password pattern "+r+". Patterns must match individual "+"characters, not the password as a whole.")}while(i.length<e)t?(i.match(n)?r=s:r=n,a=o(33,126),u=String.fromCharCode(a)):u=l[o(0,l.length)],t&&(u=u.toLowerCase()),u.match(r)&&(i=""+i+u);return i},(typeof exports!="undefined"?exports:e)[t]=i,typeof exports!="undefined"&&typeof module!="undefined"&&module.exports&&(module.exports=i)})(this); |
@@ -79,5 +79,30 @@ /* | ||
function rand(min, max) { | ||
return Math.floor(Math.random() * (max - min) + min); | ||
var key, value, arr = new Uint8Array(max); | ||
getRandomValues(arr); | ||
for (key in arr) { | ||
if (arr.hasOwnProperty(key)) { | ||
value = arr[key]; | ||
if (value > min && value < max) { | ||
return value; | ||
} | ||
} | ||
} | ||
return rand(min, max); | ||
} | ||
function getRandomValues(buf) { | ||
if (root.crypto && root.crypto.getRandomValues) { | ||
root.crypto.getRandomValues(buf); | ||
} else if (typeof root.msCrypto === "object" && typeof root.msCrypto.getRandomValues === 'function') { | ||
root.msCrypto.getRandomValues(buf); | ||
} else if (module.exports === password && typeof require !== "undefined") { | ||
var bytes = require("crypto").randomBytes(buf.length); | ||
buf.set(bytes); | ||
} else { | ||
throw new Error("No secure random number generator available."); | ||
} | ||
} | ||
((typeof exports !== 'undefined') ? exports : root)[localName] = password; | ||
@@ -84,0 +109,0 @@ if (typeof exports !== 'undefined') { |
{ | ||
"name": "password-generator", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Memorable password generator. For the command line, Node.js and the browser.", | ||
@@ -12,2 +12,3 @@ "author": "Bermi Ferrer <bermi@bermilabs.com>", | ||
"browser", | ||
"crypto", | ||
"security" | ||
@@ -41,10 +42,9 @@ ], | ||
"browsers": [ | ||
"iexplore/7..latest", | ||
"firefox/10..latest", | ||
"chrome/20..latest", | ||
"opera/12..latest", | ||
"safari/4..latest", | ||
"iphone/6..latest", | ||
"ipad/6..latest", | ||
"android-browser/4.2" | ||
"iexplore/9..latest", | ||
"firefox/38..latest", | ||
"chrome/40..latest", | ||
"opera/30..latest", | ||
"safari/7..latest", | ||
"iphone/7..latest", | ||
"ipad/7..latest" | ||
], | ||
@@ -51,0 +51,0 @@ "harness": "mocha", |
@@ -86,3 +86,59 @@ # password-generator | ||
##### Example with custom validation rules | ||
Given the pattern regexp can only match a single character | ||
you can build a function that generates multiple passwords until you | ||
hit one that complies with your rules. | ||
The following example will generate a password with the following requirements | ||
* Must contain at least two numbers | ||
* Must contain at least three uppercase letters | ||
* Must contain at least three lowercase letters | ||
* Must contain at least two special characters | ||
* Must NOT contain sequences of two or more repeated characters | ||
```javascript | ||
var generatePassword = require("password-validator"); | ||
var maxLength = 18; | ||
var minLength = 12; | ||
var uppercaseMinCount = 3; | ||
var lowercaseMinCount = 3; | ||
var numberMinCount = 2; | ||
var specialMinCount = 2; | ||
var UPPERCASE_RE = /([A-Z])/g; | ||
var LOWERCASE_RE = /([a-z])/g; | ||
var NUMBER_RE = /([\d])/g; | ||
var SPECIAL_CHAR_RE = /([\?\-])/g; | ||
var NON_REPEATING_CHAR_RE = /([\w\d\?\-])\1{2,}/g; | ||
function isStrongEnough(password) { | ||
var uc = password.match(UPPERCASE_RE); | ||
var lc = password.match(LOWERCASE_RE); | ||
var n = password.match(NUMBER_RE); | ||
var sc = password.match(SPECIAL_CHAR_RE); | ||
var nr = password.match(NON_REPEATING_CHAR_RE); | ||
return password.length >= minLength && | ||
!nr && | ||
uc && uc.length >= uppercaseMinCount && | ||
lc && lc.length >= lowercaseMinCount && | ||
n && n.length >= numberMinCount && | ||
sc && sc.length >= specialMinCount; | ||
} | ||
function customPassword() { | ||
var password = ""; | ||
var randomLength = Math.floor(Math.random() * (maxLength - minLength)) + minLength; | ||
while (!isStrongEnough(password)) { | ||
password = generatePassword(randomLength, false, /[\w\d\?\-]/); | ||
} | ||
return password; | ||
} | ||
console.log(customPassword()); // => 2hP5v?1KKNx7_a-W | ||
``` | ||
## Running tests | ||
@@ -89,0 +145,0 @@ |
@@ -15,3 +15,3 @@ (function (root) { | ||
describe("When using the password generator, it:", function () { | ||
this.timeout(20); | ||
this.timeout(40); | ||
@@ -18,0 +18,0 @@ it('should generate a 10 chararacter memorable password', function () { |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
22575
371
177
17
2