@digitaldefiance/node-ecies-lib 1.0.2 → 1.0.4
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/dist/services/aes-gcm.d.ts +59 -0
- package/dist/services/aes-gcm.d.ts.map +1 -0
- package/dist/services/aes-gcm.js +91 -0
- package/dist/services/aes-gcm.js.map +1 -0
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.d.ts.map +1 -1
- package/dist/services/index.js +1 -0
- package/dist/services/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export declare abstract class AESGCMService {
|
|
2
|
+
static readonly ALGORITHM_NAME: string;
|
|
3
|
+
static readonly MODE: string;
|
|
4
|
+
static readonly KEY_BITS: number;
|
|
5
|
+
/**
|
|
6
|
+
* Encrypt data using AES-GCM
|
|
7
|
+
* @param data Data to encrypt
|
|
8
|
+
* @param key Key to use for encryption (must be 16, 24 or 32 bytes for AES)
|
|
9
|
+
* @param authTag Whether to return separate auth tag
|
|
10
|
+
* @returns Encrypted data with IV and optional separate auth tag
|
|
11
|
+
*/
|
|
12
|
+
static encrypt(data: Buffer, key: Buffer, authTag?: boolean): {
|
|
13
|
+
encrypted: Buffer;
|
|
14
|
+
iv: Buffer;
|
|
15
|
+
tag?: Buffer;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Combine encrypted data and auth tag into a single Buffer
|
|
19
|
+
* @param encryptedData The encrypted data
|
|
20
|
+
* @param authTag The authentication tag
|
|
21
|
+
* @returns The combined Buffer
|
|
22
|
+
*/
|
|
23
|
+
static combineEncryptedDataAndTag(encryptedData: Buffer, authTag: Buffer): Buffer;
|
|
24
|
+
/**
|
|
25
|
+
* Combine IV and encrypted data (with optional auth tag) into a single Buffer
|
|
26
|
+
* @param iv The initialization vector
|
|
27
|
+
* @param encryptedDataWithTag The encrypted data with auth tag already appended (if applicable)
|
|
28
|
+
* @returns The combined Buffer
|
|
29
|
+
*/
|
|
30
|
+
static combineIvAndEncryptedData(iv: Buffer, encryptedDataWithTag: Buffer): Buffer;
|
|
31
|
+
/**
|
|
32
|
+
* Combine IV, encrypted data and auth tag into a single Buffer
|
|
33
|
+
* @param iv The initialization vector
|
|
34
|
+
* @param encryptedData The encrypted data
|
|
35
|
+
* @param authTag The authentication tag
|
|
36
|
+
* @returns The combined Buffer
|
|
37
|
+
*/
|
|
38
|
+
static combineIvTagAndEncryptedData(iv: Buffer, encryptedData: Buffer, authTag: Buffer): Buffer;
|
|
39
|
+
/**
|
|
40
|
+
* Split combined encrypted data back into its components
|
|
41
|
+
* @param combinedData The combined data containing IV, encrypted data, and optionally auth tag
|
|
42
|
+
* @param hasAuthTag Whether the combined data includes an authentication tag
|
|
43
|
+
* @returns Object containing the split components
|
|
44
|
+
*/
|
|
45
|
+
static splitEncryptedData(combinedData: Buffer, hasAuthTag?: boolean): {
|
|
46
|
+
iv: Buffer;
|
|
47
|
+
encryptedDataWithTag: Buffer;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Decrypt data using AES-GCM
|
|
51
|
+
* @param iv The initialization vector
|
|
52
|
+
* @param encryptedData Data to decrypt (with auth tag appended)
|
|
53
|
+
* @param key Key to use for decryption (must be 16, 24 or 32 bytes for AES)
|
|
54
|
+
* @param authTag Whether the encrypted data includes an authentication tag
|
|
55
|
+
* @returns Decrypted data
|
|
56
|
+
*/
|
|
57
|
+
static decrypt(iv: Buffer, encryptedData: Buffer, key: Buffer, authTag?: boolean): Buffer;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=aes-gcm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aes-gcm.d.ts","sourceRoot":"","sources":["../../src/services/aes-gcm.ts"],"names":[],"mappings":"AAGA,8BAAsB,aAAa;IACjC,gBAAuB,cAAc,SAA+B;IACpE,gBAAuB,IAAI,SAA0B;IACrD,gBAAuB,QAAQ,SAA8B;IAE7D;;;;;;OAMG;WACW,OAAO,CACnB,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,OAAe,GACvB;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,CAAA;KAAE;IAmBlD;;;;;OAKG;WACW,0BAA0B,CACtC,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,MAAM,GACd,MAAM;IAIT;;;;;OAKG;WACW,yBAAyB,CACrC,EAAE,EAAE,MAAM,EACV,oBAAoB,EAAE,MAAM,GAC3B,MAAM;IAIT;;;;;;OAMG;WACW,4BAA4B,CACxC,EAAE,EAAE,MAAM,EACV,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,MAAM,GACd,MAAM;IAQT;;;;;OAKG;WACW,kBAAkB,CAC9B,YAAY,EAAE,MAAM,EACpB,UAAU,GAAE,OAAc,GACzB;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,oBAAoB,EAAE,MAAM,CAAA;KAAE;IAgB/C;;;;;;;OAOG;WACW,OAAO,CACnB,EAAE,EAAE,MAAM,EACV,aAAa,EAAE,MAAM,EACrB,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,OAAe,GACvB,MAAM;CAWV"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, randomBytes } from 'crypto';
|
|
2
|
+
import { Constants } from '../constants';
|
|
3
|
+
export class AESGCMService {
|
|
4
|
+
static ALGORITHM_NAME = Constants.KEYRING.ALGORITHM;
|
|
5
|
+
static MODE = Constants.KEYRING.MODE;
|
|
6
|
+
static KEY_BITS = Constants.KEYRING.KEY_BITS;
|
|
7
|
+
/**
|
|
8
|
+
* Encrypt data using AES-GCM
|
|
9
|
+
* @param data Data to encrypt
|
|
10
|
+
* @param key Key to use for encryption (must be 16, 24 or 32 bytes for AES)
|
|
11
|
+
* @param authTag Whether to return separate auth tag
|
|
12
|
+
* @returns Encrypted data with IV and optional separate auth tag
|
|
13
|
+
*/
|
|
14
|
+
static encrypt(data, key, authTag = false) {
|
|
15
|
+
const iv = randomBytes(Constants.WRAPPED_KEY.IV_SIZE);
|
|
16
|
+
const cipher = createCipheriv(Constants.KEYRING_ALGORITHM_CONFIGURATION, key, iv);
|
|
17
|
+
const encrypted = Buffer.concat([cipher.update(data), cipher.final()]);
|
|
18
|
+
const tag = cipher.getAuthTag();
|
|
19
|
+
if (!authTag) {
|
|
20
|
+
const encryptedWithTag = Buffer.concat([encrypted, tag]);
|
|
21
|
+
return { encrypted: encryptedWithTag, iv: iv };
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
encrypted: encrypted,
|
|
25
|
+
iv: iv,
|
|
26
|
+
tag: tag,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Combine encrypted data and auth tag into a single Buffer
|
|
31
|
+
* @param encryptedData The encrypted data
|
|
32
|
+
* @param authTag The authentication tag
|
|
33
|
+
* @returns The combined Buffer
|
|
34
|
+
*/
|
|
35
|
+
static combineEncryptedDataAndTag(encryptedData, authTag) {
|
|
36
|
+
return Buffer.concat([encryptedData, authTag]);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Combine IV and encrypted data (with optional auth tag) into a single Buffer
|
|
40
|
+
* @param iv The initialization vector
|
|
41
|
+
* @param encryptedDataWithTag The encrypted data with auth tag already appended (if applicable)
|
|
42
|
+
* @returns The combined Buffer
|
|
43
|
+
*/
|
|
44
|
+
static combineIvAndEncryptedData(iv, encryptedDataWithTag) {
|
|
45
|
+
return Buffer.concat([iv, encryptedDataWithTag]);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Combine IV, encrypted data and auth tag into a single Buffer
|
|
49
|
+
* @param iv The initialization vector
|
|
50
|
+
* @param encryptedData The encrypted data
|
|
51
|
+
* @param authTag The authentication tag
|
|
52
|
+
* @returns The combined Buffer
|
|
53
|
+
*/
|
|
54
|
+
static combineIvTagAndEncryptedData(iv, encryptedData, authTag) {
|
|
55
|
+
const encryptedWithTag = AESGCMService.combineEncryptedDataAndTag(encryptedData, authTag);
|
|
56
|
+
return AESGCMService.combineIvAndEncryptedData(iv, encryptedWithTag);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Split combined encrypted data back into its components
|
|
60
|
+
* @param combinedData The combined data containing IV, encrypted data, and optionally auth tag
|
|
61
|
+
* @param hasAuthTag Whether the combined data includes an authentication tag
|
|
62
|
+
* @returns Object containing the split components
|
|
63
|
+
*/
|
|
64
|
+
static splitEncryptedData(combinedData, hasAuthTag = true) {
|
|
65
|
+
const ivLength = Constants.WRAPPED_KEY.IV_SIZE;
|
|
66
|
+
const minLength = ivLength + (hasAuthTag ? 16 : 0);
|
|
67
|
+
if (combinedData.length < minLength) {
|
|
68
|
+
throw new Error('Combined data is too short to contain required components');
|
|
69
|
+
}
|
|
70
|
+
const iv = combinedData.slice(0, ivLength);
|
|
71
|
+
const encryptedDataWithTag = combinedData.slice(ivLength);
|
|
72
|
+
return { iv, encryptedDataWithTag };
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Decrypt data using AES-GCM
|
|
76
|
+
* @param iv The initialization vector
|
|
77
|
+
* @param encryptedData Data to decrypt (with auth tag appended)
|
|
78
|
+
* @param key Key to use for decryption (must be 16, 24 or 32 bytes for AES)
|
|
79
|
+
* @param authTag Whether the encrypted data includes an authentication tag
|
|
80
|
+
* @returns Decrypted data
|
|
81
|
+
*/
|
|
82
|
+
static decrypt(iv, encryptedData, key, authTag = false) {
|
|
83
|
+
const decipher = createDecipheriv(Constants.KEYRING_ALGORITHM_CONFIGURATION, key, iv);
|
|
84
|
+
const tagLength = 16;
|
|
85
|
+
const tag = encryptedData.subarray(-tagLength);
|
|
86
|
+
const ciphertext = encryptedData.subarray(0, -tagLength);
|
|
87
|
+
decipher.setAuthTag(tag);
|
|
88
|
+
return Buffer.concat([decipher.update(ciphertext),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ACI,MAAM,CAAC,OAAO,CACnB,EAAU,EACV,aAAqB,EACrB,GAAW,EACX,UAAmB,KAAK;QAExB,MAAM,QAAQ,GAAG,gBAAgB,CAAC,SAAS,CAAC,+BAA+B,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;QAEtF,MAAM,SAAS,GAAG,EAAE,CAAC;QACrB,MAAM,GAAG,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAC,CAAC;QAC/C,MAAM,UAAU,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC;QAEzD,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QAEzB,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IACxE,CAAC"}
|
package/dist/services/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|
package/dist/services/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC"}
|