@wcj/generate-password
Version:
Generate Password is a generating random and unique passwords.
207 lines (167 loc) • 6.84 kB
Markdown
//img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor)

[](https://www.npmjs.com/package/@wcj/generate-password)
[](https://github.com/jaywcjlove/generate-password/actions/workflows/ci.yml)
[](https://jaywcjlove.github.io/generate-password/lcov-report/)
[](https://bundlephobia.com/result?p=@wcj/generate-password)
Generate Password is a generating random and unique passwords.
```bash
$ npm install @wcj/generate-password --save
```
```js
import { generate, generateMultiple, validate } from '@wcj/generate-password';
generate(); // => dK0#vA3@fG
generate({ length: 23 }); // => bB1@aO7^bF0!aA0~aQ1%aE3
generateMultiple(2, { length: 8 }); // => [ 'aG6@aC2(', 'dH0{fQ0%' ]
validate('qK0#dQ3*gG'); // => 4 Strong :) Now it's safe!
```
Or manually download and link **generate-password.js** in your HTML, It can also be downloaded via [UNPKG](https://unpkg.com/browse/@wcj/generate-password/):
CDN: [UNPKG](https://unpkg.com/browse/@wcj/generate-password/) | [jsDelivr](https://cdn.jsdelivr.net/npm/@wcj/generate-password/)
```html
<script src="https://unpkg.com/@wcj/generate-password/dist/generate-password.min.js"></script>
<script type="text/javascript">
GeneratePassword.generate(); // => dK0#vA3@fG
GeneratePassword.generate({ length: 23 }); // => bB1@aO7^bF0!aA0~aQ1%aE3
GeneratePassword.generateMultiple(2, { length: 8 }); // => [ 'aG6@aC2(', 'dH0{fQ0%' ]
GeneratePassword.validate('qK0#dQ3*gG'); // => 4, Strong :) Now it's safe!
</script>
```
Create a random password
```js
import { generate } from '@wcj/generate-password';
generate(); // => dK0#vA3@fG
generate({ length: 23 }); // => bB1@aO7^bF0!aA0~aQ1%aE3
generate({ upperCase: false }); // => n6[a3_f0$k
generate({ lowerCase: false }); // => N0(B3,C4$I
generate({ numeric: false }); // => cX*rB|jP:j
generate({ numeric: false }); // => eD3rA0gL1b
generate({ special: false, numeric: false }); // => aCaLlGfSgI
generate({ special: false, lowerCase: false, upperCase: false }); // => 4020810127
generate({ special: false, lowerCase: false, numeric: false }); // => DEEBBCBYAO
generate({ lowerCase: false, upperCase: false, numeric: false }); // => !%:#_#*&^!
```
Create a random set of passwords
```js
import { generateMultiple } from '@wcj/generate-password';
generateMultiple();
// [
// 'qK0#dQ3*gG', 'rQ1#lB0#kE', 'mO1#dH1_tQ', 'gE1$rE2)aJ',
// 'eR6#eJ5|qE', 'rP3!cH1)aK', 'iE0#dB2$iE', 'bC0&mI1#hB',
// 'kB0(eG1!lD', 'bA7>hE4)kA'
// ]
generateMultiple(2, { length: 8 }); // => [ 'aG6@aC2(', 'dH0{fQ0%' ]
```
symbols pass with lowercase and uppercase letters, numbers and special characters
```js
import { validate } from '@wcj/generate-password';
validate('qK0#dQ3*gG'); // => 4 Strong :) Now it's safe!
validate('n6[a3_f0$k'); // => 3 Medium level. Enter more symbols!
validate('aCaLlGfSgI'); // => 2 Very Weak! It's easy to crack!
validate('4020810127'); // => 1 It's easy to crack!
validate(); // => 0
```
```ts
export declare const LOWERCASE = 'abcdefghijklmnopqrstuvwxyz';
export declare const UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
export declare const NUMERIC = '0123456789';
export declare const SPECIAL_CHARACTER = '!@#$%^&*()_+~`|}{\\[\\]:;?>,.<-=\\/';
export declare type Option = {
/**
* Integer, length of password.
* @default 10
*/
length?: number;
/** Boolean, put lowercase in password */
lowerCase?: boolean;
/** Boolean, use uppercase letters in password. */
upperCase?: boolean;
/** Boolean, put numbers in password. */
numeric?: boolean;
/** Special characters */
special?: boolean;
};
/** Create a random password */
export declare function generate(opts?: Option): string;
/** Create a random set of passwords */
export declare function generateMultiple(length?: number, opts?: Option): string[];
/**
* symbols pass with lowercase and uppercase letters, numbers and special characters
* @return [0~4]
*
* `4` Strong :) Now it's safe!
* `3` Medium level. Enter more symbols!
* `2` Very Weak! It's easy to crack!
* `1` It's easy to crack!
*/
export declare function validate(password?: string): number;
```
```jsx mdx:preview
import React, { useState } from 'react';
import { generate } from '@wcj/generate-password';
const Demo = () => {
const [lowerCase, setLowerCase] = useState(true);
const [upperCase, setUpperCase] = useState(true);
const [numeric, setNumeric] = useState(true);
const [special, setSpecial] = useState(true);
const [length, setLength] = useState(8);
const opts = { lowerCase, upperCase, numeric, special, length };
const [password, setPassword] = useState(generate(opts));
return (
<div>
<div>{password}</div>
<button onClick={() => setPassword(generate(opts))}>Generate Password</button>
<div>
<label>
<input type="range" min="8" max="50" value={length} onChange={(evn) => setLength(Number(evn.target.value))} />{' '}
{length} length of password.
</label>
<br />
<label>
<input type="checkbox" checked={lowerCase} onChange={() => setLowerCase(!lowerCase)} /> Lower Case
Letter(a..z)
</label>
<br />
<label>
<input type="checkbox" checked={upperCase} onChange={() => setUpperCase(!upperCase)} /> Upper Case
Letter(A..Z)
</label>
<br />
<label>
<input type="checkbox" checked={numeric} onChange={() => setNumeric(!numeric)} /> Number (0..9)
</label>
<br />
<label>
<input type="checkbox" checked={special} onChange={() => setSpecial(!special)} /> Special characters
</label>
</div>
</div>
);
};
export default Demo;
```
```bash
npm install
npm run build
npm run start
cd core
npm run watch
npm run test
```
As always, thanks to our amazing contributors!
<a href="https://github.com/jaywcjlove/generate-password/graphs/contributors">
<img src="https://jaywcjlove.github.io/generate-password/CONTRIBUTORS.svg" />
</a>
Made with [contributors](https://github.com/jaywcjlove/github-action-contributors).
Licensed under the MIT License.
[![Buy me a coffee](https: