@vvlad1973/crypto 2.1.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/.hintrc +15 -0
- package/.vscode/launch.json +20 -0
- package/CLAUDE.md +90 -0
- package/LICENSE +19 -0
- package/README.md +253 -0
- package/dist/__tests__/crypto.test.d.ts +1 -0
- package/dist/__tests__/crypto.test.js +181 -0
- package/dist/crypto.d.ts +59 -0
- package/dist/crypto.js +89 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/hierarchy.js +1 -0
- package/docs/assets/highlight.css +78 -0
- package/docs/assets/icons.js +18 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +60 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1633 -0
- package/docs/classes/Crypto.html +20 -0
- package/docs/functions/isCrypto.html +4 -0
- package/docs/hierarchy.html +1 -0
- package/docs/index.html +43 -0
- package/docs/interfaces/CryptoOptions.html +15 -0
- package/docs/modules.html +1 -0
- package/package.json +44 -0
- package/src/__tests__/crypto.test.ts +238 -0
- package/src/crypto.ts +141 -0
- package/src/index.ts +2 -0
- package/tsconfig.json +21 -0
- package/typedoc.json +18 -0
- package/vitest.config.ts +36 -0
package/src/crypto.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createCipheriv,
|
|
3
|
+
createDecipheriv,
|
|
4
|
+
randomBytes,
|
|
5
|
+
pbkdf2Sync,
|
|
6
|
+
} from 'crypto';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Checks if an object is an instance of Crypto.
|
|
10
|
+
* @param {any} object - The object to check.
|
|
11
|
+
* @returns {boolean} Returns true if the object is an instance of Crypto, false otherwise.
|
|
12
|
+
*/
|
|
13
|
+
export function isCrypto(object: any): object is Crypto {
|
|
14
|
+
return (
|
|
15
|
+
typeof object === 'object' &&
|
|
16
|
+
object !== null &&
|
|
17
|
+
typeof object['encrypt'] === 'function' &&
|
|
18
|
+
typeof object['decrypt'] === 'function'
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Interface representing the set of parameters to create an instance of the Crypto class.
|
|
24
|
+
* @interface CryptoOptions
|
|
25
|
+
* @property {string} password - The password used for key derivation.
|
|
26
|
+
* @property {string} salt - The salt used for key derivation.
|
|
27
|
+
* @property {string} [algorithm='sha512'] - The hash algorithm to use for key derivation.
|
|
28
|
+
* @property {number} [iterations=1000] - The number of iterations to use for key derivation.
|
|
29
|
+
* @property {number} [keyLength=32] - The length of the derived key in bytes.
|
|
30
|
+
* @property {number|Buffer} [iv] - The initialization vector for the AES encryption.
|
|
31
|
+
*/
|
|
32
|
+
export interface CryptoOptions {
|
|
33
|
+
password: string;
|
|
34
|
+
salt: string;
|
|
35
|
+
algorithm?: string;
|
|
36
|
+
iterations?: number;
|
|
37
|
+
keyLength?: number;
|
|
38
|
+
iv?: number | Buffer;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Class representing a cryptographic utility for encrypting and decrypting text.
|
|
43
|
+
* @class
|
|
44
|
+
* @param {string|CryptoOptions} passwordOrOptions - The password used for key derivation or an options object.
|
|
45
|
+
* @param {string} salt - The salt used for key derivation.
|
|
46
|
+
* @param {string} [algorithm='sha512'] - The hash algorithm to use for key derivation.
|
|
47
|
+
* @param {number} [iterations=1000] - The number of iterations to use for key derivation.
|
|
48
|
+
* @param {number} [keyLength=32] - The length of the derived key in bytes.
|
|
49
|
+
* @param {number|Buffer} [iv] - The initialization vector for the AES encryption.
|
|
50
|
+
*/
|
|
51
|
+
export class Crypto {
|
|
52
|
+
private iv: Buffer;
|
|
53
|
+
private key: Buffer;
|
|
54
|
+
|
|
55
|
+
constructor(
|
|
56
|
+
password: string,
|
|
57
|
+
salt: string,
|
|
58
|
+
algorithm?: string,
|
|
59
|
+
iterations?: number,
|
|
60
|
+
keyLength?: number,
|
|
61
|
+
iv?: number | Buffer
|
|
62
|
+
);
|
|
63
|
+
constructor(options: CryptoOptions);
|
|
64
|
+
|
|
65
|
+
constructor(
|
|
66
|
+
passwordOrOptions: string | CryptoOptions,
|
|
67
|
+
salt?: string,
|
|
68
|
+
algorithm: string = 'sha512',
|
|
69
|
+
iterations: number = 1000,
|
|
70
|
+
keyLength: number = 32,
|
|
71
|
+
iv: number | Buffer = randomBytes(16)
|
|
72
|
+
) {
|
|
73
|
+
let password: string;
|
|
74
|
+
|
|
75
|
+
if (typeof passwordOrOptions === 'string') {
|
|
76
|
+
password = passwordOrOptions;
|
|
77
|
+
this.iv = typeof iv === 'number' ? this.convertNumberToIV(iv) : iv;
|
|
78
|
+
} else {
|
|
79
|
+
({
|
|
80
|
+
password,
|
|
81
|
+
salt,
|
|
82
|
+
algorithm = 'sha512',
|
|
83
|
+
iterations = 1000,
|
|
84
|
+
keyLength = 32,
|
|
85
|
+
iv = randomBytes(16),
|
|
86
|
+
} = passwordOrOptions);
|
|
87
|
+
this.iv =
|
|
88
|
+
typeof passwordOrOptions.iv === 'number'
|
|
89
|
+
? this.convertNumberToIV(passwordOrOptions.iv)
|
|
90
|
+
: passwordOrOptions.iv ?? randomBytes(16);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
this.key = pbkdf2Sync(password, salt!, iterations, keyLength, algorithm);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private convertNumberToIV(iv: number): Buffer {
|
|
97
|
+
// Создаем Buffer размером 16 байт, заполняя его значением iv
|
|
98
|
+
const buffer = Buffer.alloc(16);
|
|
99
|
+
buffer.writeUInt32BE(iv, 12); // Записываем iv в последний 4 байта Buffer'а
|
|
100
|
+
return buffer;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Encrypt a text string.
|
|
105
|
+
* @param {string} text - The plain text to encrypt.
|
|
106
|
+
* @returns {string} The encrypted text as a hex string.
|
|
107
|
+
*/
|
|
108
|
+
public encrypt(text: string): string {
|
|
109
|
+
const cipher = createCipheriv('aes-256-ctr', this.key, this.iv);
|
|
110
|
+
const encrypted = Buffer.concat([
|
|
111
|
+
cipher.update(text, 'utf8'),
|
|
112
|
+
cipher.final(),
|
|
113
|
+
]);
|
|
114
|
+
return encrypted.toString('hex');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Decrypt an encrypted text string.
|
|
119
|
+
* @param {string} text - The encrypted text as a hex string.
|
|
120
|
+
* @returns {string} The decrypted plain text.
|
|
121
|
+
*/
|
|
122
|
+
public decrypt(text: string): string {
|
|
123
|
+
const encryptedText = Buffer.from(text, 'hex');
|
|
124
|
+
const decipher = createDecipheriv('aes-256-ctr', this.key, this.iv);
|
|
125
|
+
const decrypted = Buffer.concat([
|
|
126
|
+
decipher.update(encryptedText),
|
|
127
|
+
decipher.final(),
|
|
128
|
+
]);
|
|
129
|
+
return decrypted.toString('utf8');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Returns a UUID ver.4 string.
|
|
134
|
+
* @returns {string} UUID ver.4 string.
|
|
135
|
+
*/
|
|
136
|
+
public static getUUID(): string {
|
|
137
|
+
return crypto.randomUUID();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export default Crypto;
|
package/src/index.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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/typedoc.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://typedoc.org/schema.json",
|
|
3
|
+
"entryPoints": ["./src/index.ts"],
|
|
4
|
+
"out": "docs",
|
|
5
|
+
"exclude": ["**/__tests__/**"],
|
|
6
|
+
"excludePrivate": true,
|
|
7
|
+
"excludeProtected": false,
|
|
8
|
+
"excludeExternals": true,
|
|
9
|
+
"readme": "README.md",
|
|
10
|
+
"name": "@vvlad1973/crypto",
|
|
11
|
+
"includeVersion": true,
|
|
12
|
+
"sort": ["source-order"],
|
|
13
|
+
"navigation": {
|
|
14
|
+
"includeCategories": true,
|
|
15
|
+
"includeGroups": true
|
|
16
|
+
},
|
|
17
|
+
"plugin": []
|
|
18
|
+
}
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
});
|