typesecure 0.1.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/LICENSE +21 -0
- package/README.md +165 -0
- package/dist/index.d.mts +237 -0
- package/dist/index.d.ts +237 -0
- package/dist/index.js +720 -0
- package/dist/index.mjs +662 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Arvid Berndtsson
|
|
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,165 @@
|
|
|
1
|
+
# typesecure
|
|
2
|
+
|
|
3
|
+
A focused TypeScript cryptography package that provides secure encryption and hashing utilities with strong typing and runtime validation using Zod.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🔐 **Strong Typing**: Built with TypeScript for complete type safety.
|
|
8
|
+
- ✅ **Runtime Validation**: Uses Zod to validate inputs and ensure security.
|
|
9
|
+
- 🔍 **Advanced Encryption**: AES encryption with multiple modes (CBC, CTR, GCM, ECB).
|
|
10
|
+
- 🛡️ **Authenticated Encryption**: GCM mode for authenticated encryption with additional data (AAD).
|
|
11
|
+
- 🔏 **Cryptographic Hashing**: SHA-256, SHA-512, SHA-3, and more.
|
|
12
|
+
- 📝 **HMAC Signatures**: Create and verify message authentication codes.
|
|
13
|
+
- ⏱️ **Timing-Safe Comparison**: Prevent timing attacks with constant-time string comparison.
|
|
14
|
+
- 🚦 **Security Level Assessment**: Analyze and report the security level of encryption configurations.
|
|
15
|
+
- 🔑 **Password Hashing**: PBKDF2 for secure password hashing with salt and configurable iterations.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# Using npm
|
|
21
|
+
npm install typesecure
|
|
22
|
+
|
|
23
|
+
# Using yarn
|
|
24
|
+
yarn add typesecure
|
|
25
|
+
|
|
26
|
+
# Using pnpm
|
|
27
|
+
pnpm add typesecure
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
### Encryption with Security Assessment
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { encrypt, decrypt, generateKey, getSecurityLevel, SecurityLevel } from 'typesecure';
|
|
36
|
+
|
|
37
|
+
// Generate a secure key
|
|
38
|
+
const key = generateKey();
|
|
39
|
+
|
|
40
|
+
// Encrypt data with GCM (authenticated encryption)
|
|
41
|
+
const encrypted = encrypt('Sensitive information', key, {
|
|
42
|
+
mode: 'aes-gcm',
|
|
43
|
+
aad: 'Additional authenticated data' // Optional
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Decrypt data
|
|
47
|
+
const decrypted = decrypt(encrypted, key, {
|
|
48
|
+
mode: 'aes-gcm',
|
|
49
|
+
aad: 'Additional authenticated data' // Must match encryption
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Assess security level of encryption options
|
|
53
|
+
const securityLevel = getSecurityLevel({ mode: 'aes-cbc', padding: 'Pkcs7' });
|
|
54
|
+
if (securityLevel === SecurityLevel.HIGH) {
|
|
55
|
+
console.log('Using high security encryption configuration');
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Secure Password Storage
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { hashPassword, verifyPassword } from 'typesecure';
|
|
63
|
+
|
|
64
|
+
// Hash a password with PBKDF2
|
|
65
|
+
const { hash, salt, params } = hashPassword('userPassword123', {
|
|
66
|
+
algorithm: 'pbkdf2',
|
|
67
|
+
iterations: 10000,
|
|
68
|
+
saltLength: 32,
|
|
69
|
+
keyLength: 64
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Store hash, salt, and params in your database
|
|
73
|
+
|
|
74
|
+
// Later, verify the password
|
|
75
|
+
const isValid = verifyPassword('userPassword123', hash, salt, params);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Timing-Safe Comparison and Random Bytes Generation
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { timingSafeEqual, generateRandomBytes } from 'typesecure';
|
|
82
|
+
|
|
83
|
+
// Compare strings in constant time to prevent timing attacks
|
|
84
|
+
const isEqual = timingSafeEqual(userProvidedToken, storedToken);
|
|
85
|
+
|
|
86
|
+
// Generate cryptographically secure random bytes
|
|
87
|
+
const randomBytes = generateRandomBytes(32, 'hex');
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Hashing and HMAC
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { hash, verifyHash, hmac } from 'typesecure';
|
|
94
|
+
|
|
95
|
+
// Create a hash
|
|
96
|
+
const hashedValue = hash('data to hash', {
|
|
97
|
+
algorithm: 'sha256',
|
|
98
|
+
encoding: 'hex'
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Verify a hash
|
|
102
|
+
const isMatch = verifyHash('data to hash', hashedValue, {
|
|
103
|
+
algorithm: 'sha256',
|
|
104
|
+
encoding: 'hex'
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Create an HMAC
|
|
108
|
+
const signature = hmac('message', 'secret key', {
|
|
109
|
+
algorithm: 'sha256',
|
|
110
|
+
encoding: 'base64'
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## API Reference
|
|
115
|
+
|
|
116
|
+
### Encryption
|
|
117
|
+
|
|
118
|
+
- `encrypt(text: string, key: string, options?: Partial<EncryptionOptions>): string`
|
|
119
|
+
- `decrypt(encryptedText: string, key: string, options?: Partial<EncryptionOptions>): string`
|
|
120
|
+
- `generateKey(length?: number): string`
|
|
121
|
+
- `getSecurityLevel(options: EncryptionOptions): SecurityLevel`
|
|
122
|
+
|
|
123
|
+
### Secure Password Storage
|
|
124
|
+
|
|
125
|
+
- `hashPassword(password: string, options?: Partial<PasswordHashOptions>): { hash: string; salt: string; params: PasswordHashOptions }`
|
|
126
|
+
- `verifyPassword(password: string, hash: string, salt: string, options?: Partial<PasswordHashOptions>): boolean`
|
|
127
|
+
- `timingSafeEqual(a: string, b: string, options?: Partial<TimingSafeOptions>): boolean`
|
|
128
|
+
- `generateRandomBytes(length?: number, encoding?: 'hex' | 'base64'): string`
|
|
129
|
+
|
|
130
|
+
### Hashing
|
|
131
|
+
|
|
132
|
+
- `hash(input: string, options?: Partial<HashOptions>): string`
|
|
133
|
+
- `verifyHash(input: string, hashedValue: string, options?: Partial<HashOptions>): boolean`
|
|
134
|
+
- `hmac(input: string, key: string, options?: Partial<HashOptions>): string`
|
|
135
|
+
|
|
136
|
+
## Security Considerations
|
|
137
|
+
|
|
138
|
+
This package implements best practices for cryptographic operations, but remember that cryptography is complex. For production applications with high security requirements, consider:
|
|
139
|
+
|
|
140
|
+
1. Consulting a security professional
|
|
141
|
+
2. Using specialized security libraries
|
|
142
|
+
3. Keeping dependencies updated
|
|
143
|
+
4. Implementing proper key management
|
|
144
|
+
5. Using hardware security modules (HSMs) for key storage when possible
|
|
145
|
+
6. Conducting regular security audits
|
|
146
|
+
7. Following the latest NIST recommendations
|
|
147
|
+
|
|
148
|
+
## Development
|
|
149
|
+
|
|
150
|
+
To contribute to this project:
|
|
151
|
+
|
|
152
|
+
1. Clone the repository
|
|
153
|
+
2. Install dependencies with `pnpm install`
|
|
154
|
+
3. Run tests with `pnpm test`
|
|
155
|
+
4. Build the package with `pnpm build`
|
|
156
|
+
|
|
157
|
+
This project uses TypeScript for type safety, Jest for testing, and ESLint for code quality.
|
|
158
|
+
|
|
159
|
+
## License
|
|
160
|
+
|
|
161
|
+
MIT © [Arvid Berndtsson](https://github.com/arvid-berndtsson)
|
|
162
|
+
|
|
163
|
+
## Contributing
|
|
164
|
+
|
|
165
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
declare const PasswordStrengthSchema: z.ZodObject<{
|
|
4
|
+
score: z.ZodNumber;
|
|
5
|
+
feedback: z.ZodObject<{
|
|
6
|
+
warning: z.ZodOptional<z.ZodString>;
|
|
7
|
+
suggestions: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
8
|
+
}, "strip", z.ZodTypeAny, {
|
|
9
|
+
warning?: string | undefined;
|
|
10
|
+
suggestions?: string[] | undefined;
|
|
11
|
+
}, {
|
|
12
|
+
warning?: string | undefined;
|
|
13
|
+
suggestions?: string[] | undefined;
|
|
14
|
+
}>;
|
|
15
|
+
isStrong: z.ZodBoolean;
|
|
16
|
+
}, "strip", z.ZodTypeAny, {
|
|
17
|
+
score: number;
|
|
18
|
+
feedback: {
|
|
19
|
+
warning?: string | undefined;
|
|
20
|
+
suggestions?: string[] | undefined;
|
|
21
|
+
};
|
|
22
|
+
isStrong: boolean;
|
|
23
|
+
}, {
|
|
24
|
+
score: number;
|
|
25
|
+
feedback: {
|
|
26
|
+
warning?: string | undefined;
|
|
27
|
+
suggestions?: string[] | undefined;
|
|
28
|
+
};
|
|
29
|
+
isStrong: boolean;
|
|
30
|
+
}>;
|
|
31
|
+
type PasswordStrength = z.infer<typeof PasswordStrengthSchema>;
|
|
32
|
+
declare const HashOptionsSchema: z.ZodObject<{
|
|
33
|
+
algorithm: z.ZodDefault<z.ZodEnum<["md5", "sha1", "sha256", "sha512", "sha3"]>>;
|
|
34
|
+
encoding: z.ZodDefault<z.ZodEnum<["hex", "base64"]>>;
|
|
35
|
+
}, "strip", z.ZodTypeAny, {
|
|
36
|
+
algorithm: "md5" | "sha1" | "sha256" | "sha512" | "sha3";
|
|
37
|
+
encoding: "hex" | "base64";
|
|
38
|
+
}, {
|
|
39
|
+
algorithm?: "md5" | "sha1" | "sha256" | "sha512" | "sha3" | undefined;
|
|
40
|
+
encoding?: "hex" | "base64" | undefined;
|
|
41
|
+
}>;
|
|
42
|
+
type HashOptions = z.infer<typeof HashOptionsSchema>;
|
|
43
|
+
declare const PasswordHashOptionsSchema: z.ZodObject<{
|
|
44
|
+
algorithm: z.ZodDefault<z.ZodEnum<["pbkdf2", "argon2", "bcrypt"]>>;
|
|
45
|
+
iterations: z.ZodDefault<z.ZodNumber>;
|
|
46
|
+
saltLength: z.ZodDefault<z.ZodNumber>;
|
|
47
|
+
keyLength: z.ZodDefault<z.ZodNumber>;
|
|
48
|
+
}, "strip", z.ZodTypeAny, {
|
|
49
|
+
algorithm: "pbkdf2" | "argon2" | "bcrypt";
|
|
50
|
+
iterations: number;
|
|
51
|
+
saltLength: number;
|
|
52
|
+
keyLength: number;
|
|
53
|
+
}, {
|
|
54
|
+
algorithm?: "pbkdf2" | "argon2" | "bcrypt" | undefined;
|
|
55
|
+
iterations?: number | undefined;
|
|
56
|
+
saltLength?: number | undefined;
|
|
57
|
+
keyLength?: number | undefined;
|
|
58
|
+
}>;
|
|
59
|
+
type PasswordHashOptions = z.infer<typeof PasswordHashOptionsSchema>;
|
|
60
|
+
declare const TimingSafeOptionsSchema: z.ZodObject<{
|
|
61
|
+
encoding: z.ZodDefault<z.ZodEnum<["utf8", "hex", "base64"]>>;
|
|
62
|
+
}, "strip", z.ZodTypeAny, {
|
|
63
|
+
encoding: "hex" | "base64" | "utf8";
|
|
64
|
+
}, {
|
|
65
|
+
encoding?: "hex" | "base64" | "utf8" | undefined;
|
|
66
|
+
}>;
|
|
67
|
+
type TimingSafeOptions = z.infer<typeof TimingSafeOptionsSchema>;
|
|
68
|
+
declare const EncryptionOptionsSchema: z.ZodObject<{
|
|
69
|
+
mode: z.ZodDefault<z.ZodEnum<["aes-cbc", "aes-ctr", "aes-ecb", "aes-gcm"]>>;
|
|
70
|
+
padding: z.ZodDefault<z.ZodEnum<["Pkcs7", "NoPadding", "ZeroPadding"]>>;
|
|
71
|
+
iv: z.ZodOptional<z.ZodString>;
|
|
72
|
+
authTag: z.ZodOptional<z.ZodString>;
|
|
73
|
+
aad: z.ZodOptional<z.ZodString>;
|
|
74
|
+
}, "strip", z.ZodTypeAny, {
|
|
75
|
+
mode: "aes-cbc" | "aes-ctr" | "aes-ecb" | "aes-gcm";
|
|
76
|
+
padding: "Pkcs7" | "NoPadding" | "ZeroPadding";
|
|
77
|
+
iv?: string | undefined;
|
|
78
|
+
authTag?: string | undefined;
|
|
79
|
+
aad?: string | undefined;
|
|
80
|
+
}, {
|
|
81
|
+
mode?: "aes-cbc" | "aes-ctr" | "aes-ecb" | "aes-gcm" | undefined;
|
|
82
|
+
padding?: "Pkcs7" | "NoPadding" | "ZeroPadding" | undefined;
|
|
83
|
+
iv?: string | undefined;
|
|
84
|
+
authTag?: string | undefined;
|
|
85
|
+
aad?: string | undefined;
|
|
86
|
+
}>;
|
|
87
|
+
type EncryptionOptions = z.infer<typeof EncryptionOptionsSchema>;
|
|
88
|
+
declare const ConfigSchema: z.ZodObject<{
|
|
89
|
+
minimumPasswordLength: z.ZodDefault<z.ZodNumber>;
|
|
90
|
+
requireNumbers: z.ZodDefault<z.ZodBoolean>;
|
|
91
|
+
requireSymbols: z.ZodDefault<z.ZodBoolean>;
|
|
92
|
+
requireUppercase: z.ZodDefault<z.ZodBoolean>;
|
|
93
|
+
requireLowercase: z.ZodDefault<z.ZodBoolean>;
|
|
94
|
+
}, "strip", z.ZodTypeAny, {
|
|
95
|
+
minimumPasswordLength: number;
|
|
96
|
+
requireNumbers: boolean;
|
|
97
|
+
requireSymbols: boolean;
|
|
98
|
+
requireUppercase: boolean;
|
|
99
|
+
requireLowercase: boolean;
|
|
100
|
+
}, {
|
|
101
|
+
minimumPasswordLength?: number | undefined;
|
|
102
|
+
requireNumbers?: boolean | undefined;
|
|
103
|
+
requireSymbols?: boolean | undefined;
|
|
104
|
+
requireUppercase?: boolean | undefined;
|
|
105
|
+
requireLowercase?: boolean | undefined;
|
|
106
|
+
}>;
|
|
107
|
+
type Config = z.infer<typeof ConfigSchema>;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Creates a hash of the input string using the specified algorithm
|
|
111
|
+
* @param input - The string to hash
|
|
112
|
+
* @param options - Hash options
|
|
113
|
+
* @returns The hashed string
|
|
114
|
+
*/
|
|
115
|
+
declare function hash(input: string, options?: Partial<HashOptions>): string;
|
|
116
|
+
/**
|
|
117
|
+
* Verifies if an input matches a previously hashed value
|
|
118
|
+
* @param input - The plain text to verify
|
|
119
|
+
* @param hashedValue - The hashed value to compare against
|
|
120
|
+
* @param options - Hash options
|
|
121
|
+
* @returns True if the input matches the hash, false otherwise
|
|
122
|
+
*/
|
|
123
|
+
declare function verifyHash(input: string, hashedValue: string, options?: Partial<HashOptions>): boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Creates a HMAC (Hash-based Message Authentication Code) of the input
|
|
126
|
+
* @param input - The input message
|
|
127
|
+
* @param key - The secret key
|
|
128
|
+
* @param options - Hash options
|
|
129
|
+
* @returns The HMAC signature
|
|
130
|
+
*/
|
|
131
|
+
declare function hmac(input: string, key: string, options?: Partial<HashOptions>): string;
|
|
132
|
+
/**
|
|
133
|
+
* Performs a secure password hashing using PBKDF2 (default), Argon2, or bcrypt simulation
|
|
134
|
+
* Note: This is a simplified implementation. For production, consider using a specialized library
|
|
135
|
+
* @param password - The password to hash
|
|
136
|
+
* @param options - Password hash options
|
|
137
|
+
* @returns An object containing the hash, salt, and parameters
|
|
138
|
+
*/
|
|
139
|
+
declare function hashPassword(password: string, options?: Partial<PasswordHashOptions>): {
|
|
140
|
+
hash: string;
|
|
141
|
+
salt: string;
|
|
142
|
+
params: PasswordHashOptions;
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Verifies a password against a previously hashed password
|
|
146
|
+
* @param password - The password to verify
|
|
147
|
+
* @param hash - The previously generated hash
|
|
148
|
+
* @param salt - The salt used in the original hash
|
|
149
|
+
* @param options - Password hash options (must match those used in hashPassword)
|
|
150
|
+
* @returns True if the password matches, false otherwise
|
|
151
|
+
*/
|
|
152
|
+
declare function verifyPassword(password: string, hash: string, salt: string, options?: Partial<PasswordHashOptions>): boolean;
|
|
153
|
+
/**
|
|
154
|
+
* Performs a constant-time comparison of two strings to prevent timing attacks
|
|
155
|
+
* @param a - First string to compare
|
|
156
|
+
* @param b - Second string to compare
|
|
157
|
+
* @param options - Timing safe comparison options
|
|
158
|
+
* @returns True if both strings are equal, false otherwise
|
|
159
|
+
*/
|
|
160
|
+
declare function timingSafeEqual(a: string, b: string, options?: Partial<TimingSafeOptions>): boolean;
|
|
161
|
+
/**
|
|
162
|
+
* Generates a cryptographically secure random string
|
|
163
|
+
* @param length - The length of the random string in bytes
|
|
164
|
+
* @param encoding - The encoding to use for the output
|
|
165
|
+
* @returns A random string in the specified encoding
|
|
166
|
+
*/
|
|
167
|
+
declare function generateRandomBytes(length?: number, encoding?: 'hex' | 'base64'): string;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* A const enum for security levels, used for warnings
|
|
171
|
+
* @public
|
|
172
|
+
*/
|
|
173
|
+
declare enum SecurityLevel {
|
|
174
|
+
HIGH = "HIGH",
|
|
175
|
+
MEDIUM = "MEDIUM",
|
|
176
|
+
LOW = "LOW",
|
|
177
|
+
INSECURE = "INSECURE"
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Returns the security level of the encryption options
|
|
181
|
+
* @param options - The encryption options
|
|
182
|
+
* @returns The security level
|
|
183
|
+
*/
|
|
184
|
+
declare function getSecurityLevel(options: EncryptionOptions): SecurityLevel;
|
|
185
|
+
/**
|
|
186
|
+
* Encrypts the provided text using the specified encryption options
|
|
187
|
+
* @param text - The text to encrypt
|
|
188
|
+
* @param key - The encryption key
|
|
189
|
+
* @param options - Encryption options
|
|
190
|
+
* @returns The encrypted text
|
|
191
|
+
*/
|
|
192
|
+
declare function encrypt(text: string, key: string, options?: Partial<EncryptionOptions>): string;
|
|
193
|
+
/**
|
|
194
|
+
* Decrypts the provided encrypted text using the specified options
|
|
195
|
+
* @param encryptedText - The text to decrypt
|
|
196
|
+
* @param key - The decryption key
|
|
197
|
+
* @param options - Encryption options
|
|
198
|
+
* @returns The decrypted text
|
|
199
|
+
*/
|
|
200
|
+
declare function decrypt(encryptedText: string, key: string, options?: Partial<EncryptionOptions>): string;
|
|
201
|
+
/**
|
|
202
|
+
* Generates a random encryption key
|
|
203
|
+
* @param length - Length of the key in bytes
|
|
204
|
+
* @returns A random key as a hex string
|
|
205
|
+
*/
|
|
206
|
+
declare function generateKey(length?: number): string;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Calculates the entropy of a password in bits
|
|
210
|
+
* @param password - The password to calculate entropy for
|
|
211
|
+
* @returns The entropy in bits
|
|
212
|
+
*/
|
|
213
|
+
declare function calculatePasswordEntropy(password: string): number;
|
|
214
|
+
/**
|
|
215
|
+
* Analyzes patterns in the password that might reduce its effective entropy
|
|
216
|
+
* @param password - The password to analyze
|
|
217
|
+
* @returns The entropy reduction (0 to 1)
|
|
218
|
+
*/
|
|
219
|
+
declare function analyzePasswordPatterns(password: string): {
|
|
220
|
+
entropyReduction: number;
|
|
221
|
+
patterns: string[];
|
|
222
|
+
};
|
|
223
|
+
/**
|
|
224
|
+
* Checks the strength of a password using both rule-based criteria and entropy
|
|
225
|
+
* @param password - The password to check
|
|
226
|
+
* @param config - Password configuration options
|
|
227
|
+
* @returns A PasswordStrength object containing score and feedback
|
|
228
|
+
*/
|
|
229
|
+
declare function checkPasswordStrength(password: string, config?: Partial<Config>): PasswordStrength;
|
|
230
|
+
/**
|
|
231
|
+
* Generates a secure random password based on configuration
|
|
232
|
+
* @param config - Password configuration options
|
|
233
|
+
* @returns A random secure password
|
|
234
|
+
*/
|
|
235
|
+
declare function generateSecurePassword(config?: Partial<Config>): string;
|
|
236
|
+
|
|
237
|
+
export { type Config, ConfigSchema, type EncryptionOptions, EncryptionOptionsSchema, type HashOptions, HashOptionsSchema, type PasswordHashOptions, PasswordHashOptionsSchema, type PasswordStrength, PasswordStrengthSchema, SecurityLevel, type TimingSafeOptions, TimingSafeOptionsSchema, analyzePasswordPatterns, calculatePasswordEntropy, checkPasswordStrength, decrypt, encrypt, generateKey, generateRandomBytes, generateSecurePassword, getSecurityLevel, hash, hashPassword, hmac, timingSafeEqual, verifyHash, verifyPassword };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
declare const PasswordStrengthSchema: z.ZodObject<{
|
|
4
|
+
score: z.ZodNumber;
|
|
5
|
+
feedback: z.ZodObject<{
|
|
6
|
+
warning: z.ZodOptional<z.ZodString>;
|
|
7
|
+
suggestions: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
8
|
+
}, "strip", z.ZodTypeAny, {
|
|
9
|
+
warning?: string | undefined;
|
|
10
|
+
suggestions?: string[] | undefined;
|
|
11
|
+
}, {
|
|
12
|
+
warning?: string | undefined;
|
|
13
|
+
suggestions?: string[] | undefined;
|
|
14
|
+
}>;
|
|
15
|
+
isStrong: z.ZodBoolean;
|
|
16
|
+
}, "strip", z.ZodTypeAny, {
|
|
17
|
+
score: number;
|
|
18
|
+
feedback: {
|
|
19
|
+
warning?: string | undefined;
|
|
20
|
+
suggestions?: string[] | undefined;
|
|
21
|
+
};
|
|
22
|
+
isStrong: boolean;
|
|
23
|
+
}, {
|
|
24
|
+
score: number;
|
|
25
|
+
feedback: {
|
|
26
|
+
warning?: string | undefined;
|
|
27
|
+
suggestions?: string[] | undefined;
|
|
28
|
+
};
|
|
29
|
+
isStrong: boolean;
|
|
30
|
+
}>;
|
|
31
|
+
type PasswordStrength = z.infer<typeof PasswordStrengthSchema>;
|
|
32
|
+
declare const HashOptionsSchema: z.ZodObject<{
|
|
33
|
+
algorithm: z.ZodDefault<z.ZodEnum<["md5", "sha1", "sha256", "sha512", "sha3"]>>;
|
|
34
|
+
encoding: z.ZodDefault<z.ZodEnum<["hex", "base64"]>>;
|
|
35
|
+
}, "strip", z.ZodTypeAny, {
|
|
36
|
+
algorithm: "md5" | "sha1" | "sha256" | "sha512" | "sha3";
|
|
37
|
+
encoding: "hex" | "base64";
|
|
38
|
+
}, {
|
|
39
|
+
algorithm?: "md5" | "sha1" | "sha256" | "sha512" | "sha3" | undefined;
|
|
40
|
+
encoding?: "hex" | "base64" | undefined;
|
|
41
|
+
}>;
|
|
42
|
+
type HashOptions = z.infer<typeof HashOptionsSchema>;
|
|
43
|
+
declare const PasswordHashOptionsSchema: z.ZodObject<{
|
|
44
|
+
algorithm: z.ZodDefault<z.ZodEnum<["pbkdf2", "argon2", "bcrypt"]>>;
|
|
45
|
+
iterations: z.ZodDefault<z.ZodNumber>;
|
|
46
|
+
saltLength: z.ZodDefault<z.ZodNumber>;
|
|
47
|
+
keyLength: z.ZodDefault<z.ZodNumber>;
|
|
48
|
+
}, "strip", z.ZodTypeAny, {
|
|
49
|
+
algorithm: "pbkdf2" | "argon2" | "bcrypt";
|
|
50
|
+
iterations: number;
|
|
51
|
+
saltLength: number;
|
|
52
|
+
keyLength: number;
|
|
53
|
+
}, {
|
|
54
|
+
algorithm?: "pbkdf2" | "argon2" | "bcrypt" | undefined;
|
|
55
|
+
iterations?: number | undefined;
|
|
56
|
+
saltLength?: number | undefined;
|
|
57
|
+
keyLength?: number | undefined;
|
|
58
|
+
}>;
|
|
59
|
+
type PasswordHashOptions = z.infer<typeof PasswordHashOptionsSchema>;
|
|
60
|
+
declare const TimingSafeOptionsSchema: z.ZodObject<{
|
|
61
|
+
encoding: z.ZodDefault<z.ZodEnum<["utf8", "hex", "base64"]>>;
|
|
62
|
+
}, "strip", z.ZodTypeAny, {
|
|
63
|
+
encoding: "hex" | "base64" | "utf8";
|
|
64
|
+
}, {
|
|
65
|
+
encoding?: "hex" | "base64" | "utf8" | undefined;
|
|
66
|
+
}>;
|
|
67
|
+
type TimingSafeOptions = z.infer<typeof TimingSafeOptionsSchema>;
|
|
68
|
+
declare const EncryptionOptionsSchema: z.ZodObject<{
|
|
69
|
+
mode: z.ZodDefault<z.ZodEnum<["aes-cbc", "aes-ctr", "aes-ecb", "aes-gcm"]>>;
|
|
70
|
+
padding: z.ZodDefault<z.ZodEnum<["Pkcs7", "NoPadding", "ZeroPadding"]>>;
|
|
71
|
+
iv: z.ZodOptional<z.ZodString>;
|
|
72
|
+
authTag: z.ZodOptional<z.ZodString>;
|
|
73
|
+
aad: z.ZodOptional<z.ZodString>;
|
|
74
|
+
}, "strip", z.ZodTypeAny, {
|
|
75
|
+
mode: "aes-cbc" | "aes-ctr" | "aes-ecb" | "aes-gcm";
|
|
76
|
+
padding: "Pkcs7" | "NoPadding" | "ZeroPadding";
|
|
77
|
+
iv?: string | undefined;
|
|
78
|
+
authTag?: string | undefined;
|
|
79
|
+
aad?: string | undefined;
|
|
80
|
+
}, {
|
|
81
|
+
mode?: "aes-cbc" | "aes-ctr" | "aes-ecb" | "aes-gcm" | undefined;
|
|
82
|
+
padding?: "Pkcs7" | "NoPadding" | "ZeroPadding" | undefined;
|
|
83
|
+
iv?: string | undefined;
|
|
84
|
+
authTag?: string | undefined;
|
|
85
|
+
aad?: string | undefined;
|
|
86
|
+
}>;
|
|
87
|
+
type EncryptionOptions = z.infer<typeof EncryptionOptionsSchema>;
|
|
88
|
+
declare const ConfigSchema: z.ZodObject<{
|
|
89
|
+
minimumPasswordLength: z.ZodDefault<z.ZodNumber>;
|
|
90
|
+
requireNumbers: z.ZodDefault<z.ZodBoolean>;
|
|
91
|
+
requireSymbols: z.ZodDefault<z.ZodBoolean>;
|
|
92
|
+
requireUppercase: z.ZodDefault<z.ZodBoolean>;
|
|
93
|
+
requireLowercase: z.ZodDefault<z.ZodBoolean>;
|
|
94
|
+
}, "strip", z.ZodTypeAny, {
|
|
95
|
+
minimumPasswordLength: number;
|
|
96
|
+
requireNumbers: boolean;
|
|
97
|
+
requireSymbols: boolean;
|
|
98
|
+
requireUppercase: boolean;
|
|
99
|
+
requireLowercase: boolean;
|
|
100
|
+
}, {
|
|
101
|
+
minimumPasswordLength?: number | undefined;
|
|
102
|
+
requireNumbers?: boolean | undefined;
|
|
103
|
+
requireSymbols?: boolean | undefined;
|
|
104
|
+
requireUppercase?: boolean | undefined;
|
|
105
|
+
requireLowercase?: boolean | undefined;
|
|
106
|
+
}>;
|
|
107
|
+
type Config = z.infer<typeof ConfigSchema>;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Creates a hash of the input string using the specified algorithm
|
|
111
|
+
* @param input - The string to hash
|
|
112
|
+
* @param options - Hash options
|
|
113
|
+
* @returns The hashed string
|
|
114
|
+
*/
|
|
115
|
+
declare function hash(input: string, options?: Partial<HashOptions>): string;
|
|
116
|
+
/**
|
|
117
|
+
* Verifies if an input matches a previously hashed value
|
|
118
|
+
* @param input - The plain text to verify
|
|
119
|
+
* @param hashedValue - The hashed value to compare against
|
|
120
|
+
* @param options - Hash options
|
|
121
|
+
* @returns True if the input matches the hash, false otherwise
|
|
122
|
+
*/
|
|
123
|
+
declare function verifyHash(input: string, hashedValue: string, options?: Partial<HashOptions>): boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Creates a HMAC (Hash-based Message Authentication Code) of the input
|
|
126
|
+
* @param input - The input message
|
|
127
|
+
* @param key - The secret key
|
|
128
|
+
* @param options - Hash options
|
|
129
|
+
* @returns The HMAC signature
|
|
130
|
+
*/
|
|
131
|
+
declare function hmac(input: string, key: string, options?: Partial<HashOptions>): string;
|
|
132
|
+
/**
|
|
133
|
+
* Performs a secure password hashing using PBKDF2 (default), Argon2, or bcrypt simulation
|
|
134
|
+
* Note: This is a simplified implementation. For production, consider using a specialized library
|
|
135
|
+
* @param password - The password to hash
|
|
136
|
+
* @param options - Password hash options
|
|
137
|
+
* @returns An object containing the hash, salt, and parameters
|
|
138
|
+
*/
|
|
139
|
+
declare function hashPassword(password: string, options?: Partial<PasswordHashOptions>): {
|
|
140
|
+
hash: string;
|
|
141
|
+
salt: string;
|
|
142
|
+
params: PasswordHashOptions;
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Verifies a password against a previously hashed password
|
|
146
|
+
* @param password - The password to verify
|
|
147
|
+
* @param hash - The previously generated hash
|
|
148
|
+
* @param salt - The salt used in the original hash
|
|
149
|
+
* @param options - Password hash options (must match those used in hashPassword)
|
|
150
|
+
* @returns True if the password matches, false otherwise
|
|
151
|
+
*/
|
|
152
|
+
declare function verifyPassword(password: string, hash: string, salt: string, options?: Partial<PasswordHashOptions>): boolean;
|
|
153
|
+
/**
|
|
154
|
+
* Performs a constant-time comparison of two strings to prevent timing attacks
|
|
155
|
+
* @param a - First string to compare
|
|
156
|
+
* @param b - Second string to compare
|
|
157
|
+
* @param options - Timing safe comparison options
|
|
158
|
+
* @returns True if both strings are equal, false otherwise
|
|
159
|
+
*/
|
|
160
|
+
declare function timingSafeEqual(a: string, b: string, options?: Partial<TimingSafeOptions>): boolean;
|
|
161
|
+
/**
|
|
162
|
+
* Generates a cryptographically secure random string
|
|
163
|
+
* @param length - The length of the random string in bytes
|
|
164
|
+
* @param encoding - The encoding to use for the output
|
|
165
|
+
* @returns A random string in the specified encoding
|
|
166
|
+
*/
|
|
167
|
+
declare function generateRandomBytes(length?: number, encoding?: 'hex' | 'base64'): string;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* A const enum for security levels, used for warnings
|
|
171
|
+
* @public
|
|
172
|
+
*/
|
|
173
|
+
declare enum SecurityLevel {
|
|
174
|
+
HIGH = "HIGH",
|
|
175
|
+
MEDIUM = "MEDIUM",
|
|
176
|
+
LOW = "LOW",
|
|
177
|
+
INSECURE = "INSECURE"
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Returns the security level of the encryption options
|
|
181
|
+
* @param options - The encryption options
|
|
182
|
+
* @returns The security level
|
|
183
|
+
*/
|
|
184
|
+
declare function getSecurityLevel(options: EncryptionOptions): SecurityLevel;
|
|
185
|
+
/**
|
|
186
|
+
* Encrypts the provided text using the specified encryption options
|
|
187
|
+
* @param text - The text to encrypt
|
|
188
|
+
* @param key - The encryption key
|
|
189
|
+
* @param options - Encryption options
|
|
190
|
+
* @returns The encrypted text
|
|
191
|
+
*/
|
|
192
|
+
declare function encrypt(text: string, key: string, options?: Partial<EncryptionOptions>): string;
|
|
193
|
+
/**
|
|
194
|
+
* Decrypts the provided encrypted text using the specified options
|
|
195
|
+
* @param encryptedText - The text to decrypt
|
|
196
|
+
* @param key - The decryption key
|
|
197
|
+
* @param options - Encryption options
|
|
198
|
+
* @returns The decrypted text
|
|
199
|
+
*/
|
|
200
|
+
declare function decrypt(encryptedText: string, key: string, options?: Partial<EncryptionOptions>): string;
|
|
201
|
+
/**
|
|
202
|
+
* Generates a random encryption key
|
|
203
|
+
* @param length - Length of the key in bytes
|
|
204
|
+
* @returns A random key as a hex string
|
|
205
|
+
*/
|
|
206
|
+
declare function generateKey(length?: number): string;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Calculates the entropy of a password in bits
|
|
210
|
+
* @param password - The password to calculate entropy for
|
|
211
|
+
* @returns The entropy in bits
|
|
212
|
+
*/
|
|
213
|
+
declare function calculatePasswordEntropy(password: string): number;
|
|
214
|
+
/**
|
|
215
|
+
* Analyzes patterns in the password that might reduce its effective entropy
|
|
216
|
+
* @param password - The password to analyze
|
|
217
|
+
* @returns The entropy reduction (0 to 1)
|
|
218
|
+
*/
|
|
219
|
+
declare function analyzePasswordPatterns(password: string): {
|
|
220
|
+
entropyReduction: number;
|
|
221
|
+
patterns: string[];
|
|
222
|
+
};
|
|
223
|
+
/**
|
|
224
|
+
* Checks the strength of a password using both rule-based criteria and entropy
|
|
225
|
+
* @param password - The password to check
|
|
226
|
+
* @param config - Password configuration options
|
|
227
|
+
* @returns A PasswordStrength object containing score and feedback
|
|
228
|
+
*/
|
|
229
|
+
declare function checkPasswordStrength(password: string, config?: Partial<Config>): PasswordStrength;
|
|
230
|
+
/**
|
|
231
|
+
* Generates a secure random password based on configuration
|
|
232
|
+
* @param config - Password configuration options
|
|
233
|
+
* @returns A random secure password
|
|
234
|
+
*/
|
|
235
|
+
declare function generateSecurePassword(config?: Partial<Config>): string;
|
|
236
|
+
|
|
237
|
+
export { type Config, ConfigSchema, type EncryptionOptions, EncryptionOptionsSchema, type HashOptions, HashOptionsSchema, type PasswordHashOptions, PasswordHashOptionsSchema, type PasswordStrength, PasswordStrengthSchema, SecurityLevel, type TimingSafeOptions, TimingSafeOptionsSchema, analyzePasswordPatterns, calculatePasswordEntropy, checkPasswordStrength, decrypt, encrypt, generateKey, generateRandomBytes, generateSecurePassword, getSecurityLevel, hash, hashPassword, hmac, timingSafeEqual, verifyHash, verifyPassword };
|