@systemix/password 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 shahadathhs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # @systemix/password
2
+
3
+ `@systemix/password` is a customizable, lightweight, and cryptographically secure password generator for JavaScript and TypeScript. Built for maximum security, it relies entirely on Node.js built-in `crypto` modules, ensuring zero external dependencies and a low supply-chain risk profile.
4
+
5
+ ## Features
6
+
7
+ - **Secure**: Uses `crypto.getRandomValues()` for cryptographically strong random data.
8
+ - **Zero Dependencies**: Purely built with Node.js built-ins.
9
+ - **TypeScript Ready**: Full type definitions included.
10
+ - **Customizable**: Control length, character sets (numbers, symbols, uppercase, lowercase), and exclude similar characters.
11
+ - **Validation**: Built-in strict validation for generation options.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ pnpm add @systemix/password
17
+ ```
18
+
19
+ or
20
+
21
+ ```bash
22
+ npm install @systemix/password
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Simple Example
28
+
29
+ ```typescript
30
+ import { generatePassword } from '@systemix/password';
31
+
32
+ const password = generatePassword({
33
+ length: 16,
34
+ useNumbers: true,
35
+ useSymbols: true,
36
+ useUppercase: true,
37
+ useLowercase: true,
38
+ });
39
+
40
+ console.log(password); // Example: "Z#kM@4p*J!h2X&b7"
41
+ ```
42
+
43
+ ### Advanced Configuration
44
+
45
+ ```typescript
46
+ const password = generatePassword({
47
+ length: 12,
48
+ useNumbers: true,
49
+ excludeSimilarCharacters: true, // Excludes: i, l, 1, L, o, 0, O
50
+ exclude: 'abc', // Manually exclude specific characters
51
+ });
52
+ ```
53
+
54
+ ## API Reference
55
+
56
+ ### `generatePassword(props?: PasswordProps): string`
57
+
58
+ | Property | Type | Default | Description |
59
+ | :------------------------- | :-------- | :------ | :----------------------------------- |
60
+ | `length` | `number` | `8` | Password length (1 to 50). |
61
+ | `useNumbers` | `boolean` | `true` | Include 0-9. |
62
+ | `useUppercase` | `boolean` | `true` | Include A-Z. |
63
+ | `useLowercase` | `boolean` | `true` | Include a-z. |
64
+ | `useSymbols` | `boolean` | `false` | Include special characters. |
65
+ | `excludeSimilarCharacters` | `boolean` | `false` | Exclude visually similar characters. |
66
+ | `exclude` | `string` | `""` | Specific characters to exclude. |
67
+
68
+ ## License
69
+
70
+ MIT © [shahadathhs](https://github.com/shahadathhs)
package/dist/index.cjs ADDED
@@ -0,0 +1,201 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ GeneratePasswordPropsEnum: () => GeneratePasswordPropsEnum,
24
+ allOptionsFalseError: () => allOptionsFalseError,
25
+ countError: () => countError,
26
+ excludeError: () => excludeError,
27
+ excludeSimilarCharactersError: () => excludeSimilarCharactersError,
28
+ generatePassword: () => generatePassword,
29
+ lengthError: () => lengthError,
30
+ unwantedPropsError: () => unwantedPropsError,
31
+ useLowercaseError: () => useLowercaseError,
32
+ useNumbersError: () => useNumbersError,
33
+ useSymbolsError: () => useSymbolsError,
34
+ useUppercaseError: () => useUppercaseError
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+
38
+ // src/enum/props.enum.ts
39
+ var GeneratePasswordPropsEnum = /* @__PURE__ */ ((GeneratePasswordPropsEnum2) => {
40
+ GeneratePasswordPropsEnum2["LENGTH"] = "length";
41
+ GeneratePasswordPropsEnum2["USE_NUMBERS"] = "useNumbers";
42
+ GeneratePasswordPropsEnum2["USE_UPPERCASE"] = "useUppercase";
43
+ GeneratePasswordPropsEnum2["USE_LOWERCASE"] = "useLowercase";
44
+ GeneratePasswordPropsEnum2["USE_SYMBOLS"] = "useSymbols";
45
+ GeneratePasswordPropsEnum2["EXCLUDE_SIMILAR_CHARACTERS"] = "excludeSimilarCharacters";
46
+ GeneratePasswordPropsEnum2["EXCLUDE"] = "exclude";
47
+ GeneratePasswordPropsEnum2["COUNT"] = "count";
48
+ return GeneratePasswordPropsEnum2;
49
+ })(GeneratePasswordPropsEnum || {});
50
+
51
+ // src/functions/generate.function.ts
52
+ var import_node_crypto = require("crypto");
53
+
54
+ // src/utils/error.message.ts
55
+ var lengthError = {
56
+ invalidLength: "Invalid length. Length must be a positive number.",
57
+ lengthTooLarge: "Invalid length. Length must be less than or equal to 50."
58
+ };
59
+ var useNumbersError = "Invalid useNumbers. It must be a boolean value.";
60
+ var useUppercaseError = "Invalid useUppercase. It must be a boolean value.";
61
+ var useLowercaseError = "Invalid useLowercase. It must be a boolean value.";
62
+ var useSymbolsError = "Invalid useSymbols. It must be a boolean value.";
63
+ var excludeSimilarCharactersError = "Invalid excludeSimilarCharacters. It must be a boolean value.";
64
+ var excludeError = "Invalid exclude. It must be a string.";
65
+ var countError = {
66
+ invalidCount: "Invalid count. Count must be a positive number.",
67
+ countTooLarge: "Invalid count. Count must be less than or equal to 10."
68
+ };
69
+ var unwantedPropsError = (unwantedProps) => {
70
+ return `Invalid prop(s): ${unwantedProps.join(", ")}. Only the following options are allowed: length, useNumbers, useUppercase, useLowercase, useSymbols, excludeSimilarCharacters, exclude, count.`;
71
+ };
72
+ var allOptionsFalseError = "At least one of the options must be true.";
73
+
74
+ // src/functions/validation.function.ts
75
+ var allValidProps = Object.values(GeneratePasswordPropsEnum);
76
+ function generatePasswordPropValidation(props) {
77
+ const propsValue = Object.keys(props);
78
+ const unwantedProps = propsValue.filter(
79
+ (prop) => !allValidProps.includes(prop)
80
+ );
81
+ if (unwantedProps.length > 0) {
82
+ throw new Error(unwantedPropsError(unwantedProps));
83
+ }
84
+ const {
85
+ length,
86
+ useNumbers,
87
+ useUppercase,
88
+ useLowercase,
89
+ useSymbols,
90
+ excludeSimilarCharacters,
91
+ exclude,
92
+ count
93
+ } = props;
94
+ if (typeof length !== "number" || length < 1) {
95
+ throw new Error(lengthError.invalidLength);
96
+ }
97
+ if (length > 50) {
98
+ throw new Error(lengthError.lengthTooLarge);
99
+ }
100
+ if (typeof useNumbers !== "boolean") {
101
+ throw new Error(useNumbersError);
102
+ }
103
+ if (typeof useUppercase !== "boolean") {
104
+ throw new Error(useUppercaseError);
105
+ }
106
+ if (typeof useLowercase !== "boolean") {
107
+ throw new Error(useLowercaseError);
108
+ }
109
+ if (typeof useSymbols !== "boolean") {
110
+ throw new Error(useSymbolsError);
111
+ }
112
+ if (typeof excludeSimilarCharacters !== "boolean") {
113
+ throw new Error(excludeSimilarCharactersError);
114
+ }
115
+ if (typeof exclude !== "string") {
116
+ throw new Error(excludeError);
117
+ }
118
+ if (typeof count !== "number" || count < 1) {
119
+ throw new Error(countError.invalidCount);
120
+ }
121
+ if (count > 10) {
122
+ throw new Error(countError.countTooLarge);
123
+ }
124
+ if (!useNumbers && !useUppercase && !useLowercase && !useSymbols) {
125
+ throw new Error(allOptionsFalseError);
126
+ }
127
+ }
128
+
129
+ // src/functions/generate.function.ts
130
+ function generatePassword(props = {}) {
131
+ const {
132
+ length = 8,
133
+ useNumbers = true,
134
+ useUppercase = true,
135
+ useLowercase = true,
136
+ useSymbols = false,
137
+ excludeSimilarCharacters = false,
138
+ exclude = "",
139
+ count = 1,
140
+ ...extraProps
141
+ } = props;
142
+ const allProps = {
143
+ length,
144
+ useNumbers,
145
+ useUppercase,
146
+ useLowercase,
147
+ useSymbols,
148
+ excludeSimilarCharacters,
149
+ exclude,
150
+ count,
151
+ ...extraProps
152
+ };
153
+ generatePasswordPropValidation(allProps);
154
+ const lowercaseChars = "abcdefghijklmnopqrstuvwxyz";
155
+ const uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
156
+ const numberChars = "0123456789";
157
+ const symbolChars = "@#$%^&*()_+=<>?/|";
158
+ const similarChars = "il1Lo0O";
159
+ let charset = "";
160
+ const charsetOptions = [
161
+ { condition: useLowercase, value: lowercaseChars },
162
+ { condition: useUppercase, value: uppercaseChars },
163
+ { condition: useNumbers, value: numberChars },
164
+ { condition: useSymbols, value: symbolChars }
165
+ ];
166
+ charset = charsetOptions.reduce((acc, { condition, value }) => {
167
+ return condition ? acc + value : acc;
168
+ }, "");
169
+ if (excludeSimilarCharacters) {
170
+ charset = charset.split("").filter((char) => !similarChars.includes(char)).join("");
171
+ }
172
+ if (exclude) {
173
+ charset = charset.split("").filter((char) => !exclude.includes(char)).join("");
174
+ }
175
+ if (charset.length === 0) {
176
+ throw new Error("No valid characters to generate password.");
177
+ }
178
+ const generateSinglePassword = () => {
179
+ const randomIndices = new Uint32Array(length);
180
+ import_node_crypto.webcrypto.getRandomValues(randomIndices);
181
+ const password = Array.from(randomIndices).map((index) => charset[index % charset.length]).join("");
182
+ return password;
183
+ };
184
+ if (count === 1) return generateSinglePassword();
185
+ return Array.from({ length: count }, generateSinglePassword);
186
+ }
187
+ // Annotate the CommonJS export names for ESM import in node:
188
+ 0 && (module.exports = {
189
+ GeneratePasswordPropsEnum,
190
+ allOptionsFalseError,
191
+ countError,
192
+ excludeError,
193
+ excludeSimilarCharactersError,
194
+ generatePassword,
195
+ lengthError,
196
+ unwantedPropsError,
197
+ useLowercaseError,
198
+ useNumbersError,
199
+ useSymbolsError,
200
+ useUppercaseError
201
+ });
@@ -0,0 +1,35 @@
1
+ declare enum GeneratePasswordPropsEnum {
2
+ LENGTH = "length",
3
+ USE_NUMBERS = "useNumbers",
4
+ USE_UPPERCASE = "useUppercase",
5
+ USE_LOWERCASE = "useLowercase",
6
+ USE_SYMBOLS = "useSymbols",
7
+ EXCLUDE_SIMILAR_CHARACTERS = "excludeSimilarCharacters",
8
+ EXCLUDE = "exclude",
9
+ COUNT = "count"
10
+ }
11
+
12
+ type GeneratePasswordFunctionProps = {
13
+ [key in GeneratePasswordPropsEnum]?: key extends GeneratePasswordPropsEnum.LENGTH | GeneratePasswordPropsEnum.COUNT ? number : key extends GeneratePasswordPropsEnum.EXCLUDE ? string : key extends GeneratePasswordPropsEnum.USE_NUMBERS | GeneratePasswordPropsEnum.USE_UPPERCASE | GeneratePasswordPropsEnum.USE_LOWERCASE | GeneratePasswordPropsEnum.USE_SYMBOLS | GeneratePasswordPropsEnum.EXCLUDE_SIMILAR_CHARACTERS ? boolean : never;
14
+ };
15
+
16
+ declare function generatePassword(props?: GeneratePasswordFunctionProps): string | string[];
17
+
18
+ declare const lengthError: {
19
+ invalidLength: string;
20
+ lengthTooLarge: string;
21
+ };
22
+ declare const useNumbersError = "Invalid useNumbers. It must be a boolean value.";
23
+ declare const useUppercaseError = "Invalid useUppercase. It must be a boolean value.";
24
+ declare const useLowercaseError = "Invalid useLowercase. It must be a boolean value.";
25
+ declare const useSymbolsError = "Invalid useSymbols. It must be a boolean value.";
26
+ declare const excludeSimilarCharactersError = "Invalid excludeSimilarCharacters. It must be a boolean value.";
27
+ declare const excludeError = "Invalid exclude. It must be a string.";
28
+ declare const countError: {
29
+ invalidCount: string;
30
+ countTooLarge: string;
31
+ };
32
+ declare const unwantedPropsError: (unwantedProps: string[]) => string;
33
+ declare const allOptionsFalseError = "At least one of the options must be true.";
34
+
35
+ export { type GeneratePasswordFunctionProps, GeneratePasswordPropsEnum, allOptionsFalseError, countError, excludeError, excludeSimilarCharactersError, generatePassword, lengthError, unwantedPropsError, useLowercaseError, useNumbersError, useSymbolsError, useUppercaseError };
@@ -0,0 +1,35 @@
1
+ declare enum GeneratePasswordPropsEnum {
2
+ LENGTH = "length",
3
+ USE_NUMBERS = "useNumbers",
4
+ USE_UPPERCASE = "useUppercase",
5
+ USE_LOWERCASE = "useLowercase",
6
+ USE_SYMBOLS = "useSymbols",
7
+ EXCLUDE_SIMILAR_CHARACTERS = "excludeSimilarCharacters",
8
+ EXCLUDE = "exclude",
9
+ COUNT = "count"
10
+ }
11
+
12
+ type GeneratePasswordFunctionProps = {
13
+ [key in GeneratePasswordPropsEnum]?: key extends GeneratePasswordPropsEnum.LENGTH | GeneratePasswordPropsEnum.COUNT ? number : key extends GeneratePasswordPropsEnum.EXCLUDE ? string : key extends GeneratePasswordPropsEnum.USE_NUMBERS | GeneratePasswordPropsEnum.USE_UPPERCASE | GeneratePasswordPropsEnum.USE_LOWERCASE | GeneratePasswordPropsEnum.USE_SYMBOLS | GeneratePasswordPropsEnum.EXCLUDE_SIMILAR_CHARACTERS ? boolean : never;
14
+ };
15
+
16
+ declare function generatePassword(props?: GeneratePasswordFunctionProps): string | string[];
17
+
18
+ declare const lengthError: {
19
+ invalidLength: string;
20
+ lengthTooLarge: string;
21
+ };
22
+ declare const useNumbersError = "Invalid useNumbers. It must be a boolean value.";
23
+ declare const useUppercaseError = "Invalid useUppercase. It must be a boolean value.";
24
+ declare const useLowercaseError = "Invalid useLowercase. It must be a boolean value.";
25
+ declare const useSymbolsError = "Invalid useSymbols. It must be a boolean value.";
26
+ declare const excludeSimilarCharactersError = "Invalid excludeSimilarCharacters. It must be a boolean value.";
27
+ declare const excludeError = "Invalid exclude. It must be a string.";
28
+ declare const countError: {
29
+ invalidCount: string;
30
+ countTooLarge: string;
31
+ };
32
+ declare const unwantedPropsError: (unwantedProps: string[]) => string;
33
+ declare const allOptionsFalseError = "At least one of the options must be true.";
34
+
35
+ export { type GeneratePasswordFunctionProps, GeneratePasswordPropsEnum, allOptionsFalseError, countError, excludeError, excludeSimilarCharactersError, generatePassword, lengthError, unwantedPropsError, useLowercaseError, useNumbersError, useSymbolsError, useUppercaseError };
@@ -0,0 +1,35 @@
1
+ declare enum GeneratePasswordPropsEnum {
2
+ LENGTH = "length",
3
+ USE_NUMBERS = "useNumbers",
4
+ USE_UPPERCASE = "useUppercase",
5
+ USE_LOWERCASE = "useLowercase",
6
+ USE_SYMBOLS = "useSymbols",
7
+ EXCLUDE_SIMILAR_CHARACTERS = "excludeSimilarCharacters",
8
+ EXCLUDE = "exclude",
9
+ COUNT = "count"
10
+ }
11
+
12
+ type GeneratePasswordFunctionProps = {
13
+ [key in GeneratePasswordPropsEnum]?: key extends GeneratePasswordPropsEnum.LENGTH | GeneratePasswordPropsEnum.COUNT ? number : key extends GeneratePasswordPropsEnum.EXCLUDE ? string : key extends GeneratePasswordPropsEnum.USE_NUMBERS | GeneratePasswordPropsEnum.USE_UPPERCASE | GeneratePasswordPropsEnum.USE_LOWERCASE | GeneratePasswordPropsEnum.USE_SYMBOLS | GeneratePasswordPropsEnum.EXCLUDE_SIMILAR_CHARACTERS ? boolean : never;
14
+ };
15
+
16
+ declare function generatePassword(props?: GeneratePasswordFunctionProps): string | string[];
17
+
18
+ declare const lengthError: {
19
+ invalidLength: string;
20
+ lengthTooLarge: string;
21
+ };
22
+ declare const useNumbersError = "Invalid useNumbers. It must be a boolean value.";
23
+ declare const useUppercaseError = "Invalid useUppercase. It must be a boolean value.";
24
+ declare const useLowercaseError = "Invalid useLowercase. It must be a boolean value.";
25
+ declare const useSymbolsError = "Invalid useSymbols. It must be a boolean value.";
26
+ declare const excludeSimilarCharactersError = "Invalid excludeSimilarCharacters. It must be a boolean value.";
27
+ declare const excludeError = "Invalid exclude. It must be a string.";
28
+ declare const countError: {
29
+ invalidCount: string;
30
+ countTooLarge: string;
31
+ };
32
+ declare const unwantedPropsError: (unwantedProps: string[]) => string;
33
+ declare const allOptionsFalseError = "At least one of the options must be true.";
34
+
35
+ export { type GeneratePasswordFunctionProps, GeneratePasswordPropsEnum, allOptionsFalseError, countError, excludeError, excludeSimilarCharactersError, generatePassword, lengthError, unwantedPropsError, useLowercaseError, useNumbersError, useSymbolsError, useUppercaseError };
package/dist/index.js ADDED
@@ -0,0 +1,163 @@
1
+ // src/enum/props.enum.ts
2
+ var GeneratePasswordPropsEnum = /* @__PURE__ */ ((GeneratePasswordPropsEnum2) => {
3
+ GeneratePasswordPropsEnum2["LENGTH"] = "length";
4
+ GeneratePasswordPropsEnum2["USE_NUMBERS"] = "useNumbers";
5
+ GeneratePasswordPropsEnum2["USE_UPPERCASE"] = "useUppercase";
6
+ GeneratePasswordPropsEnum2["USE_LOWERCASE"] = "useLowercase";
7
+ GeneratePasswordPropsEnum2["USE_SYMBOLS"] = "useSymbols";
8
+ GeneratePasswordPropsEnum2["EXCLUDE_SIMILAR_CHARACTERS"] = "excludeSimilarCharacters";
9
+ GeneratePasswordPropsEnum2["EXCLUDE"] = "exclude";
10
+ GeneratePasswordPropsEnum2["COUNT"] = "count";
11
+ return GeneratePasswordPropsEnum2;
12
+ })(GeneratePasswordPropsEnum || {});
13
+
14
+ // src/functions/generate.function.ts
15
+ import { webcrypto as crypto } from "crypto";
16
+
17
+ // src/utils/error.message.ts
18
+ var lengthError = {
19
+ invalidLength: "Invalid length. Length must be a positive number.",
20
+ lengthTooLarge: "Invalid length. Length must be less than or equal to 50."
21
+ };
22
+ var useNumbersError = "Invalid useNumbers. It must be a boolean value.";
23
+ var useUppercaseError = "Invalid useUppercase. It must be a boolean value.";
24
+ var useLowercaseError = "Invalid useLowercase. It must be a boolean value.";
25
+ var useSymbolsError = "Invalid useSymbols. It must be a boolean value.";
26
+ var excludeSimilarCharactersError = "Invalid excludeSimilarCharacters. It must be a boolean value.";
27
+ var excludeError = "Invalid exclude. It must be a string.";
28
+ var countError = {
29
+ invalidCount: "Invalid count. Count must be a positive number.",
30
+ countTooLarge: "Invalid count. Count must be less than or equal to 10."
31
+ };
32
+ var unwantedPropsError = (unwantedProps) => {
33
+ return `Invalid prop(s): ${unwantedProps.join(", ")}. Only the following options are allowed: length, useNumbers, useUppercase, useLowercase, useSymbols, excludeSimilarCharacters, exclude, count.`;
34
+ };
35
+ var allOptionsFalseError = "At least one of the options must be true.";
36
+
37
+ // src/functions/validation.function.ts
38
+ var allValidProps = Object.values(GeneratePasswordPropsEnum);
39
+ function generatePasswordPropValidation(props) {
40
+ const propsValue = Object.keys(props);
41
+ const unwantedProps = propsValue.filter(
42
+ (prop) => !allValidProps.includes(prop)
43
+ );
44
+ if (unwantedProps.length > 0) {
45
+ throw new Error(unwantedPropsError(unwantedProps));
46
+ }
47
+ const {
48
+ length,
49
+ useNumbers,
50
+ useUppercase,
51
+ useLowercase,
52
+ useSymbols,
53
+ excludeSimilarCharacters,
54
+ exclude,
55
+ count
56
+ } = props;
57
+ if (typeof length !== "number" || length < 1) {
58
+ throw new Error(lengthError.invalidLength);
59
+ }
60
+ if (length > 50) {
61
+ throw new Error(lengthError.lengthTooLarge);
62
+ }
63
+ if (typeof useNumbers !== "boolean") {
64
+ throw new Error(useNumbersError);
65
+ }
66
+ if (typeof useUppercase !== "boolean") {
67
+ throw new Error(useUppercaseError);
68
+ }
69
+ if (typeof useLowercase !== "boolean") {
70
+ throw new Error(useLowercaseError);
71
+ }
72
+ if (typeof useSymbols !== "boolean") {
73
+ throw new Error(useSymbolsError);
74
+ }
75
+ if (typeof excludeSimilarCharacters !== "boolean") {
76
+ throw new Error(excludeSimilarCharactersError);
77
+ }
78
+ if (typeof exclude !== "string") {
79
+ throw new Error(excludeError);
80
+ }
81
+ if (typeof count !== "number" || count < 1) {
82
+ throw new Error(countError.invalidCount);
83
+ }
84
+ if (count > 10) {
85
+ throw new Error(countError.countTooLarge);
86
+ }
87
+ if (!useNumbers && !useUppercase && !useLowercase && !useSymbols) {
88
+ throw new Error(allOptionsFalseError);
89
+ }
90
+ }
91
+
92
+ // src/functions/generate.function.ts
93
+ function generatePassword(props = {}) {
94
+ const {
95
+ length = 8,
96
+ useNumbers = true,
97
+ useUppercase = true,
98
+ useLowercase = true,
99
+ useSymbols = false,
100
+ excludeSimilarCharacters = false,
101
+ exclude = "",
102
+ count = 1,
103
+ ...extraProps
104
+ } = props;
105
+ const allProps = {
106
+ length,
107
+ useNumbers,
108
+ useUppercase,
109
+ useLowercase,
110
+ useSymbols,
111
+ excludeSimilarCharacters,
112
+ exclude,
113
+ count,
114
+ ...extraProps
115
+ };
116
+ generatePasswordPropValidation(allProps);
117
+ const lowercaseChars = "abcdefghijklmnopqrstuvwxyz";
118
+ const uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
119
+ const numberChars = "0123456789";
120
+ const symbolChars = "@#$%^&*()_+=<>?/|";
121
+ const similarChars = "il1Lo0O";
122
+ let charset = "";
123
+ const charsetOptions = [
124
+ { condition: useLowercase, value: lowercaseChars },
125
+ { condition: useUppercase, value: uppercaseChars },
126
+ { condition: useNumbers, value: numberChars },
127
+ { condition: useSymbols, value: symbolChars }
128
+ ];
129
+ charset = charsetOptions.reduce((acc, { condition, value }) => {
130
+ return condition ? acc + value : acc;
131
+ }, "");
132
+ if (excludeSimilarCharacters) {
133
+ charset = charset.split("").filter((char) => !similarChars.includes(char)).join("");
134
+ }
135
+ if (exclude) {
136
+ charset = charset.split("").filter((char) => !exclude.includes(char)).join("");
137
+ }
138
+ if (charset.length === 0) {
139
+ throw new Error("No valid characters to generate password.");
140
+ }
141
+ const generateSinglePassword = () => {
142
+ const randomIndices = new Uint32Array(length);
143
+ crypto.getRandomValues(randomIndices);
144
+ const password = Array.from(randomIndices).map((index) => charset[index % charset.length]).join("");
145
+ return password;
146
+ };
147
+ if (count === 1) return generateSinglePassword();
148
+ return Array.from({ length: count }, generateSinglePassword);
149
+ }
150
+ export {
151
+ GeneratePasswordPropsEnum,
152
+ allOptionsFalseError,
153
+ countError,
154
+ excludeError,
155
+ excludeSimilarCharactersError,
156
+ generatePassword,
157
+ lengthError,
158
+ unwantedPropsError,
159
+ useLowercaseError,
160
+ useNumbersError,
161
+ useSymbolsError,
162
+ useUppercaseError
163
+ };
package/dist/index.mjs ADDED
@@ -0,0 +1,158 @@
1
+ // src/enum/props.enum.ts
2
+ var GeneratePasswordPropsEnum = /* @__PURE__ */ ((GeneratePasswordPropsEnum2) => {
3
+ GeneratePasswordPropsEnum2["LENGTH"] = "length";
4
+ GeneratePasswordPropsEnum2["USE_NUMBERS"] = "useNumbers";
5
+ GeneratePasswordPropsEnum2["USE_UPPERCASE"] = "useUppercase";
6
+ GeneratePasswordPropsEnum2["USE_LOWERCASE"] = "useLowercase";
7
+ GeneratePasswordPropsEnum2["USE_SYMBOLS"] = "useSymbols";
8
+ GeneratePasswordPropsEnum2["EXCLUDE_SIMILAR_CHARACTERS"] = "excludeSimilarCharacters";
9
+ GeneratePasswordPropsEnum2["EXCLUDE"] = "exclude";
10
+ GeneratePasswordPropsEnum2["COUNT"] = "count";
11
+ return GeneratePasswordPropsEnum2;
12
+ })(GeneratePasswordPropsEnum || {});
13
+
14
+ // src/utils/error.message.ts
15
+ var lengthError = {
16
+ invalidLength: "Invalid length. Length must be a positive number.",
17
+ lengthTooLarge: "Invalid length. Length must be less than or equal to 50."
18
+ };
19
+ var useNumbersError = "Invalid useNumbers. It must be a boolean value.";
20
+ var useUppercaseError = "Invalid useUppercase. It must be a boolean value.";
21
+ var useLowercaseError = "Invalid useLowercase. It must be a boolean value.";
22
+ var useSymbolsError = "Invalid useSymbols. It must be a boolean value.";
23
+ var excludeSimilarCharactersError = "Invalid excludeSimilarCharacters. It must be a boolean value.";
24
+ var excludeError = "Invalid exclude. It must be a string.";
25
+ var countError = {
26
+ invalidCount: "Invalid count. Count must be a positive number.",
27
+ countTooLarge: "Invalid count. Count must be less than or equal to 10."
28
+ };
29
+ var unwantedPropsError = (unwantedProps) => {
30
+ return `Invalid prop(s): ${unwantedProps.join(", ")}. Only the following options are allowed: length, useNumbers, useUppercase, useLowercase, useSymbols, excludeSimilarCharacters, exclude, count.`;
31
+ };
32
+ var allOptionsFalseError = "At least one of the options must be true.";
33
+
34
+ // src/functions/validation.function.ts
35
+ var allValidProps = Object.values(GeneratePasswordPropsEnum);
36
+ function generatePasswordPropValidation(props) {
37
+ const propsValue = Object.keys(props);
38
+ const unwantedProps = propsValue.filter((prop) => !allValidProps.includes(prop));
39
+ if (unwantedProps.length > 0) {
40
+ throw new Error(unwantedPropsError(unwantedProps));
41
+ }
42
+ const {
43
+ length,
44
+ useNumbers,
45
+ useUppercase,
46
+ useLowercase,
47
+ useSymbols,
48
+ excludeSimilarCharacters,
49
+ exclude,
50
+ count
51
+ } = props;
52
+ if (typeof length !== "number" || length < 1) {
53
+ throw new Error(lengthError.invalidLength);
54
+ }
55
+ if (length > 50) {
56
+ throw new Error(lengthError.lengthTooLarge);
57
+ }
58
+ if (typeof useNumbers !== "boolean") {
59
+ throw new Error(useNumbersError);
60
+ }
61
+ if (typeof useUppercase !== "boolean") {
62
+ throw new Error(useUppercaseError);
63
+ }
64
+ if (typeof useLowercase !== "boolean") {
65
+ throw new Error(useLowercaseError);
66
+ }
67
+ if (typeof useSymbols !== "boolean") {
68
+ throw new Error(useSymbolsError);
69
+ }
70
+ if (typeof excludeSimilarCharacters !== "boolean") {
71
+ throw new Error(excludeSimilarCharactersError);
72
+ }
73
+ if (typeof exclude !== "string") {
74
+ throw new Error(excludeError);
75
+ }
76
+ if (typeof count !== "number" || count < 1) {
77
+ throw new Error(countError.invalidCount);
78
+ }
79
+ if (count > 10) {
80
+ throw new Error(countError.countTooLarge);
81
+ }
82
+ if (!useNumbers && !useUppercase && !useLowercase && !useSymbols) {
83
+ throw new Error(allOptionsFalseError);
84
+ }
85
+ }
86
+
87
+ // src/functions/generate.function.ts
88
+ function generatePassword(props = {}) {
89
+ const {
90
+ length = 8,
91
+ useNumbers = true,
92
+ useUppercase = true,
93
+ useLowercase = true,
94
+ useSymbols = false,
95
+ excludeSimilarCharacters = false,
96
+ exclude = "",
97
+ count = 1,
98
+ ...extraProps
99
+ } = props;
100
+ const allProps = {
101
+ length,
102
+ useNumbers,
103
+ useUppercase,
104
+ useLowercase,
105
+ useSymbols,
106
+ excludeSimilarCharacters,
107
+ exclude,
108
+ count,
109
+ ...extraProps
110
+ };
111
+ generatePasswordPropValidation(allProps);
112
+ const lowercaseChars = "abcdefghijklmnopqrstuvwxyz";
113
+ const uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
114
+ const numberChars = "0123456789";
115
+ const symbolChars = "@#$%^&*()_+=<>?/|";
116
+ const similarChars = "il1Lo0O";
117
+ let charset = "";
118
+ const charsetOptions = [
119
+ { condition: useLowercase, value: lowercaseChars },
120
+ { condition: useUppercase, value: uppercaseChars },
121
+ { condition: useNumbers, value: numberChars },
122
+ { condition: useSymbols, value: symbolChars }
123
+ ];
124
+ charset = charsetOptions.reduce((acc, { condition, value }) => {
125
+ return condition ? acc + value : acc;
126
+ }, "");
127
+ if (excludeSimilarCharacters) {
128
+ charset = charset.split("").filter((char) => !similarChars.includes(char)).join("");
129
+ }
130
+ if (exclude) {
131
+ charset = charset.split("").filter((char) => !exclude.includes(char)).join("");
132
+ }
133
+ if (charset.length === 0) {
134
+ throw new Error("No valid characters to generate password.");
135
+ }
136
+ const generateSinglePassword = () => {
137
+ const randomIndices = new Uint32Array(length);
138
+ crypto.getRandomValues(randomIndices);
139
+ const password = Array.from(randomIndices).map((index) => charset[index % charset.length]).join("");
140
+ return password;
141
+ };
142
+ if (count === 1) return generateSinglePassword();
143
+ return Array.from({ length: count }, generateSinglePassword);
144
+ }
145
+ export {
146
+ GeneratePasswordPropsEnum,
147
+ allOptionsFalseError,
148
+ countError,
149
+ excludeError,
150
+ excludeSimilarCharactersError,
151
+ generatePassword,
152
+ lengthError,
153
+ unwantedPropsError,
154
+ useLowercaseError,
155
+ useNumbersError,
156
+ useSymbolsError,
157
+ useUppercaseError
158
+ };
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@systemix/password",
3
+ "version": "0.0.1",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "private": false,
8
+ "type": "module",
9
+ "main": "./dist/index.js",
10
+ "module": "./dist/index.mjs",
11
+ "types": "./dist/index.d.ts",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/shahadathhs/systemix.git",
15
+ "directory": "packages/password"
16
+ },
17
+ "homepage": "https://github.com/shahadathhs/systemix/tree/main/packages/password#readme",
18
+ "scripts": {
19
+ "build": "tsup src/index.ts --format cjs,esm --dts",
20
+ "typecheck": "tsc --noEmit",
21
+ "lint": "eslint .",
22
+ "lint:fix": "eslint . --fix",
23
+ "format": "prettier --check . --ignore-path .prettierignore",
24
+ "format:fix": "prettier --write . --ignore-path .prettierignore",
25
+ "clean": "rimraf dist node_modules",
26
+ "clean:turbo": "rimraf .turbo dist node_modules",
27
+ "prepublishOnly": "pnpm build"
28
+ },
29
+ "keywords": [
30
+ "systemix",
31
+ "password",
32
+ "generator",
33
+ "password-generator",
34
+ "secure-password",
35
+ "strong-passwords",
36
+ "crypto",
37
+ "typescript",
38
+ "node",
39
+ "utility"
40
+ ],
41
+ "author": "Shahadath Hossain <shahadath.dev@gmail.com> (https://github.com/shahadathhs)",
42
+ "license": "MIT",
43
+ "description": "A cryptographically secure password generator with customizable complexity. Part of the Systemix toolkit.",
44
+ "devDependencies": {
45
+ "@systemix/eslint": "workspace:*",
46
+ "@systemix/typescript": "workspace:*",
47
+ "@types/node": "^25.2.3",
48
+ "eslint": "^10.0.0",
49
+ "tsup": "^8.5.1",
50
+ "typescript": "^5.9.3"
51
+ },
52
+ "directories": {
53
+ "test": "tests"
54
+ },
55
+ "sideEffects": [
56
+ "*.css",
57
+ "*.scss"
58
+ ],
59
+ "bugs": {
60
+ "url": "https://github.com/shahadathhs/systemix/issues"
61
+ },
62
+ "files": [
63
+ "dist",
64
+ "README.md",
65
+ "package.json"
66
+ ]
67
+ }