devils-dice 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -0
- package/package.json +28 -0
- package/src/index.cjs +71 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +85 -0
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# devils-dice 🔐
|
|
2
|
+
|
|
3
|
+
A cryptographically secure random password generator for Node.js.
|
|
4
|
+
|
|
5
|
+
Built using Node's `crypto` module and follows modern password security best practices.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Secure randomness using `crypto.randomInt`
|
|
12
|
+
- Supports ESM and CommonJS
|
|
13
|
+
- Enforces strong password rules
|
|
14
|
+
- Customizable length and character sets
|
|
15
|
+
- Zero dependencies
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install devils-dice
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "devils-dice",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Cryptographically secure random password generator",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"types": "./src/index.d.ts",
|
|
7
|
+
"main": "./src/index.cjs",
|
|
8
|
+
"module": "./src/index.js",
|
|
9
|
+
"exports": {
|
|
10
|
+
"import": "./src/index.js",
|
|
11
|
+
"require": "./src/index.cjs"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src"
|
|
15
|
+
],
|
|
16
|
+
"author": "souvik_maji_99",
|
|
17
|
+
"keywords": [
|
|
18
|
+
"password",
|
|
19
|
+
"password-generator",
|
|
20
|
+
"security",
|
|
21
|
+
"crypto"
|
|
22
|
+
],
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/Devil-99/devils-dice.git"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/index.cjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const crypto = require("crypto");
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generate a secure random password
|
|
5
|
+
*/
|
|
6
|
+
function generatePassword({
|
|
7
|
+
length = 8,
|
|
8
|
+
uppercase = true,
|
|
9
|
+
lowercase = true,
|
|
10
|
+
numbers = true,
|
|
11
|
+
symbols = true
|
|
12
|
+
} = {}) {
|
|
13
|
+
if (length < 8) {
|
|
14
|
+
throw new Error("Password length must be at least 8 characters");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
18
|
+
const lower = "abcdefghijklmnopqrstuvwxyz";
|
|
19
|
+
const digits = "0123456789";
|
|
20
|
+
const special = "!@#$%^&*()";
|
|
21
|
+
|
|
22
|
+
let allowedChars = "";
|
|
23
|
+
const requiredChars = [];
|
|
24
|
+
|
|
25
|
+
if (uppercase) {
|
|
26
|
+
allowedChars += upper;
|
|
27
|
+
requiredChars.push(getRandomChar(upper));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (lowercase) {
|
|
31
|
+
allowedChars += lower;
|
|
32
|
+
requiredChars.push(getRandomChar(lower));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (numbers) {
|
|
36
|
+
allowedChars += digits;
|
|
37
|
+
requiredChars.push(getRandomChar(digits));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (symbols) {
|
|
41
|
+
allowedChars += special;
|
|
42
|
+
requiredChars.push(getRandomChar(special));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!allowedChars) {
|
|
46
|
+
throw new Error("At least one character type must be enabled");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const remainingLength = length - requiredChars.length;
|
|
50
|
+
const passwordChars = [...requiredChars];
|
|
51
|
+
|
|
52
|
+
for (let i = 0; i < remainingLength; i++) {
|
|
53
|
+
passwordChars.push(getRandomChar(allowedChars));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return shuffleArray(passwordChars).join("");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function getRandomChar(charset) {
|
|
60
|
+
return charset[crypto.randomInt(0, charset.length)];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function shuffleArray(array) {
|
|
64
|
+
for (let i = array.length - 1; i > 0; i--) {
|
|
65
|
+
const j = crypto.randomInt(0, i + 1);
|
|
66
|
+
[array[i], array[j]] = [array[j], array[i]];
|
|
67
|
+
}
|
|
68
|
+
return array;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
module.exports = { generatePassword };
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import crypto from "crypto";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generate a secure random password
|
|
5
|
+
* @param {Object} options
|
|
6
|
+
* @param {number} options.length - Password length (min 8)
|
|
7
|
+
* @param {boolean} options.uppercase
|
|
8
|
+
* @param {boolean} options.lowercase
|
|
9
|
+
* @param {boolean} options.numbers
|
|
10
|
+
* @param {boolean} options.symbols
|
|
11
|
+
* @returns {string}
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export function generatePassword({
|
|
15
|
+
length = 8,
|
|
16
|
+
uppercase = true,
|
|
17
|
+
lowercase = true,
|
|
18
|
+
numbers = true,
|
|
19
|
+
symbols = true
|
|
20
|
+
} = {}) {
|
|
21
|
+
if (length < 8) {
|
|
22
|
+
throw new Error("Password length must be at least 8 characters");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
26
|
+
const lower = "abcdefghijklmnopqrstuvwxyz";
|
|
27
|
+
const digits = "0123456789";
|
|
28
|
+
const special = "!@#$%^&*()";
|
|
29
|
+
|
|
30
|
+
let allowedChars = "";
|
|
31
|
+
const requiredChars = [];
|
|
32
|
+
|
|
33
|
+
if (uppercase) {
|
|
34
|
+
allowedChars += upper;
|
|
35
|
+
requiredChars.push(getRandomChar(upper));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (lowercase) {
|
|
39
|
+
allowedChars += lower;
|
|
40
|
+
requiredChars.push(getRandomChar(lower));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (numbers) {
|
|
44
|
+
allowedChars += digits;
|
|
45
|
+
requiredChars.push(getRandomChar(digits));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (symbols) {
|
|
49
|
+
allowedChars += special;
|
|
50
|
+
requiredChars.push(getRandomChar(special));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!allowedChars) {
|
|
54
|
+
throw new Error("At least one character type must be enabled");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const remainingLength = length - requiredChars.length;
|
|
58
|
+
const passwordChars = [...requiredChars];
|
|
59
|
+
|
|
60
|
+
for (let i = 0; i < remainingLength; i++) {
|
|
61
|
+
passwordChars.push(getRandomChar(allowedChars));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Shuffle to avoid predictable positions
|
|
65
|
+
return shuffleArray(passwordChars).join("");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get a cryptographically secure random character
|
|
70
|
+
*/
|
|
71
|
+
function getRandomChar(charset) {
|
|
72
|
+
const index = crypto.randomInt(0, charset.length);
|
|
73
|
+
return charset[index];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Fisher-Yates shuffle using crypto randomness
|
|
78
|
+
*/
|
|
79
|
+
function shuffleArray(array) {
|
|
80
|
+
for (let i = array.length - 1; i > 0; i--) {
|
|
81
|
+
const j = crypto.randomInt(0, i + 1);
|
|
82
|
+
[array[i], array[j]] = [array[j], array[i]];
|
|
83
|
+
}
|
|
84
|
+
return array;
|
|
85
|
+
}
|