-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.js
More file actions
135 lines (130 loc) · 4.25 KB
/
encrypt.js
File metadata and controls
135 lines (130 loc) · 4.25 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// encrypt files create report
async function encryptFileData(data, multiPublicKeys) {
const publicKeysArray = await Promise.all(multiPublicKeys.map(async (key) => {
const { keys } = await openpgp.key.readArmored(key);
if (keys && keys.length > 0) {
return keys[0];
} else {
throw new Error('Invalid PGP public key');
}
}));
try {
const { data: encryptedData } = await openpgp.encrypt({
message: openpgp.message.fromText(data),
publicKeys: publicKeysArray
});
return encryptedData;
} catch (error) {
console.error('Encryption error:', error);
throw error;
}
}
//encrypt text fields create Report
async function encryptReport(multiPublicKeys, simples) {
const publicKeysArray = await Promise.all(multiPublicKeys.map(async (key) => {
const { keys } = await openpgp.key.readArmored(key);
if (keys && keys.length > 0) {
return keys[0];
} else {
throw new Error('Invalid PGP public key');
}
}));
for (const e of simples) {
const markdownValue = e.codemirror.getValue();
if (markdownValue !== '') {
if (!isEncrypted(markdownValue)) {
try {
const { data: encryptedData } = await openpgp.encrypt({
message: openpgp.message.fromText(markdownValue),
publicKeys: publicKeysArray,
});
e.value(encryptedData);
} catch (error) {
console.error('Error encrypting:', error);
}
}
}
}
}
// decrypt files
async function decryptData(data, filePrivetKeyInput, filePrivetPass) {
try {
const { keys: [privateKeyObj] } = await openpgp.key.readArmored(filePrivetKeyInput);
if (filePrivetPass !== '') {
await privateKeyObj.decrypt(filePrivetPass);
}
const { data: decryptedData } = await openpgp.decrypt({
message: await openpgp.message.readArmored(data),
privateKeys: [privateKeyObj],
});
return decryptedData;
} catch (error) {
console.log(error.message)
throw error;
}
}
// encrypt report sharing
async function encryptData(data, publicKey) {
try {
const { keys: [publicKeyObj] } = await openpgp.key.readArmored(publicKey);
const { data: encryptedData } = await openpgp.encrypt({
message: openpgp.message.fromText(data),
publicKeys: publicKeyObj
});
return encryptedData;
} catch (error) {
console.error('Encryption error:', error);
throw error;
}
}
// Encrypt share report
async function encryptMessage(plainText, publicKeyArmored) {
try {
const {keys: [publicKey]} = await openpgp.key.readArmored(publicKeyArmored);
if (!publicKey) {
throw new Error("Invalid public key");
}
const encryptedMessage = await openpgp.encrypt({
message: openpgp.message.fromText(plainText),
publicKeys: publicKey,
});
return encryptedMessage.data;
} catch (error) {
throw new Error("Encryption failed: " + error.message);
}
}
// decrypt report
async function decryptMessage(encryptedMessage, privateKeyArmored, privateKeyPassphrase) {
try {
const privateKeyObj = await openpgp.key.readArmored(privateKeyArmored);
if (privateKeyPassphrase !== '') {
await privateKeyObj.keys[0].decrypt(privateKeyPassphrase);
}
const decryptedMessage = await openpgp.decrypt({
message: await openpgp.message.readArmored(encryptedMessage),
privateKeys: [privateKeyObj.keys[0]],
});
return decryptedMessage.data;
} catch (error) {
throw new Error('Decryption error:', error);
}
}
// decrypt MD report
async function decryptTextContents(textContents, privateKeyArmored, privateKeyPassphrase) {
try {
const privateKeyObj = await openpgp.key.readArmored(privateKeyArmored);
if (privateKeyPassphrase !== '') {
await privateKeyObj.keys[0].decrypt(privateKeyPassphrase);
}
const decryptedMessagesArray = await Promise.all(textContents.map(async (encryptedMessage) => {
const decryptedMessage = await openpgp.decrypt({
message: await openpgp.message.readArmored(encryptedMessage),
privateKeys: [privateKeyObj.keys[0]],
});
return decryptedMessage.data;
}));
return decryptedMessagesArray;
} catch (error) {
throw new Error('Decryption error:', error);
}
}