diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22b4c77 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +.vscode/ +lib/ +node_modules/ diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..f9bce9b --- /dev/null +++ b/.npmignore @@ -0,0 +1,3 @@ +node_modules/ +.idea/ +.vscode/ diff --git a/README.md b/README.md index 3240428..5bba401 100644 --- a/README.md +++ b/README.md @@ -1,88 +1,52 @@ -# JS-Password-Generator :lock_with_ink_pen: +# JS-Password-Generator +### Install: +`npm i sos-password-generator` -![image](assets/screenshot.jpg) -It enables you to generate your own passwords with different degrees of power and security and adjust them to the length, characters, etc. You can include it in your website very easily, where no outside libraries have been used. Only Java Script. +### Usage +1. Require the package: -This writing method is not the only one and you can generate the same results in different ways in the same language. -I hope it's useful to you in your work, and I'm going to constantly improve its coordination and add new features to it. - -- Explain Code
-Here we use some function to generate a strings from Uppercase and lowercase letters, numbers and symbols -```java-script -function getRandomLower() { - return String.fromCharCode(Math.floor(Math.random() * 26) + 97); -} - -function getRandomUpper() { - return String.fromCharCode(Math.floor(Math.random() * 26) + 65); -} - -function getRandomNumber() { - return String.fromCharCode(Math.floor(Math.random() * 10) + 48); -} +```javascript +const PasswordGenerator = require('sos-password-generator') +``` -function getRandomSymbol() { - const symbols = "!@#$%^&*(){}[]=/<>,." - return symbols[Math.floor(Math.random() * symbols.length)]; -} +2. Create an instance of PowerGenerator Class -const randomFunc = { - lower: getRandomLower, - upper: getRandomUpper, - number: getRandomNumber, - symbol: getRandomSymbol, -}; +```javascript +const generator = new PasswordGenerator(); ``` -This is the code that executes the command when the button is pressed in order to collect and generate those values -```java-script -const generate = document.getElementById("generateBtn"); -generate.addEventListener("click", () => { - const length = document.getElementById("PasswordLength").value; - const hasUpper = document.getElementById("uppercase").checked; - const hasLower = document.getElementById("lowercase").checked; - const hasNumber = document.getElementById("numbers").checked; - const hasSymbol = document.getElementById("symbols").checked; - const result = document.getElementById("PasswordResult"); - result.innerText = generatePassword( - hasLower, - hasUpper, - hasNumber, - hasSymbol, - length - ); -}); - -function generatePassword(lower, upper, number, symbol, length){ - let generatedPassword = ""; - const typesCount = lower + upper + number + symbol; - const typesArr = [{lower}, {upper}, {number}, {symbol}].filter( - (item) => Object.values(item)[0] - ); - for (let i = 0; i < length; i += typesCount){ - typesArr.forEach((type) => { - const funcName = Object.keys(type)[0]; - generatedPassword += randomFunc[funcName](); - }); - } - const finalPassword = generatedPassword.slice(0, length); - return finalPassword; -} +3. Generate your password: +```javascript +console.log(generator.generatePassword()); //Result => generates random 8-character-length password + // contains numbers,lower case ,upper case, and symbols. +``` + + +### Custom Options +Before generating password,You can pass options object to the method init + +```javascript +generator.init({ + hasUpper:true, + hasLower:true, + hasSymbol:true, + hasNumber:false, + passwordLength:15, + allowedSymbols: '!@#$', +}) ``` -And the last ting is the code for the "copy to clipboard" button in order to copy the generated password. -```java-script -let button = document.getElementById("clipboardBtn"); -button.addEventListener("click", (e) => { - e.preventDefault(); - document.execCommand( - "copy", - false, - document.getElementById("PasswordResult").select() - ); -}); +### Default options + +```javascript +hasUpper:true +hasLower:true +hasSymbol:true +hasNumber:true +passwordLength:8 +allowedSymbols: '!@#$%^&*(){}[]=/<>,.' ``` -Note: This is optional, you can not add it, it's up to how useful this code can be in your projects. + Credits ------- diff --git a/assets/js.js b/assets/js.js deleted file mode 100644 index cfdaf7d..0000000 --- a/assets/js.js +++ /dev/null @@ -1,66 +0,0 @@ -function getRandomLower() { - return String.fromCharCode(Math.floor(Math.random() * 26) + 97); -} - -function getRandomUpper() { - return String.fromCharCode(Math.floor(Math.random() * 26) + 65); -} - -function getRandomNumber() { - return String.fromCharCode(Math.floor(Math.random() * 10) + 48); -} - -function getRandomSymbol() { - const symbols = "!@#$%^&*(){}[]=/<>,." - return symbols[Math.floor(Math.random() * symbols.length)]; -} - -const randomFunc = { - lower: getRandomLower, - upper: getRandomUpper, - number: getRandomNumber, - symbol: getRandomSymbol, -}; - -const generate = document.getElementById("generateBtn"); -generate.addEventListener("click", () => { - const length = document.getElementById("PasswordLength").value; - const hasUpper = document.getElementById("uppercase").checked; - const hasLower = document.getElementById("lowercase").checked; - const hasNumber = document.getElementById("numbers").checked; - const hasSymbol = document.getElementById("symbols").checked; - const result = document.getElementById("PasswordResult"); - result.innerText = generatePassword( - hasLower, - hasUpper, - hasNumber, - hasSymbol, - length - ); -}); - -function generatePassword(lower, upper, number, symbol, length){ - let generatedPassword = ""; - const typesCount = lower + upper + number + symbol; - const typesArr = [{lower}, {upper}, {number}, {symbol}].filter( - (item) => Object.values(item)[0] - ); - for (let i = 0; i < length; i += typesCount){ - typesArr.forEach((type) => { - const funcName = Object.keys(type)[0]; - generatedPassword += randomFunc[funcName](); - }); - } - const finalPassword = generatedPassword.slice(0, length); - return finalPassword; -} - -let button = document.getElementById("clipboardBtn"); -button.addEventListener("click", (e) => { - e.preventDefault(); - document.execCommand( - "copy", - false, - document.getElementById("PasswordResult").select() - ); -}); \ No newline at end of file diff --git a/index.html b/index.html deleted file mode 100644 index 5ba01ff..0000000 --- a/index.html +++ /dev/null @@ -1,49 +0,0 @@ - - - - - - - - - - JS Password Generator - - -
- -
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
-
- - -
- - - - \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..fd129c3 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,5 @@ +{ + "name": "password-generator", + "version": "1.0.0", + "lockfileVersion": 1 +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..dcf4ea3 --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "sos-password-generator", + "version": "1.0.0", + "description": "Secure passwords generator", + "main": "src/password-generator.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/abdussalam-alali/js-password-generator.git" + }, + "author": "Syrian Open Source", + "license": "ISC", + "bugs": { + "url": "https://github.com/abdussalam-alali/js-password-generator/issues" + }, + "homepage": "https://github.com/abdussalam-alali/js-password-generator#readme" +} diff --git a/src/password-generator.js b/src/password-generator.js new file mode 100644 index 0000000..5aaf082 --- /dev/null +++ b/src/password-generator.js @@ -0,0 +1,71 @@ +'use strict'; +class PasswordGenerator{ + constructor() { + this.settings = { + hasUpper:true, + hasLower:true, + hasSymbol:true, + hasNumber:true, + passwordLength:8, + allowedSymbols: '!@#$%^&*(){}[]=/<>,.', + }; + + this.password = ""; + } + init(settings){ + this.settings = Object.assign({},this.settings,settings); + } + #getRandomCharacter(start,offset) { + return String.fromCharCode(Math.floor(Math.random() * offset) + start); + } + + #randomFunc = { + hasLower: ()=> { return this.#getRandomCharacter(97,26)}, + hasUpper: ()=> { return this.#getRandomCharacter(97,26)}, + hasNumber: ()=>{ return this.#getRandomCharacter(48,10) }, + hasSymbol: ()=> { return this.settings.allowedSymbols[Math.floor(Math.random() * this.settings.allowedSymbols.length)]; + }, + }; + generatePassword(){ + let password = ""; + + const typesCount = Object.values(this.settings).filter(item=>{if(item===true) return item}).length; + const typesArr = Object.keys(this.settings) + .filter((item) => + { if(this.settings[item]===true) + return item + }); + + for (let i = 0; i < this.settings.passwordLength; i += typesCount){ + typesArr.forEach((type) => { + password += this.#randomFunc[type](); + }); + + } + password = password.slice(0,this.settings.passwordLength); + for(let i =0; i<16; i++) + { + password = this.#shuffle(password); + } + return password; + } + + + #shuffle(s) { + var arr = s.split(''); + var n = arr.length; + + for(var i=0 ; i