@vvlad1973/crypto 2.1.1 → 2.1.3
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/package.json +1 -1
- package/.vscode/launch.json +0 -20
- package/CLAUDE.md +0 -90
- package/src/__tests__/crypto.test.ts +0 -238
- package/tsconfig.json +0 -21
- package/vitest.config.ts +0 -36
package/package.json
CHANGED
package/.vscode/launch.json
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
// Используйте IntelliSense, чтобы узнать о возможных атрибутах.
|
|
3
|
-
// Наведите указатель мыши, чтобы просмотреть описания существующих атрибутов.
|
|
4
|
-
// Для получения дополнительной информации посетите: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
-
"version": "0.2.0",
|
|
6
|
-
"configurations": [
|
|
7
|
-
{
|
|
8
|
-
"type": "node",
|
|
9
|
-
"request": "launch",
|
|
10
|
-
"env": {
|
|
11
|
-
"NODE_OPTIONS": "--experimental-vm-modules"
|
|
12
|
-
},
|
|
13
|
-
"name": "Запустить программу",
|
|
14
|
-
"skipFiles": [
|
|
15
|
-
"<node_internals>/**"
|
|
16
|
-
],
|
|
17
|
-
"program": "${workspaceFolder}\\index.js"
|
|
18
|
-
}
|
|
19
|
-
]
|
|
20
|
-
}
|
package/CLAUDE.md
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
# CLAUDE.md
|
|
2
|
-
|
|
3
|
-
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
-
|
|
5
|
-
## Common requirements
|
|
6
|
-
|
|
7
|
-
- отвечай на русском языке. Используй английский только если на то есть особые указания
|
|
8
|
-
- комментарии в коде, в JSDoc - используй английский язык
|
|
9
|
-
- отчеты - используй русский язык
|
|
10
|
-
- при создании документов и commit messages НИКОГДА НЕ ДОБАВЛЯЙ в нихх никакой информации о том, что этот текст сгенерирован тобой, либо при твоем участии/соавторстве
|
|
11
|
-
- при формировании и редактировании файлов *.md соблюдай синтаксис MarkDown. Все Markdown-файлы (*.md) ДОЛЖНЫ проходить проверку линтером без ошибок и предупреждений
|
|
12
|
-
- Не вноси изменений в CHANGELOG.md, этот файл формируется автоматически
|
|
13
|
-
- **НИКОГДА НЕ ИСПОЛЬЗУЙ EMOJI** в файлах .md - это строгое требование проекта без исключений
|
|
14
|
-
- при подготовке файлов документации соблюдай структуру каталога docs, правила, изложенные в C:\Repositories\botApp\bot-app-service\docs\CONTRIBUTING.md
|
|
15
|
-
|
|
16
|
-
## Project Overview
|
|
17
|
-
|
|
18
|
-
TypeScript library providing AES-256-CTR encryption/decryption functionality. Wrapper around Node.js native `crypto` module, replacing the previous `aes-js` dependency.
|
|
19
|
-
|
|
20
|
-
## Development Commands
|
|
21
|
-
|
|
22
|
-
### Build
|
|
23
|
-
```bash
|
|
24
|
-
npm run build
|
|
25
|
-
```
|
|
26
|
-
Compiles TypeScript to JavaScript using `tsc`. Output: `dist/` directory with `.js` and `.d.ts` files.
|
|
27
|
-
|
|
28
|
-
### Testing
|
|
29
|
-
```bash
|
|
30
|
-
npm test
|
|
31
|
-
```
|
|
32
|
-
Runs build followed by Node.js native test runner with ts-node loader.
|
|
33
|
-
|
|
34
|
-
To run a specific test file:
|
|
35
|
-
```bash
|
|
36
|
-
npm run build && node --loader ts-node/esm --test src/__tests__/crypto.test.ts
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
### Documentation Generation
|
|
40
|
-
```bash
|
|
41
|
-
npm run doc
|
|
42
|
-
```
|
|
43
|
-
Generates JSDoc documentation using better-docs template. Output: `docs/` directory.
|
|
44
|
-
|
|
45
|
-
## Architecture
|
|
46
|
-
|
|
47
|
-
### Core Module Structure
|
|
48
|
-
- [src/crypto.ts](src/crypto.ts) - Main `Crypto` class implementation
|
|
49
|
-
- [src/index.ts](src/index.ts) - Module exports
|
|
50
|
-
- [src/__tests__/crypto.test.ts](src/__tests__/crypto.test.ts) - Test suite
|
|
51
|
-
|
|
52
|
-
### Crypto Class Design
|
|
53
|
-
|
|
54
|
-
The `Crypto` class uses Node.js native crypto APIs:
|
|
55
|
-
- **Key derivation**: `pbkdf2Sync` with configurable algorithm (default SHA512), iterations (1000), and key length (32 bytes)
|
|
56
|
-
- **Encryption**: AES-256-CTR mode via `createCipheriv`
|
|
57
|
-
- **Initialization Vector (IV)**: Accepts either Buffer or number (converted to 16-byte Buffer)
|
|
58
|
-
|
|
59
|
-
Constructor supports two signatures:
|
|
60
|
-
1. Individual parameters: `new Crypto(password, salt, algorithm?, iterations?, keyLength?, iv?)`
|
|
61
|
-
2. Options object: `new Crypto({ password, salt, algorithm?, iterations?, keyLength?, iv? })`
|
|
62
|
-
|
|
63
|
-
### Key Implementation Details
|
|
64
|
-
|
|
65
|
-
- IV conversion from number: Uses `Buffer.alloc(16)` with `writeUInt32BE` at offset 12
|
|
66
|
-
- Encryption output: Hex-encoded string
|
|
67
|
-
- Type guard: `isCrypto(object)` function checks for `encrypt` and `decrypt` methods
|
|
68
|
-
- Static method: `Crypto.getUUID()` returns UUIDv4 string using `crypto.randomUUID()`
|
|
69
|
-
|
|
70
|
-
### Module System
|
|
71
|
-
- ES modules (`"type": "module"` in package.json)
|
|
72
|
-
- TypeScript target: ES2022
|
|
73
|
-
- File extensions: Import paths must use `.js` extension (not `.ts`) due to ES module resolution
|
|
74
|
-
|
|
75
|
-
## TypeScript Configuration
|
|
76
|
-
|
|
77
|
-
Strict mode enabled with full type checking. Output includes declaration files for TypeScript consumers.
|
|
78
|
-
|
|
79
|
-
## Testing Approach
|
|
80
|
-
|
|
81
|
-
Tests use Node.js native test runner (`node:test`) with:
|
|
82
|
-
- Encryption/decryption round-trip verification
|
|
83
|
-
- Both constructor signatures
|
|
84
|
-
- Default parameter handling
|
|
85
|
-
- Type guard validation
|
|
86
|
-
- Backward compatibility with previous library version (validates specific encrypted output)
|
|
87
|
-
|
|
88
|
-
## JSDoc Requirements
|
|
89
|
-
|
|
90
|
-
Per user instructions: All modules must include complete JSDoc with module definitions. Comments and JSDoc must be in English.
|
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
import { describe, it, beforeEach, expect } from 'vitest';
|
|
2
|
-
import { randomBytes } from 'crypto';
|
|
3
|
-
import Crypto, { CryptoOptions, isCrypto } from '../crypto.js';
|
|
4
|
-
|
|
5
|
-
describe('Crypto', () => {
|
|
6
|
-
const plainText = 'Hello, World!';
|
|
7
|
-
let options: CryptoOptions;
|
|
8
|
-
let password: string;
|
|
9
|
-
let salt: string;
|
|
10
|
-
let algorithm: string;
|
|
11
|
-
let iterations: number;
|
|
12
|
-
let keyLength: number;
|
|
13
|
-
let iv: number;
|
|
14
|
-
|
|
15
|
-
beforeEach(() => {
|
|
16
|
-
options = {
|
|
17
|
-
password: 'testPassword',
|
|
18
|
-
salt: 'testSalt',
|
|
19
|
-
algorithm: 'SHA512',
|
|
20
|
-
iterations: 1000,
|
|
21
|
-
keyLength: 32,
|
|
22
|
-
iv: 5,
|
|
23
|
-
};
|
|
24
|
-
password = 'testPassword';
|
|
25
|
-
salt = 'testSalt';
|
|
26
|
-
algorithm = 'SHA512';
|
|
27
|
-
iterations = 1000;
|
|
28
|
-
keyLength = 32;
|
|
29
|
-
iv = 5;
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('should initialize with CryptoOptions object', () => {
|
|
33
|
-
const crypto = new Crypto(options);
|
|
34
|
-
expect(crypto).toBeDefined();
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it('should initialize with separate parameters', () => {
|
|
38
|
-
const crypto = new Crypto(
|
|
39
|
-
password,
|
|
40
|
-
salt,
|
|
41
|
-
algorithm,
|
|
42
|
-
iterations,
|
|
43
|
-
keyLength,
|
|
44
|
-
iv
|
|
45
|
-
);
|
|
46
|
-
expect(crypto).toBeDefined();
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('should return UUIDv4', () => {
|
|
50
|
-
const crypto = new Crypto(options);
|
|
51
|
-
const uuid = Crypto.getUUID();
|
|
52
|
-
const uuidv4Regex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
53
|
-
expect(uuid).toMatch(uuidv4Regex);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('should encrypt and decrypt text correctly using ICryptoOptions', () => {
|
|
57
|
-
const crypto = new Crypto(options);
|
|
58
|
-
const encryptedText = crypto.encrypt(plainText);
|
|
59
|
-
const decryptedText = crypto.decrypt(encryptedText);
|
|
60
|
-
expect(decryptedText).toBe(plainText);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('should encrypt and decrypt text correctly using separate parameters', () => {
|
|
64
|
-
const crypto = new Crypto(
|
|
65
|
-
password,
|
|
66
|
-
salt,
|
|
67
|
-
algorithm,
|
|
68
|
-
iterations,
|
|
69
|
-
keyLength,
|
|
70
|
-
iv
|
|
71
|
-
);
|
|
72
|
-
const encryptedText = crypto.encrypt(plainText);
|
|
73
|
-
const decryptedText = crypto.decrypt(encryptedText);
|
|
74
|
-
expect(decryptedText).toBe(plainText);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should handle default values for optional parameters', () => {
|
|
78
|
-
const cryptoWithDefaults = new Crypto(password, salt);
|
|
79
|
-
const encryptedText = cryptoWithDefaults.encrypt(plainText);
|
|
80
|
-
const decryptedText = cryptoWithDefaults.decrypt(encryptedText);
|
|
81
|
-
expect(decryptedText).toBe(plainText);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('should initialize with Buffer IV using options object', () => {
|
|
85
|
-
const ivBuffer = randomBytes(16);
|
|
86
|
-
const crypto = new Crypto({
|
|
87
|
-
password,
|
|
88
|
-
salt,
|
|
89
|
-
algorithm,
|
|
90
|
-
iterations,
|
|
91
|
-
keyLength,
|
|
92
|
-
iv: ivBuffer,
|
|
93
|
-
});
|
|
94
|
-
const encryptedText = crypto.encrypt(plainText);
|
|
95
|
-
const decryptedText = crypto.decrypt(encryptedText);
|
|
96
|
-
expect(decryptedText).toBe(plainText);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it('should initialize with Buffer IV using separate parameters', () => {
|
|
100
|
-
const ivBuffer = randomBytes(16);
|
|
101
|
-
const crypto = new Crypto(
|
|
102
|
-
password,
|
|
103
|
-
salt,
|
|
104
|
-
algorithm,
|
|
105
|
-
iterations,
|
|
106
|
-
keyLength,
|
|
107
|
-
ivBuffer
|
|
108
|
-
);
|
|
109
|
-
const encryptedText = crypto.encrypt(plainText);
|
|
110
|
-
const decryptedText = crypto.decrypt(encryptedText);
|
|
111
|
-
expect(decryptedText).toBe(plainText);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it('should initialize without IV (random generation via options)', () => {
|
|
115
|
-
const crypto1 = new Crypto({ password, salt });
|
|
116
|
-
const crypto2 = new Crypto({ password, salt });
|
|
117
|
-
const encrypted1 = crypto1.encrypt(plainText);
|
|
118
|
-
const encrypted2 = crypto2.encrypt(plainText);
|
|
119
|
-
// Should produce different encrypted results due to different random IVs
|
|
120
|
-
expect(encrypted1).not.toBe(encrypted2);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('should handle empty string encryption and decryption', () => {
|
|
124
|
-
const crypto = new Crypto(options);
|
|
125
|
-
const emptyText = '';
|
|
126
|
-
const encryptedText = crypto.encrypt(emptyText);
|
|
127
|
-
const decryptedText = crypto.decrypt(encryptedText);
|
|
128
|
-
expect(decryptedText).toBe(emptyText);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it('should handle Unicode characters', () => {
|
|
132
|
-
const crypto = new Crypto(options);
|
|
133
|
-
const unicodeText = '🔐 Encryption test 中文 العربية';
|
|
134
|
-
const encryptedText = crypto.encrypt(unicodeText);
|
|
135
|
-
const decryptedText = crypto.decrypt(encryptedText);
|
|
136
|
-
expect(decryptedText).toBe(unicodeText);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it('should handle different hash algorithms', () => {
|
|
140
|
-
const crypto256 = new Crypto(password, salt, 'sha256', iterations, keyLength, iv);
|
|
141
|
-
const encryptedText = crypto256.encrypt(plainText);
|
|
142
|
-
const decryptedText = crypto256.decrypt(encryptedText);
|
|
143
|
-
expect(decryptedText).toBe(plainText);
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
it('should handle different key lengths', () => {
|
|
147
|
-
// AES-256-CTR requires 32 bytes key, testing with valid 32 bytes
|
|
148
|
-
const crypto32 = new Crypto(password, salt, algorithm, iterations, 32, iv);
|
|
149
|
-
const encryptedText = crypto32.encrypt(plainText);
|
|
150
|
-
const decryptedText = crypto32.decrypt(encryptedText);
|
|
151
|
-
expect(decryptedText).toBe(plainText);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
it('should handle different iteration counts', () => {
|
|
155
|
-
const crypto = new Crypto(password, salt, algorithm, 5000, keyLength, iv);
|
|
156
|
-
const encryptedText = crypto.encrypt(plainText);
|
|
157
|
-
const decryptedText = crypto.decrypt(encryptedText);
|
|
158
|
-
expect(decryptedText).toBe(plainText);
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
describe('isCrypto', () => {
|
|
163
|
-
const validCryptoOptions: CryptoOptions = {
|
|
164
|
-
password: 'testPassword',
|
|
165
|
-
salt: 'testSalt',
|
|
166
|
-
algorithm: 'SHA512',
|
|
167
|
-
iterations: 1000,
|
|
168
|
-
keyLength: 32,
|
|
169
|
-
iv: 5,
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
it('should return true for a valid Crypto instance created with ICryptoOptions', () => {
|
|
173
|
-
const crypto = new Crypto(validCryptoOptions);
|
|
174
|
-
expect(isCrypto(crypto)).toBe(true);
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
it('should return true for a valid Crypto instance created with separate parameters', () => {
|
|
178
|
-
const crypto = new Crypto(
|
|
179
|
-
validCryptoOptions.password,
|
|
180
|
-
validCryptoOptions.salt,
|
|
181
|
-
validCryptoOptions.algorithm,
|
|
182
|
-
validCryptoOptions.iterations,
|
|
183
|
-
validCryptoOptions.keyLength,
|
|
184
|
-
validCryptoOptions.iv
|
|
185
|
-
);
|
|
186
|
-
expect(isCrypto(crypto)).toBe(true);
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
it('should return false for null', () => {
|
|
190
|
-
const nullObject = null;
|
|
191
|
-
expect(isCrypto(nullObject)).toBe(false);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it('should return false for undefined', () => {
|
|
195
|
-
const undefinedObject = undefined;
|
|
196
|
-
expect(isCrypto(undefinedObject)).toBe(false);
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it('should return false for a plain object without encrypt and decrypt methods', () => {
|
|
200
|
-
const plainObject = {
|
|
201
|
-
someMethod: () => {},
|
|
202
|
-
};
|
|
203
|
-
expect(isCrypto(plainObject)).toBe(false);
|
|
204
|
-
});
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
describe('Compatibility with previous version', () => {
|
|
208
|
-
|
|
209
|
-
const plainText = 'Проверка связи';
|
|
210
|
-
let options: CryptoOptions;
|
|
211
|
-
let encText = 'a4661cb701751a895f65418ed488faad33da3090d05ad661f0a7a9';
|
|
212
|
-
|
|
213
|
-
beforeEach(() => {
|
|
214
|
-
options = {
|
|
215
|
-
algorithm: 'SHA512',
|
|
216
|
-
iv: 5,
|
|
217
|
-
keyLength: 32,
|
|
218
|
-
iterations: 1000,
|
|
219
|
-
password: 'password',
|
|
220
|
-
salt: 'saltsaltsalt',
|
|
221
|
-
};
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it('should return correct string', () => {
|
|
225
|
-
const crypto = new Crypto(options);
|
|
226
|
-
|
|
227
|
-
const decryptedText = crypto.decrypt(encText);
|
|
228
|
-
expect(decryptedText).toBe(plainText);
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
it('should return correct encrypt-decrypt', () => {
|
|
232
|
-
const crypto = new Crypto(options);
|
|
233
|
-
|
|
234
|
-
const encryptedText = crypto.encrypt(plainText);
|
|
235
|
-
const decryptedText = crypto.decrypt(encryptedText);
|
|
236
|
-
expect(decryptedText).toBe(plainText);
|
|
237
|
-
});
|
|
238
|
-
});
|
package/tsconfig.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2022",
|
|
4
|
-
"module": "ES2022",
|
|
5
|
-
"moduleResolution": "node",
|
|
6
|
-
"strict": true,
|
|
7
|
-
"esModuleInterop": true,
|
|
8
|
-
"outDir": "./dist",
|
|
9
|
-
"declaration": true,
|
|
10
|
-
"skipLibCheck": true
|
|
11
|
-
},
|
|
12
|
-
"include": [
|
|
13
|
-
"./src/**/*"
|
|
14
|
-
],
|
|
15
|
-
"exclude": [
|
|
16
|
-
"node_modules",
|
|
17
|
-
"dist",
|
|
18
|
-
"coverage",
|
|
19
|
-
"docs"
|
|
20
|
-
]
|
|
21
|
-
}
|
package/vitest.config.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from 'vitest/config';
|
|
2
|
-
|
|
3
|
-
export default defineConfig({
|
|
4
|
-
resolve: {
|
|
5
|
-
extensions: ['.ts', '.js']
|
|
6
|
-
},
|
|
7
|
-
test: {
|
|
8
|
-
environment: 'node',
|
|
9
|
-
coverage: {
|
|
10
|
-
provider: 'v8',
|
|
11
|
-
reporter: ['text', 'html', 'lcov', 'json'],
|
|
12
|
-
reportsDirectory: './coverage',
|
|
13
|
-
clean: true,
|
|
14
|
-
exclude: [
|
|
15
|
-
'node_modules/**',
|
|
16
|
-
'dist/**',
|
|
17
|
-
'docs/**',
|
|
18
|
-
'coverage/**',
|
|
19
|
-
'**/__tests__/**',
|
|
20
|
-
'**/*.test.ts',
|
|
21
|
-
'**/*.spec.ts',
|
|
22
|
-
'**/*.config.ts',
|
|
23
|
-
'**/index.ts'
|
|
24
|
-
],
|
|
25
|
-
include: ['src/crypto.ts'],
|
|
26
|
-
thresholds: {
|
|
27
|
-
lines: 90,
|
|
28
|
-
functions: 85,
|
|
29
|
-
branches: 90,
|
|
30
|
-
statements: 90
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
include: ['src/**/__tests__/**/*.test.ts'],
|
|
34
|
-
exclude: ['node_modules', 'dist', 'docs']
|
|
35
|
-
}
|
|
36
|
-
});
|