@digitaldefiance/ecies-lib 1.0.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 +237 -0
- package/dist/backup-code-string.d.ts +26 -0
- package/dist/backup-code-string.d.ts.map +1 -0
- package/dist/backup-code-string.js +69 -0
- package/dist/backup-code-string.js.map +1 -0
- package/dist/constants.d.ts +22 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +177 -0
- package/dist/constants.js.map +1 -0
- package/dist/email-string.d.ts +42 -0
- package/dist/email-string.d.ts.map +1 -0
- package/dist/email-string.js +67 -0
- package/dist/email-string.js.map +1 -0
- package/dist/enumerations/ecies-encryption-type.d.ts +15 -0
- package/dist/enumerations/ecies-encryption-type.d.ts.map +1 -0
- package/dist/enumerations/ecies-encryption-type.js +63 -0
- package/dist/enumerations/ecies-encryption-type.js.map +1 -0
- package/dist/enumerations/ecies-error-type.d.ts +32 -0
- package/dist/enumerations/ecies-error-type.d.ts.map +1 -0
- package/dist/enumerations/ecies-error-type.js +33 -0
- package/dist/enumerations/ecies-error-type.js.map +1 -0
- package/dist/enumerations/guid-brand-type.d.ts +27 -0
- package/dist/enumerations/guid-brand-type.d.ts.map +1 -0
- package/dist/enumerations/guid-brand-type.js +28 -0
- package/dist/enumerations/guid-brand-type.js.map +1 -0
- package/dist/enumerations/guid-error-type.d.ts +7 -0
- package/dist/enumerations/guid-error-type.d.ts.map +1 -0
- package/dist/enumerations/guid-error-type.js +8 -0
- package/dist/enumerations/guid-error-type.js.map +1 -0
- package/dist/enumerations/index.d.ts +13 -0
- package/dist/enumerations/index.d.ts.map +1 -0
- package/dist/enumerations/index.js +13 -0
- package/dist/enumerations/index.js.map +1 -0
- package/dist/enumerations/invalid-email-type.d.ts +6 -0
- package/dist/enumerations/invalid-email-type.d.ts.map +1 -0
- package/dist/enumerations/invalid-email-type.js +7 -0
- package/dist/enumerations/invalid-email-type.js.map +1 -0
- package/dist/enumerations/length-encoding-type.d.ts +7 -0
- package/dist/enumerations/length-encoding-type.d.ts.map +1 -0
- package/dist/enumerations/length-encoding-type.js +8 -0
- package/dist/enumerations/length-encoding-type.js.map +1 -0
- package/dist/enumerations/length-error-type.d.ts +6 -0
- package/dist/enumerations/length-error-type.d.ts.map +1 -0
- package/dist/enumerations/length-error-type.js +7 -0
- package/dist/enumerations/length-error-type.js.map +1 -0
- package/dist/enumerations/member-error-type.d.ts +87 -0
- package/dist/enumerations/member-error-type.d.ts.map +1 -0
- package/dist/enumerations/member-error-type.js +88 -0
- package/dist/enumerations/member-error-type.js.map +1 -0
- package/dist/enumerations/member-type.d.ts +16 -0
- package/dist/enumerations/member-type.d.ts.map +1 -0
- package/dist/enumerations/member-type.js +17 -0
- package/dist/enumerations/member-type.js.map +1 -0
- package/dist/enumerations/pbkdf2-error-type.d.ts +5 -0
- package/dist/enumerations/pbkdf2-error-type.d.ts.map +1 -0
- package/dist/enumerations/pbkdf2-error-type.js +6 -0
- package/dist/enumerations/pbkdf2-error-type.js.map +1 -0
- package/dist/enumerations/pbkdf2-profile.d.ts +5 -0
- package/dist/enumerations/pbkdf2-profile.d.ts.map +1 -0
- package/dist/enumerations/pbkdf2-profile.js +6 -0
- package/dist/enumerations/pbkdf2-profile.js.map +1 -0
- package/dist/enumerations/secure-storage-error-type.d.ts +6 -0
- package/dist/enumerations/secure-storage-error-type.d.ts.map +1 -0
- package/dist/enumerations/secure-storage-error-type.js +7 -0
- package/dist/enumerations/secure-storage-error-type.js.map +1 -0
- package/dist/errors/disposed.d.ts +4 -0
- package/dist/errors/disposed.d.ts.map +1 -0
- package/dist/errors/disposed.js +8 -0
- package/dist/errors/disposed.js.map +1 -0
- package/dist/errors/ecies.d.ts +6 -0
- package/dist/errors/ecies.d.ts.map +1 -0
- package/dist/errors/ecies.js +8 -0
- package/dist/errors/ecies.js.map +1 -0
- package/dist/errors/guid.d.ts +14 -0
- package/dist/errors/guid.d.ts.map +1 -0
- package/dist/errors/guid.js +19 -0
- package/dist/errors/guid.js.map +1 -0
- package/dist/errors/index.d.ts +9 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +9 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/errors/invalid-backup-code.d.ts +4 -0
- package/dist/errors/invalid-backup-code.d.ts.map +1 -0
- package/dist/errors/invalid-backup-code.js +7 -0
- package/dist/errors/invalid-backup-code.js.map +1 -0
- package/dist/errors/invalid-email.d.ts +6 -0
- package/dist/errors/invalid-email.d.ts.map +1 -0
- package/dist/errors/invalid-email.js +9 -0
- package/dist/errors/invalid-email.js.map +1 -0
- package/dist/errors/length.d.ts +6 -0
- package/dist/errors/length.d.ts.map +1 -0
- package/dist/errors/length.js +9 -0
- package/dist/errors/length.js.map +1 -0
- package/dist/errors/member.d.ts +6 -0
- package/dist/errors/member.d.ts.map +1 -0
- package/dist/errors/member.js +8 -0
- package/dist/errors/member.js.map +1 -0
- package/dist/errors/pbkdf2.d.ts +6 -0
- package/dist/errors/pbkdf2.d.ts.map +1 -0
- package/dist/errors/pbkdf2.js +8 -0
- package/dist/errors/pbkdf2.js.map +1 -0
- package/dist/errors/secure-storage.d.ts +6 -0
- package/dist/errors/secure-storage.d.ts.map +1 -0
- package/dist/errors/secure-storage.js +8 -0
- package/dist/errors/secure-storage.js.map +1 -0
- package/dist/errors/typed.d.ts +5 -0
- package/dist/errors/typed.d.ts.map +1 -0
- package/dist/errors/typed.js +11 -0
- package/dist/errors/typed.js.map +1 -0
- package/dist/guid.d.ts +153 -0
- package/dist/guid.d.ts.map +1 -0
- package/dist/guid.js +604 -0
- package/dist/guid.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/backup-code-consts.d.ts +16 -0
- package/dist/interfaces/backup-code-consts.d.ts.map +1 -0
- package/dist/interfaces/backup-code-consts.js +2 -0
- package/dist/interfaces/backup-code-consts.js.map +1 -0
- package/dist/interfaces/backup-code.d.ts +7 -0
- package/dist/interfaces/backup-code.d.ts.map +1 -0
- package/dist/interfaces/backup-code.js +2 -0
- package/dist/interfaces/backup-code.js.map +1 -0
- package/dist/interfaces/constants.d.ts +45 -0
- package/dist/interfaces/constants.d.ts.map +1 -0
- package/dist/interfaces/constants.js +2 -0
- package/dist/interfaces/constants.js.map +1 -0
- package/dist/interfaces/ecies-config.d.ts +9 -0
- package/dist/interfaces/ecies-config.d.ts.map +1 -0
- package/dist/interfaces/ecies-config.js +2 -0
- package/dist/interfaces/ecies-config.js.map +1 -0
- package/dist/interfaces/ecies-consts.d.ts +58 -0
- package/dist/interfaces/ecies-consts.d.ts.map +1 -0
- package/dist/interfaces/ecies-consts.js +2 -0
- package/dist/interfaces/ecies-consts.js.map +1 -0
- package/dist/interfaces/ecies-file-service.d.ts +7 -0
- package/dist/interfaces/ecies-file-service.d.ts.map +1 -0
- package/dist/interfaces/ecies-file-service.js +2 -0
- package/dist/interfaces/ecies-file-service.js.map +1 -0
- package/dist/interfaces/guid.d.ts +45 -0
- package/dist/interfaces/guid.d.ts.map +1 -0
- package/dist/interfaces/guid.js +2 -0
- package/dist/interfaces/guid.js.map +1 -0
- package/dist/interfaces/index.d.ts +14 -0
- package/dist/interfaces/index.d.ts.map +1 -0
- package/dist/interfaces/index.js +14 -0
- package/dist/interfaces/index.js.map +1 -0
- package/dist/interfaces/member-operational.d.ts +39 -0
- package/dist/interfaces/member-operational.d.ts.map +1 -0
- package/dist/interfaces/member-operational.js +2 -0
- package/dist/interfaces/member-operational.js.map +1 -0
- package/dist/interfaces/member-storage.d.ts +21 -0
- package/dist/interfaces/member-storage.d.ts.map +1 -0
- package/dist/interfaces/member-storage.js +2 -0
- package/dist/interfaces/member-storage.js.map +1 -0
- package/dist/interfaces/member-with-mnemonic.d.ts +7 -0
- package/dist/interfaces/member-with-mnemonic.d.ts.map +1 -0
- package/dist/interfaces/member-with-mnemonic.js +2 -0
- package/dist/interfaces/member-with-mnemonic.js.map +1 -0
- package/dist/interfaces/pbkdf2-config.d.ts +7 -0
- package/dist/interfaces/pbkdf2-config.d.ts.map +1 -0
- package/dist/interfaces/pbkdf2-config.js +2 -0
- package/dist/interfaces/pbkdf2-config.js.map +1 -0
- package/dist/interfaces/pbkdf2-consts.d.ts +9 -0
- package/dist/interfaces/pbkdf2-consts.d.ts.map +1 -0
- package/dist/interfaces/pbkdf2-consts.js +2 -0
- package/dist/interfaces/pbkdf2-consts.js.map +1 -0
- package/dist/interfaces/pbkdf2-result.d.ts +6 -0
- package/dist/interfaces/pbkdf2-result.d.ts.map +1 -0
- package/dist/interfaces/pbkdf2-result.js +2 -0
- package/dist/interfaces/pbkdf2-result.js.map +1 -0
- package/dist/member.d.ts +68 -0
- package/dist/member.d.ts.map +1 -0
- package/dist/member.js +256 -0
- package/dist/member.js.map +1 -0
- package/dist/pbkdf2-profiles.d.ts +6 -0
- package/dist/pbkdf2-profiles.d.ts.map +1 -0
- package/dist/pbkdf2-profiles.js +2 -0
- package/dist/pbkdf2-profiles.js.map +1 -0
- package/dist/secure-buffer.d.ts +38 -0
- package/dist/secure-buffer.d.ts.map +1 -0
- package/dist/secure-buffer.js +161 -0
- package/dist/secure-buffer.js.map +1 -0
- package/dist/secure-string.d.ts +39 -0
- package/dist/secure-string.d.ts.map +1 -0
- package/dist/secure-string.js +188 -0
- package/dist/secure-string.js.map +1 -0
- package/dist/services/aes-gcm.d.ts +56 -0
- package/dist/services/aes-gcm.d.ts.map +1 -0
- package/dist/services/aes-gcm.js +101 -0
- package/dist/services/aes-gcm.js.map +1 -0
- package/dist/services/ecies/crypto-core.d.ts +53 -0
- package/dist/services/ecies/crypto-core.d.ts.map +1 -0
- package/dist/services/ecies/crypto-core.js +158 -0
- package/dist/services/ecies/crypto-core.js.map +1 -0
- package/dist/services/ecies/example.d.ts +25 -0
- package/dist/services/ecies/example.d.ts.map +1 -0
- package/dist/services/ecies/example.js +121 -0
- package/dist/services/ecies/example.js.map +1 -0
- package/dist/services/ecies/file.d.ts +18 -0
- package/dist/services/ecies/file.d.ts.map +1 -0
- package/dist/services/ecies/file.js +106 -0
- package/dist/services/ecies/file.js.map +1 -0
- package/dist/services/ecies/index.d.ts +38 -0
- package/dist/services/ecies/index.d.ts.map +1 -0
- package/dist/services/ecies/index.js +39 -0
- package/dist/services/ecies/index.js.map +1 -0
- package/dist/services/ecies/integration.d.ts +59 -0
- package/dist/services/ecies/integration.d.ts.map +1 -0
- package/dist/services/ecies/integration.js +163 -0
- package/dist/services/ecies/integration.js.map +1 -0
- package/dist/services/ecies/interfaces.d.ts +51 -0
- package/dist/services/ecies/interfaces.d.ts.map +1 -0
- package/dist/services/ecies/interfaces.js +5 -0
- package/dist/services/ecies/interfaces.js.map +1 -0
- package/dist/services/ecies/manual-test.d.ts +29 -0
- package/dist/services/ecies/manual-test.d.ts.map +1 -0
- package/dist/services/ecies/manual-test.js +163 -0
- package/dist/services/ecies/manual-test.js.map +1 -0
- package/dist/services/ecies/multi-recipient.d.ts +49 -0
- package/dist/services/ecies/multi-recipient.d.ts.map +1 -0
- package/dist/services/ecies/multi-recipient.js +222 -0
- package/dist/services/ecies/multi-recipient.js.map +1 -0
- package/dist/services/ecies/service.d.ts +100 -0
- package/dist/services/ecies/service.d.ts.map +1 -0
- package/dist/services/ecies/service.js +186 -0
- package/dist/services/ecies/service.js.map +1 -0
- package/dist/services/ecies/signature.d.ts +27 -0
- package/dist/services/ecies/signature.d.ts.map +1 -0
- package/dist/services/ecies/signature.js +72 -0
- package/dist/services/ecies/signature.js.map +1 -0
- package/dist/services/ecies/single-recipient.d.ts +43 -0
- package/dist/services/ecies/single-recipient.d.ts.map +1 -0
- package/dist/services/ecies/single-recipient.js +196 -0
- package/dist/services/ecies/single-recipient.js.map +1 -0
- package/dist/services/index.d.ts +5 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +5 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/password-login.d.ts +41 -0
- package/dist/services/password-login.d.ts.map +1 -0
- package/dist/services/password-login.js +96 -0
- package/dist/services/password-login.js.map +1 -0
- package/dist/services/pbkdf2.d.ts +49 -0
- package/dist/services/pbkdf2.d.ts.map +1 -0
- package/dist/services/pbkdf2.js +97 -0
- package/dist/services/pbkdf2.js.map +1 -0
- package/dist/services/xor.d.ts +37 -0
- package/dist/services/xor.d.ts.map +1 -0
- package/dist/services/xor.js +63 -0
- package/dist/services/xor.js.map +1 -0
- package/dist/types.d.ts +29 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +62 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +244 -0
- package/dist/utils.js.map +1 -0
- package/dist/xor.d.ts +37 -0
- package/dist/xor.d.ts.map +1 -0
- package/dist/xor.js +63 -0
- package/dist/xor.js.map +1 -0
- package/package.json +60 -0
package/dist/utils.js
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { LengthEncodingType } from './enumerations/length-encoding-type';
|
|
2
|
+
import { LengthErrorType } from './enumerations/length-error-type';
|
|
3
|
+
import { LengthError } from './errors';
|
|
4
|
+
/**
|
|
5
|
+
* Encodes the length of the data in the buffer
|
|
6
|
+
* @param buffer The buffer to encode
|
|
7
|
+
* @returns The encoded buffer
|
|
8
|
+
*/
|
|
9
|
+
export function lengthEncodeData(buffer) {
|
|
10
|
+
const lengthType = getLengthEncodingTypeForLength(buffer.length);
|
|
11
|
+
const lengthTypeSize = getLengthForLengthType(lengthType);
|
|
12
|
+
const result = Buffer.alloc(1 + lengthTypeSize + buffer.length);
|
|
13
|
+
result.writeUInt8(lengthType, 0);
|
|
14
|
+
switch (lengthType) {
|
|
15
|
+
case LengthEncodingType.UInt8:
|
|
16
|
+
result.writeUInt8(buffer.length, 1);
|
|
17
|
+
break;
|
|
18
|
+
case LengthEncodingType.UInt16:
|
|
19
|
+
result.writeUInt16BE(buffer.length, 1);
|
|
20
|
+
break;
|
|
21
|
+
case LengthEncodingType.UInt32:
|
|
22
|
+
result.writeUInt32BE(buffer.length, 1);
|
|
23
|
+
break;
|
|
24
|
+
case LengthEncodingType.UInt64:
|
|
25
|
+
result.writeBigUInt64BE(BigInt(buffer.length), 1);
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
buffer.copy(result, 1 + lengthTypeSize);
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
export function decodeLengthEncodedData(buffer) {
|
|
32
|
+
if (buffer.length < 1) {
|
|
33
|
+
throw new LengthError(LengthErrorType.Error_LengthIsTooShort, 'Buffer is too short to read length type.');
|
|
34
|
+
}
|
|
35
|
+
const lengthType = getLengthEncodingTypeFromValue(buffer.readUint8(0));
|
|
36
|
+
const lengthTypeSize = getLengthForLengthType(lengthType);
|
|
37
|
+
if (buffer.length < 1 + lengthTypeSize) {
|
|
38
|
+
throw new LengthError(LengthErrorType.Error_LengthIsTooShort, 'Buffer is too short to read the full length value.');
|
|
39
|
+
}
|
|
40
|
+
let length;
|
|
41
|
+
switch (lengthType) {
|
|
42
|
+
case LengthEncodingType.UInt8:
|
|
43
|
+
length = buffer.readUint8(1);
|
|
44
|
+
break;
|
|
45
|
+
case LengthEncodingType.UInt16:
|
|
46
|
+
length = buffer.readUint16BE(1);
|
|
47
|
+
break;
|
|
48
|
+
case LengthEncodingType.UInt32:
|
|
49
|
+
length = buffer.readUint32BE(1);
|
|
50
|
+
break;
|
|
51
|
+
case LengthEncodingType.UInt64:
|
|
52
|
+
length = buffer.readBigUInt64BE(1);
|
|
53
|
+
if (Number(length) > Number.MAX_SAFE_INTEGER) {
|
|
54
|
+
throw new LengthError(LengthErrorType.Error_LengthIsTooLong, 'Length exceeds maximum safe integer value');
|
|
55
|
+
}
|
|
56
|
+
break;
|
|
57
|
+
default:
|
|
58
|
+
throw new LengthError(LengthErrorType.Error_LengthIsInvalidType, 'Invalid length type.');
|
|
59
|
+
}
|
|
60
|
+
const totalLength = 1 + lengthTypeSize + Number(length);
|
|
61
|
+
if (totalLength > buffer.length) {
|
|
62
|
+
throw new LengthError(LengthErrorType.Error_LengthIsTooShort, 'Buffer is too short for declared data length');
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
data: buffer.subarray(1 + lengthTypeSize, totalLength),
|
|
66
|
+
totalLength,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export function uint8ArrayToBase64(uint8Array) {
|
|
70
|
+
let binaryString = '';
|
|
71
|
+
for (let i = 0; i < uint8Array.length; i++) {
|
|
72
|
+
binaryString += String.fromCharCode(uint8Array[i]);
|
|
73
|
+
}
|
|
74
|
+
return btoa(binaryString);
|
|
75
|
+
}
|
|
76
|
+
export function base64ToUint8Array(base64String) {
|
|
77
|
+
const binaryString = atob(base64String);
|
|
78
|
+
const len = binaryString.length;
|
|
79
|
+
const bytes = new Uint8Array(len);
|
|
80
|
+
for (let i = 0; i < len; i++) {
|
|
81
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
82
|
+
}
|
|
83
|
+
return bytes;
|
|
84
|
+
}
|
|
85
|
+
export function uint8ArrayToHex(uint8Array) {
|
|
86
|
+
return Array.from(uint8Array)
|
|
87
|
+
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
88
|
+
.join('');
|
|
89
|
+
}
|
|
90
|
+
export function hexToUint8Array(hexString) {
|
|
91
|
+
const len = hexString.length;
|
|
92
|
+
const bytes = new Uint8Array(len / 2);
|
|
93
|
+
for (let i = 0; i < len; i += 2) {
|
|
94
|
+
bytes[i / 2] = parseInt(hexString.substring(i, i + 2), 16);
|
|
95
|
+
}
|
|
96
|
+
return bytes;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Utility functions for browser ECIES implementation
|
|
100
|
+
*/
|
|
101
|
+
/**
|
|
102
|
+
* CRC16-CCITT implementation for data integrity checking
|
|
103
|
+
* Uses CRC16-CCITT-FALSE variant (init 0xFFFF)
|
|
104
|
+
*/
|
|
105
|
+
export function crc16(data) {
|
|
106
|
+
let crc = 0xffff; // Initial value for CRC16-CCITT-FALSE
|
|
107
|
+
const polynomial = 0x1021; // CRC16-CCITT polynomial
|
|
108
|
+
for (let i = 0; i < data.length; i++) {
|
|
109
|
+
crc ^= data[i] << 8;
|
|
110
|
+
for (let j = 0; j < 8; j++) {
|
|
111
|
+
if (crc & 0x8000) {
|
|
112
|
+
crc = (crc << 1) ^ polynomial;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
crc = crc << 1;
|
|
116
|
+
}
|
|
117
|
+
crc &= 0xffff; // Keep it 16-bit
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const result = new Uint8Array(2);
|
|
121
|
+
result[0] = (crc >>> 8) & 0xff; // Big-endian
|
|
122
|
+
result[1] = crc & 0xff;
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Convert string to Uint8Array (UTF-8 encoding)
|
|
127
|
+
*/
|
|
128
|
+
export function stringToUint8Array(str) {
|
|
129
|
+
return new TextEncoder().encode(str);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Convert Uint8Array to string (UTF-8 decoding)
|
|
133
|
+
*/
|
|
134
|
+
export function uint8ArrayToString(array) {
|
|
135
|
+
return new TextDecoder().decode(array);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Secure random bytes generation
|
|
139
|
+
*/
|
|
140
|
+
export function randomBytes(length) {
|
|
141
|
+
return crypto.getRandomValues(new Uint8Array(length));
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Compare two Uint8Arrays for equality
|
|
145
|
+
*/
|
|
146
|
+
export function arraysEqual(a, b) {
|
|
147
|
+
if (a.length !== b.length)
|
|
148
|
+
return false;
|
|
149
|
+
for (let i = 0; i < a.length; i++) {
|
|
150
|
+
if (a[i] !== b[i])
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Concatenate multiple Uint8Arrays
|
|
157
|
+
*/
|
|
158
|
+
export function concatUint8Arrays(...arrays) {
|
|
159
|
+
const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
160
|
+
const result = new Uint8Array(totalLength);
|
|
161
|
+
let offset = 0;
|
|
162
|
+
for (const array of arrays) {
|
|
163
|
+
result.set(array, offset);
|
|
164
|
+
offset += array.length;
|
|
165
|
+
}
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Get the length encoding type for a given length
|
|
170
|
+
* @param length The length to evaluate
|
|
171
|
+
* @returns The corresponding LengthEncodingType
|
|
172
|
+
*/
|
|
173
|
+
export function getLengthEncodingTypeForLength(length) {
|
|
174
|
+
if (typeof length === 'number') {
|
|
175
|
+
if (length < 256) {
|
|
176
|
+
return LengthEncodingType.UInt8;
|
|
177
|
+
}
|
|
178
|
+
else if (length < 65536) {
|
|
179
|
+
return LengthEncodingType.UInt16;
|
|
180
|
+
}
|
|
181
|
+
else if (length < 4294967296) {
|
|
182
|
+
return LengthEncodingType.UInt32;
|
|
183
|
+
}
|
|
184
|
+
else if (length < Number.MAX_SAFE_INTEGER) {
|
|
185
|
+
return LengthEncodingType.UInt64;
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
throw new LengthError(LengthErrorType.Error_LengthIsTooLong, 'Length is too long.');
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
else if (typeof length === 'bigint') {
|
|
192
|
+
if (length < 256n) {
|
|
193
|
+
return LengthEncodingType.UInt8;
|
|
194
|
+
}
|
|
195
|
+
else if (length < 65536n) {
|
|
196
|
+
return LengthEncodingType.UInt16;
|
|
197
|
+
}
|
|
198
|
+
else if (length < 4294967296n) {
|
|
199
|
+
return LengthEncodingType.UInt32;
|
|
200
|
+
}
|
|
201
|
+
else if (length < 18446744073709551616n) {
|
|
202
|
+
return LengthEncodingType.UInt64;
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
throw new LengthError(LengthErrorType.Error_LengthIsTooLong, 'Length is too long.');
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
throw new LengthError(LengthErrorType.Error_LengthIsInvalidType, 'Invalid length type.');
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Get the length encoding type for a given value
|
|
214
|
+
* @param value The value to evaluate
|
|
215
|
+
* @returns The corresponding LengthEncodingType
|
|
216
|
+
*/
|
|
217
|
+
export function getLengthEncodingTypeFromValue(value) {
|
|
218
|
+
for (const length of Object.values(LengthEncodingType)) {
|
|
219
|
+
if (length === value) {
|
|
220
|
+
return length;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
throw new LengthError(LengthErrorType.Error_LengthIsInvalidType, 'Invalid length type.');
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Get the length in bytes for a given LengthEncodingType
|
|
227
|
+
* @param type The LengthEncodingType to evaluate
|
|
228
|
+
* @returns The length in bytes
|
|
229
|
+
*/
|
|
230
|
+
export function getLengthForLengthType(type) {
|
|
231
|
+
switch (type) {
|
|
232
|
+
case LengthEncodingType.UInt8:
|
|
233
|
+
return 1;
|
|
234
|
+
case LengthEncodingType.UInt16:
|
|
235
|
+
return 2;
|
|
236
|
+
case LengthEncodingType.UInt32:
|
|
237
|
+
return 4;
|
|
238
|
+
case LengthEncodingType.UInt64:
|
|
239
|
+
return 8;
|
|
240
|
+
default:
|
|
241
|
+
throw new LengthError(LengthErrorType.Error_LengthIsInvalidType, 'Invalid length type.');
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AACzE,OAAO,EAAE,eA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AC;IACH,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,WAAW,CACnB,eAAe,CAAC,yBAAyB,EACzC,sBAAsB,CACvB,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,8BAA8B,CAC5C,KAAa;IAEb,KAAK,MAAM,MAAM,IAAI,MAAM,CAAC,MAAM,CAAC,kBAAkB,CAAC,EAAE,CAAC;QACvD,IAAI,MAAM,KAAK,KAAK,EAAE,CAAC;YACrB,OAAO,MAAM,CAAC;QAChB,CAAC;IACH,CAAC;IACD,MAAM,IAAI,WAAW,CACnB,eAAe,CAAC,yBAAyB,EACzC,sBAAsB,CACvB,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,sBAAsB,CAAC,IAAwB;IAC7D,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,kBAAkB,CAAC,KAAK;YAC3B,OAAO,CAAC,CAAC;QACX,KAAK,kBAAkB,CAAC,MAAM;YAC5B,OAAO,CAAC,CAAC;QACX,KAAK,kBAAkB,CAAC,MAAM;YAC5B,OAAO,CAAC,CAAC;QACX,KAAK,kBAAkB,CAAC,MAAM;YAC5B,OAAO,CAAC,CAAC;QACX;YACE,MAAM,IAAI,WAAW,CACnB,eAAe,CAAC,yBAAyB,EACzC,sBAAsB,CACvB,CAAC;IACN,CAAC;AACH,CAAC"}
|
package/dist/xor.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A simple and fast XOR cipher for obfuscating data in memory for browser environments.
|
|
3
|
+
* This is not a substitute for strong cryptography but provides a lightweight way
|
|
4
|
+
* to prevent sensitive data from being stored in plaintext in memory.
|
|
5
|
+
*/
|
|
6
|
+
export declare class XorService {
|
|
7
|
+
/**
|
|
8
|
+
* Encrypts or decrypts data using a simple XOR cipher.
|
|
9
|
+
* The key is XORed with the data. The same function is used for
|
|
10
|
+
* both encryption and decryption.
|
|
11
|
+
*
|
|
12
|
+
* @param data The data to process.
|
|
13
|
+
* @param key The key to use for the XOR operation.
|
|
14
|
+
* @returns A new Uint8Array containing the result of the XOR operation.
|
|
15
|
+
*/
|
|
16
|
+
static xor(data: Uint8Array, key: Uint8Array): Uint8Array;
|
|
17
|
+
/**
|
|
18
|
+
* Generates a random key of a specified length.
|
|
19
|
+
* @param length The length of the key in bytes.
|
|
20
|
+
* @returns A Uint8Array containing the random key.
|
|
21
|
+
* @throws Error if crypto API is not available.
|
|
22
|
+
*/
|
|
23
|
+
static generateKey(length: number): Uint8Array;
|
|
24
|
+
/**
|
|
25
|
+
* Helper method to convert a string to a Uint8Array.
|
|
26
|
+
* @param str The string to convert.
|
|
27
|
+
* @returns A Uint8Array representation of the string.
|
|
28
|
+
*/
|
|
29
|
+
static stringToBytes(str: string): Uint8Array;
|
|
30
|
+
/**
|
|
31
|
+
* Helper method to convert a Uint8Array to a string.
|
|
32
|
+
* @param bytes The Uint8Array to convert.
|
|
33
|
+
* @returns A string representation of the Uint8Array.
|
|
34
|
+
*/
|
|
35
|
+
static bytesToString(bytes: Uint8Array): string;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=xor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xor.d.ts","sourceRoot":"","sources":["../src/xor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,qBAAa,UAAU;IACrB;;;;;;;;OAQG;WACW,GAAG,CAAC,IAAI,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,UAAU;IAQhE;;;;;OAKG;WACW,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU;IAgBrD;;;;OAIG;WACW,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU;IAKpD;;;;OAIG;WACW,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM;CAIvD"}
|
package/dist/xor.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A simple and fast XOR cipher for obfuscating data in memory for browser environments.
|
|
3
|
+
* This is not a substitute for strong cryptography but provides a lightweight way
|
|
4
|
+
* to prevent sensitive data from being stored in plaintext in memory.
|
|
5
|
+
*/
|
|
6
|
+
export class XorService {
|
|
7
|
+
/**
|
|
8
|
+
* Encrypts or decrypts data using a simple XOR cipher.
|
|
9
|
+
* The key is XORed with the data. The same function is used for
|
|
10
|
+
* both encryption and decryption.
|
|
11
|
+
*
|
|
12
|
+
* @param data The data to process.
|
|
13
|
+
* @param key The key to use for the XOR operation.
|
|
14
|
+
* @returns A new Uint8Array containing the result of the XOR operation.
|
|
15
|
+
*/
|
|
16
|
+
static xor(data, key) {
|
|
17
|
+
const result = new Uint8Array(data.length);
|
|
18
|
+
for (let i = 0; i < data.length; i++) {
|
|
19
|
+
result[i] = data[i] ^ key[i % key.length];
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Generates a random key of a specified length.
|
|
25
|
+
* @param length The length of the key in bytes.
|
|
26
|
+
* @returns A Uint8Array containing the random key.
|
|
27
|
+
* @throws Error if crypto API is not available.
|
|
28
|
+
*/
|
|
29
|
+
static generateKey(length) {
|
|
30
|
+
const randomBytes = new Uint8Array(length);
|
|
31
|
+
// Check if running in a browser with crypto support
|
|
32
|
+
if (typeof window !== 'undefined' && window.crypto) {
|
|
33
|
+
window.crypto.getRandomValues(randomBytes);
|
|
34
|
+
}
|
|
35
|
+
else if (typeof crypto !== 'undefined') {
|
|
36
|
+
// For environments where crypto is global (some newer JS runtimes)
|
|
37
|
+
crypto.getRandomValues(randomBytes);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
throw new Error('Crypto API not available in this environment');
|
|
41
|
+
}
|
|
42
|
+
return randomBytes;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Helper method to convert a string to a Uint8Array.
|
|
46
|
+
* @param str The string to convert.
|
|
47
|
+
* @returns A Uint8Array representation of the string.
|
|
48
|
+
*/
|
|
49
|
+
static stringToBytes(str) {
|
|
50
|
+
const encoder = new TextEncoder();
|
|
51
|
+
return encoder.encode(str);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Helper method to convert a Uint8Array to a string.
|
|
55
|
+
* @param bytes The Uint8Array to convert.
|
|
56
|
+
* @returns A string representation of the Uint8Array.
|
|
57
|
+
*/
|
|
58
|
+
static bytesToString(bytes) {
|
|
59
|
+
const decoder = new TextDecoder();
|
|
60
|
+
return decoder.decode(bytes);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=xor.js.map
|
package/dist/xor.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xor.js","sourceRoot":"","sources":["../src/xor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,OAAO,UAAU;IACrB;;;;;;;;OAQG;IACI,MAAM,CAAC,GAAG,CAAC,IAAgB,EAAE,GAAe;QACjD,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC3C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACrC,MAAM,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC;QAC5C,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,WAAW,CAAC,MAAc;QACtC,MAAM,WAAW,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;QAE3C,oDAAoD;QACpD,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;YACnD,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,WAAW,CAAC,CAAC;QAC7C,CAAC;aAAM,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;YACzC,mEAAmE;YACnE,MAAM,CAAC,eAAe,CAAC,WAAW,CAAC,CAAC;QACtC,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;QAClE,CAAC;QAED,OAAO,WAAW,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,aAAa,CAAC,GAAW;QACrC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;QAClC,OAAO,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,aAAa,CAAC,KAAiB;QAC3C,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;QAClC,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@digitaldefiance/ecies-lib",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Digital Defiance ECIES Library",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "yarn tsc",
|
|
9
|
+
"test": "yarn jest",
|
|
10
|
+
"lint": "eslint src/**/*.ts",
|
|
11
|
+
"lint:fix": "eslint src/**/*.ts --fix",
|
|
12
|
+
"prettier:check": "prettier --check 'src/**/*.{ts,tsx}'",
|
|
13
|
+
"prettier:fix": "prettier --write 'src/**/*.{ts,tsx}'",
|
|
14
|
+
"format": "yarn prettier:fix && yarn lint:fix",
|
|
15
|
+
"prepublishOnly": "yarn build",
|
|
16
|
+
"publish:public": "npm publish --access public"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@babel/core": "^7.28.4",
|
|
20
|
+
"@babel/preset-env": "^7.28.3",
|
|
21
|
+
"@babel/preset-typescript": "^7.27.1",
|
|
22
|
+
"@types/babel__core": "^7",
|
|
23
|
+
"@types/babel__preset-env": "^7",
|
|
24
|
+
"@types/jest": "^29.0.0",
|
|
25
|
+
"@typescript-eslint/eslint-plugin": "^8.31.1",
|
|
26
|
+
"@typescript-eslint/parser": "^8.31.1",
|
|
27
|
+
"babel-jest": "^30.1.2",
|
|
28
|
+
"eslint": "^9.8.0",
|
|
29
|
+
"eslint-config-prettier": "^10.1.2",
|
|
30
|
+
"eslint-plugin-import": "^2.32.0",
|
|
31
|
+
"eslint-plugin-prettier": "^5.3.1",
|
|
32
|
+
"jest": "^29.0.0",
|
|
33
|
+
"jest-util": "^30.0.5",
|
|
34
|
+
"prettier": "^2.6.2",
|
|
35
|
+
"prettier-plugin-organize-imports": "^4.1.0",
|
|
36
|
+
"ts-jest": "^29.0.0",
|
|
37
|
+
"typescript": "^5.9.2"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"README.md"
|
|
42
|
+
],
|
|
43
|
+
"keywords": [
|
|
44
|
+
"ecies",
|
|
45
|
+
"encryption",
|
|
46
|
+
"decryption",
|
|
47
|
+
"typescript"
|
|
48
|
+
],
|
|
49
|
+
"author": "Digital Defiance",
|
|
50
|
+
"license": "MIT",
|
|
51
|
+
"packageManager": "yarn@4.10.3",
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@ethereumjs/wallet": "^10.0.0",
|
|
54
|
+
"@noble/curves": "^2.0.1",
|
|
55
|
+
"@noble/hashes": "^2.0.1",
|
|
56
|
+
"@scure/bip32": "^2.0.0",
|
|
57
|
+
"bson": "^6.10.4",
|
|
58
|
+
"ts-brand": "^0.2.0"
|
|
59
|
+
}
|
|
60
|
+
}
|