-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
501 lines (438 loc) · 16.1 KB
/
script.js
File metadata and controls
501 lines (438 loc) · 16.1 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
let encryptedBlob = null;
let currentFileMeta = null;
let selectedFile = null;
let currentLang = "it";
const MAX_FILE_SIZE_BYTES = 150 * 1024 * 1024;
let fileTooLarge = false;
const I18N = {
it: {
meta: {
title: "ZeroCrypt – Cripta file, testo e link in modo sicuro",
description:
"Cripta file, testo e link direttamente nel tuo browser con ZeroCrypt. Crittografia client-side, zero-knowledge, nessun upload."
},
header: { tagline: "Privacy First — Zero Knowledge Encryption" },
drop: {
title: "Carica un file",
hint: "Trascina qui o clicca per sfogliare",
support: "Supporta file, testo e link",
fileReady: "File pronto per la crittografia",
maxSize: "Consigliato ≤ 150MB"
},
input: { placeholder: "Incolla testo o link..." },
password: {
placeholder: "Inserisci password",
show: "Mostra",
hide: "Nascondi"
},
output: { placeholder: "Output..." },
actions: {
encrypt: "Cripta",
decrypt: "Decripta",
import: "Importa .tiv",
copy: "Copia",
download: "Download"
},
badge: { local: "LOCAL ENCRYPTION" },
about: {
title: "Cos’è ZeroCrypt?",
text:
"ZeroCrypt è uno strumento di crittografia che funziona direttamente nel tuo browser. I dati vengono criptati localmente prima di essere condivisi, garantendo il massimo livello di privacy."
},
how: {
title: "Come funziona",
step1: "Inserisci testo o carica un file",
step2: "Inserisci una password",
step3: "Clicca su Cripta",
step4: "Scarica o copia il risultato",
step5: "Condividi in sicurezza"
},
features: {
encryption: {
title: "CRITTOGRAFIA E2EE",
desc: "AES client-side. Nessun dato lascia il dispositivo."
},
zk: { title: "ZERO-KNOWLEDGE", desc: "Nessun accesso lato server." },
file: { title: "SUPPORTO FILE", desc: "Compatibile con tutti i file." },
link: { title: "PROTEZIONE LINK", desc: "Proteggi URL sensibili." }
},
faq: {
title: "FAQ",
q1: { q: "È sicuro?", a: "Sì, utilizza crittografia moderna lato browser." },
q2: { q: "I dati vengono salvati?", a: "No, tutto avviene localmente." },
q3: { q: "Posso recuperare la password?", a: "No, per motivi di sicurezza." }
},
glossary: {
title: "Glossario",
aes: "algoritmo di crittografia avanzato.",
zk: "il server non conosce i dati.",
client: "elaborazione nel browser."
},
cta: { text: "Per mantenere lo stesso livello di sicurezza, condividi il file o testo appena crittografato con <a href='https://tivustream.com/zerodrop/' target='_blank'>ZeroDrop.</a>" },
footer: { home: "HOME", privacy: "PRIVACY", tos: "TOS", contacts: "CONTACTS" },
status: {
encryptedSuccess: "✔ Criptato con successo",
decryptedSuccess: "✔ Decriptazione riuscita",
decryptError: "❌ Password errata o file corrotto",
importedSuccess: "✔ File .tiv importato. Inserisci la password e clicca Decripta."
},
alerts: {
missingPassword: "Inserisci password",
missingDecryptPassword: "Password mancante. Inseriscila per decriptare.",
missingEncryptedData: "Sequenza criptata mancante. Incolla l'output oppure importa un file .tiv.",
missingDecryptBoth: "Dati mancanti: password e sequenza criptata. Inserisci la password e incolla l'output oppure importa un file .tiv.",
missingData: "Dati mancanti",
noFile: "Nessun file",
fileTooLarge: "File troppo grande: consigliato ≤ 150MB per evitare problemi di memoria nel browser.",
invalidTiv: "Seleziona un file .tiv valido."
}
},
en: {
meta: {
title: "ZeroCrypt – Encrypt files, text and links securely",
description:
"Encrypt files, text and links directly in your browser with ZeroCrypt. Client-side encryption, zero-knowledge, no upload."
},
header: { tagline: "Privacy First — Zero Knowledge Encryption" },
drop: {
title: "Upload a file",
hint: "Drop here or click to browse",
support: "Supports files, text and links",
fileReady: "File ready for encryption",
maxSize: "Recommended ≤ 150MB"
},
input: { placeholder: "Paste text or link..." },
password: {
placeholder: "Enter password",
show: "Show",
hide: "Hide"
},
output: { placeholder: "Output..." },
actions: {
encrypt: "Encrypt",
decrypt: "Decrypt",
import: "Import .tiv",
copy: "Copy",
download: "Download"
},
badge: { local: "LOCAL ENCRYPTION" },
about: {
title: "What is ZeroCrypt?",
text:
"ZeroCrypt is an encryption tool that runs directly in your browser. Data is encrypted locally before sharing, ensuring maximum privacy."
},
how: {
title: "How it works",
step1: "Paste text or choose a file",
step2: "Enter a password",
step3: "Click Encrypt",
step4: "Download or copy the result",
step5: "Share safely"
},
features: {
encryption: {
title: "E2EE ENCRYPTION",
desc: "Client-side AES. No data leaves your device."
},
zk: { title: "ZERO-KNOWLEDGE", desc: "No server-side access." },
file: { title: "FILE SUPPORT", desc: "Works with any file type." },
link: { title: "LINK PROTECTION", desc: "Protect sensitive URLs." }
},
faq: {
title: "FAQ",
q1: { q: "Is it safe?", a: "Yes, it uses modern browser-based encryption." },
q2: { q: "Is data stored?", a: "No, everything happens locally." },
q3: { q: "Can I recover the password?", a: "No, for security reasons." }
},
glossary: {
title: "Glossary",
aes: "advanced encryption algorithm.",
zk: "the server does not know the data.",
client: "processing inside the browser."
},
cta: { text: "To maintain the same level of security, share the newly encrypted file or text with <a href='https://tivustream.com/zerodrop/' target='_blank'>ZeroDrop.</a>" },
footer: { home: "HOME", privacy: "PRIVACY", tos: "TOS", contacts: "CONTACTS" },
status: {
encryptedSuccess: "✔ Encrypted successfully",
decryptedSuccess: "✔ Decryption successful",
decryptError: "❌ Wrong password or corrupted data",
importedSuccess: "✔ .tiv imported. Enter the password and click Decrypt."
},
alerts: {
missingPassword: "Enter a password",
missingDecryptPassword: "Missing password. Enter it to decrypt.",
missingEncryptedData: "Missing encrypted data. Paste the output or import a .tiv file.",
missingDecryptBoth: "Missing data: password and encrypted data. Enter the password and paste the output or import a .tiv file.",
missingData: "Missing data",
noFile: "No file",
fileTooLarge: "File is too large: recommended ≤ 150MB to avoid browser memory issues.",
invalidTiv: "Please select a valid .tiv file."
}
}
};
const dropArea = document.getElementById("dropArea");
const fileInput = document.getElementById("fileInput");
const tivInput = document.getElementById("tivInput");
const langButtons = document.querySelectorAll(".lang-switch [data-lang]");
function t(key) {
const langTable = I18N[currentLang] || I18N.it;
const parts = key.split(".");
let value = langTable;
for (const part of parts) value = value && Object.prototype.hasOwnProperty.call(value, part) ? value[part] : undefined;
if (typeof value === "string") return value;
return key;
}
function applyI18n() {
document.documentElement.setAttribute("lang", currentLang);
const title = t("meta.title");
if (title) document.title = title;
const metaDesc = document.querySelector('meta[name="description"]');
if (metaDesc) metaDesc.setAttribute("content", t("meta.description"));
document.querySelectorAll("[data-i18n]").forEach(el => {
const key = el.getAttribute("data-i18n");
if (!key) return;
el.innerHTML = t(key);
});
document.querySelectorAll("[data-i18n-placeholder]").forEach(el => {
const key = el.getAttribute("data-i18n-placeholder");
if (!key) return;
el.setAttribute("placeholder", t(key));
});
syncPasswordToggleLabel();
updateDropUI();
}
function setLang(lang) {
currentLang = lang === "en" ? "en" : "it";
try {
localStorage.setItem("zerocrypt.lang", currentLang);
} catch {}
applyI18n();
syncLangButtons();
}
function syncLangButtons() {
if (!langButtons || langButtons.length === 0) return;
langButtons.forEach(btn => {
const lang = btn.getAttribute("data-lang");
const isActive = lang === currentLang;
btn.classList.toggle("active", isActive);
btn.setAttribute("aria-pressed", isActive ? "true" : "false");
});
}
function syncPasswordToggleLabel() {
const p = document.getElementById("password");
const btn = document.getElementById("btnTogglePassword");
if (!p || !btn) return;
btn.textContent = p.type === "password" ? t("password.show") : t("password.hide");
}
dropArea.addEventListener("click", () => fileInput.click());
dropArea.addEventListener("dragover", e => {
e.preventDefault();
dropArea.classList.add("active");
});
dropArea.addEventListener("dragleave", () => {
dropArea.classList.remove("active");
});
dropArea.addEventListener("drop", e => {
e.preventDefault();
selectedFile = e.dataTransfer.files && e.dataTransfer.files[0] ? e.dataTransfer.files[0] : null;
if (selectedFile) {
const dt = new DataTransfer();
dt.items.add(selectedFile);
fileInput.files = dt.files;
}
updateDropUI();
dropArea.classList.remove("active");
});
fileInput.addEventListener("change", () => {
selectedFile = fileInput.files && fileInput.files[0] ? fileInput.files[0] : null;
updateDropUI();
});
function togglePassword() {
const p = document.getElementById("password");
const next = p.type === "password" ? "text" : "password";
p.type = next;
syncPasswordToggleLabel();
}
async function getKey(password, salt) {
const enc = new TextEncoder();
const baseKey = await crypto.subtle.importKey(
"raw",
enc.encode(password),
{ name: "PBKDF2" },
false,
["deriveKey"]
);
return crypto.subtle.deriveKey(
{
name: "PBKDF2",
salt,
iterations: 100000,
hash: "SHA-256"
},
baseKey,
{ name: "AES-GCM", length: 256 },
false,
["encrypt", "decrypt"]
);
}
async function encryptData() {
const text = document.getElementById("inputText").value;
const file = selectedFile || (fileInput.files && fileInput.files[0] ? fileInput.files[0] : null);
const password = document.getElementById("password").value;
if (!password) return alert(t("alerts.missingPassword"));
if (file && fileTooLarge) return alert(t("alerts.fileTooLarge"));
setStatus("");
const salt = crypto.getRandomValues(new Uint8Array(16));
const iv = crypto.getRandomValues(new Uint8Array(12));
const key = await getKey(password, salt);
let payload;
if (file) {
const buffer = await file.arrayBuffer();
payload = {
name: file.name,
type: file.type,
data: Array.from(new Uint8Array(buffer))
};
currentFileMeta = file.name;
} else {
payload = {
type: "text",
data: text
};
}
const encoded = new TextEncoder().encode(JSON.stringify(payload));
const encrypted = await crypto.subtle.encrypt(
{ name: "AES-GCM", iv },
key,
encoded
);
const combined = new Uint8Array([
...salt,
...iv,
...new Uint8Array(encrypted)
]);
encryptedBlob = new Blob([combined]);
document.getElementById("outputText").value =
btoa(String.fromCharCode(...combined));
setStatus(t("status.encryptedSuccess"));
}
async function decryptData() {
const password = document.getElementById("password").value;
const input = document.getElementById("outputText").value;
if (!password && !input) return alert(t("alerts.missingDecryptBoth"));
if (!password) return alert(t("alerts.missingDecryptPassword"));
if (!input) return alert(t("alerts.missingEncryptedData"));
setStatus("");
const data = Uint8Array.from(atob(input), c => c.charCodeAt(0));
const salt = data.slice(0, 16);
const iv = data.slice(16, 28);
const encrypted = data.slice(28);
const key = await getKey(password, salt);
try {
const decrypted = await crypto.subtle.decrypt(
{ name: "AES-GCM", iv },
key,
encrypted
);
const decoded = JSON.parse(new TextDecoder().decode(decrypted));
if (decoded.type === "text") {
document.getElementById("outputText").value = decoded.data;
} else {
const blob = new Blob([new Uint8Array(decoded.data)], { type: decoded.type });
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = decoded.name || "file";
a.click();
}
setStatus(t("status.decryptedSuccess"));
} catch {
setStatus(t("status.decryptError"));
}
}
function copyOutput() {
const out = document.getElementById("outputText");
out.select();
document.execCommand("copy");
}
function importTiv() {
if (!tivInput) return;
tivInput.value = "";
tivInput.click();
}
function readFileAsBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onerror = () => reject(reader.error || new Error("Read failed"));
reader.onload = () => {
const result = reader.result;
if (typeof result !== "string") return reject(new Error("Unexpected result"));
const commaIndex = result.indexOf(",");
resolve(commaIndex >= 0 ? result.slice(commaIndex + 1) : result);
};
reader.readAsDataURL(file);
});
}
function sanitizeFileName(name) {
const cleaned = String(name || "")
.replace(/[\/\\]/g, "-")
.replace(/[\u0000-\u001F\u007F]/g, "")
.trim();
return cleaned || "encrypted";
}
function downloadFile() {
if (!encryptedBlob) return alert(t("alerts.noFile"));
const baseName = currentFileMeta ? sanitizeFileName(currentFileMeta) : "encrypted";
const downloadName = baseName.toLowerCase().endsWith(".tiv") ? baseName : `${baseName}.tiv`;
const a = document.createElement("a");
a.href = URL.createObjectURL(encryptedBlob);
a.download = downloadName;
a.click();
}
function updateDropUI() {
const dropText = document.getElementById("dropText");
const dropSub = document.getElementById("dropSub");
const encryptBtn = document.getElementById("btnEncrypt");
if (!dropText || !dropSub) return;
if (selectedFile) {
fileTooLarge = selectedFile.size > MAX_FILE_SIZE_BYTES;
if (encryptBtn) encryptBtn.disabled = fileTooLarge;
dropText.innerText = selectedFile.name;
dropSub.innerText = fileTooLarge ? t("alerts.fileTooLarge") : t("drop.fileReady");
return;
}
fileTooLarge = false;
if (encryptBtn) encryptBtn.disabled = false;
dropText.innerText = t("drop.title");
dropSub.innerText = t("drop.hint");
}
function setStatus(message) {
const el = document.getElementById("status");
if (!el) return;
el.innerText = message;
}
if (langButtons && langButtons.length > 0) {
langButtons.forEach(btn => {
btn.addEventListener("click", () => setLang(btn.getAttribute("data-lang")));
});
}
if (tivInput) {
tivInput.addEventListener("change", async () => {
const file = tivInput.files && tivInput.files[0] ? tivInput.files[0] : null;
if (!file) return;
const isTiv = file.name && file.name.toLowerCase().endsWith(".tiv");
if (!isTiv) return alert(t("alerts.invalidTiv"));
try {
const base64 = await readFileAsBase64(file);
const output = document.getElementById("outputText");
if (output) output.value = base64;
setStatus(t("status.importedSuccess"));
} catch {
alert(t("alerts.invalidTiv"));
}
});
}
let savedLang = null;
try {
savedLang = localStorage.getItem("zerocrypt.lang");
} catch {}
setLang(savedLang === "en" ? "en" : "it");