-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.password-utils.ts
117 lines (95 loc) · 3.14 KB
/
decrypt.password-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {
decodeDOMStringToArrayBuffer,
encodeArrayBufferToDOMString,
} from '@pashoo2/typed-array-utils';
import {
importPasswordKeyFromString,
generatePasswordKeyByPasswordString,
} from './derive-key.password-utils';
import {getInitializationVectorFromData} from '../encryption-utils/encrypt-data.encryption-utils';
import {decryptDataFromString} from '../encryption-utils/decrypt-data.encryption-utils';
import {PASSWORD_ENCRYPTION_UTILS_DECRYPTION_PARAMS} from './password-utils.const';
import {TSaltUtilsSaltType} from '../encryption-utils/salt-utils.types';
import {isCryptoKey} from '../encryption-keys-utils/encryption-keys-utils';
export const decryptDataWithKeyNative = async (
key: string | CryptoKey,
dataWithIv: ArrayBuffer
): Promise<ArrayBuffer | Error> => {
let cryptoKey;
if (isCryptoKey(key)) {
cryptoKey = key;
} else {
cryptoKey = await importPasswordKeyFromString(key);
}
if (cryptoKey instanceof Error) {
return cryptoKey;
}
const dataWithIvStructure = getInitializationVectorFromData(dataWithIv);
if (dataWithIvStructure instanceof Error) {
return dataWithIvStructure;
}
const {iv, data} = dataWithIvStructure;
return decryptDataFromString(cryptoKey, data, {
...PASSWORD_ENCRYPTION_UTILS_DECRYPTION_PARAMS,
iv,
});
};
export const decryptDataWithKey = async (
key: string | CryptoKey,
dataWithIv: string
): Promise<string | Error> => {
const dataWithIvArrayBuffer = decodeDOMStringToArrayBuffer(dataWithIv);
if (dataWithIvArrayBuffer instanceof Error) {
return dataWithIvArrayBuffer;
}
const decryptedArrayBuffer = await decryptDataWithKeyNative(
key,
dataWithIvArrayBuffer
);
if (decryptedArrayBuffer instanceof Error) {
return decryptedArrayBuffer;
}
return encodeArrayBufferToDOMString(decryptedArrayBuffer);
};
export const decryptDataWithKeyFromUint8Array = async (
key: string | CryptoKey,
dataWithIv: Uint8Array
): Promise<string | Error> => {
const decryptedArrayBuffer = await decryptDataWithKeyNative(key, dataWithIv);
if (decryptedArrayBuffer instanceof Error) {
return decryptedArrayBuffer;
}
return encodeArrayBufferToDOMString(decryptedArrayBuffer);
};
export const decryptDataArrayOrStringWithKeyToUInt8Array = async (
key: string | CryptoKey,
dataWithIv: Uint8Array | string
): Promise<Uint8Array | Error> => {
const dataWithIvArrayBuffer: ArrayBuffer | Error =
typeof dataWithIv === 'string'
? decodeDOMStringToArrayBuffer(dataWithIv)
: dataWithIv.buffer;
if (dataWithIvArrayBuffer instanceof Error) {
return dataWithIvArrayBuffer;
}
const decryptedArrayBuffer = await decryptDataWithKeyNative(
key,
dataWithIvArrayBuffer
);
if (decryptedArrayBuffer instanceof Error) {
return decryptedArrayBuffer;
}
return new Uint8Array(decryptedArrayBuffer);
};
export const decryptDataByPassword = async (
password: string,
salt: TSaltUtilsSaltType,
dataWithIv: string
): Promise<string | Error> => {
const key = await generatePasswordKeyByPasswordString(password, salt);
if (key instanceof Error) {
console.error(key);
return key;
}
return decryptDataWithKey(key, dataWithIv);
};