Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
.vscode/
lib/
node_modules/
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
.idea/
.vscode/
116 changes: 40 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 </br>
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
-------
Expand Down
66 changes: 0 additions & 66 deletions assets/js.js

This file was deleted.

49 changes: 0 additions & 49 deletions index.html

This file was deleted.

5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
71 changes: 71 additions & 0 deletions src/password-generator.js
Original file line number Diff line number Diff line change
@@ -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<n-1 ; ++i) {
var j = Math.floor(Math.random() * n);

var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

s = arr.join('');
return s;
}
}

module.exports = PasswordGenerator;
File renamed without changes
File renamed without changes.