-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_encrypted.js
More file actions
91 lines (86 loc) · 3.17 KB
/
script_encrypted.js
File metadata and controls
91 lines (86 loc) · 3.17 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
let jsonFileContent = '';
function uploadEncryptedFile() {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.json';
input.onchange = async (event) => {
const file = event.target.files[0];
const password = prompt('Enter password to decrypt the file:');
const reader = new FileReader();
reader.onload = (e) => {
try {
const decrypted = CryptoJS.AES.decrypt(e.target.result, password).toString(CryptoJS.enc.Utf8);
jsonFileContent = JSON.parse(decrypted);
console.log('Decrypted JSON:', jsonFileContent);
} catch (error) {
alert('Invalid password or corrupted file.');
}
};
reader.readAsText(file);
};
input.click();
}
function uploadNonEncryptedFile() {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.json';
input.onchange = (event) => {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = (e) => {
// try {
jsonFileContent = JSON.parse(e.target.result);
console.log('Uploaded JSON:', jsonFileContent);
set_graph(jsonFileContent);
for (const nodeId in graph) {
const node = graph[nodeId];
node.tags.forEach(nodeTag => {
if(!searchSuggestions.includes(nodeTag)) {
searchSuggestions.push(nodeTag);
}
});
}
drawGraph();
// } catch (error) {
// alert('Invalid JSON file.');
// }
};
reader.readAsText(file);
};
input.click();
}
function saveFile() {
const password = prompt('Enter password to encrypt the file:');
const encrypted = CryptoJS.AES.encrypt(JSON.stringify(jsonFileContent), password).toString();
const blob = new Blob([encrypted], { type: 'text/plain' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'encrypted.json';
link.click();
}
function saveFileUnencrypted() {
const blob = new Blob([JSON.stringify(jsonFileContent, null, 4)], { type: 'application/json' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'unencrypted.json';
link.click();
}
function uploadEncryptedFileFromURL() {
const url = prompt('Enter the URL of the encrypted JSON file:');
const password = prompt('Enter password to decrypt the file:');
fetch(url)
.then(response => response.text())
.then(data => {
try {
const decrypted = CryptoJS.AES.decrypt(data, password).toString(CryptoJS.enc.Utf8);
jsonFileContent = JSON.parse(decrypted);
console.log('Decrypted JSON from URL:', jsonFileContent);
} catch (error) {
alert('Invalid password or corrupted file.');
}
})
.catch(error => {
alert('Failed to fetch the file from the URL.');
console.error('Error:', error);
});
}