Add Two-Factor Authentication (2FA) support and key management features
- Implemented 2FA management in admin panel with enable/disable options. - Added QR code display for 2FA setup and input for TOTP codes in login and pickup forms. - Introduced key management section for generating, loading, and clearing RSA key pairs. - Enhanced file upload and sharing functionality with optional 2FA. - Added buttons for switching between development and production modes in admin panel. - Updated API documentation to reflect new 2FA and key management features.
This commit is contained in:
@@ -1,119 +0,0 @@
|
||||
/**
|
||||
* Encryption module.
|
||||
* Handles cryptographic operations using Web Crypto API.
|
||||
* Implements AES-GCM encryption with PBKDF2 key derivation.
|
||||
*/
|
||||
|
||||
// ===== Constants =====
|
||||
const SALT_LENGTH = 16;
|
||||
const IV_LENGTH = 12;
|
||||
const PBKDF2_ITERATIONS = 200_000;
|
||||
const KEY_LENGTH = 256;
|
||||
|
||||
// ===== Binary-safe Base64 Helpers =====
|
||||
function base64Encode(buffer) {
|
||||
const binary = Array.from(new Uint8Array(buffer))
|
||||
.map(byte => String.fromCharCode(byte))
|
||||
.join('');
|
||||
return btoa(binary);
|
||||
}
|
||||
|
||||
function base64Decode(b64str) {
|
||||
const binary = atob(b64str);
|
||||
const bytes = new Uint8Array(binary.length);
|
||||
for (let i = 0; i < binary.length; i++) {
|
||||
bytes[i] = binary.charCodeAt(i);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
// ===== Key Derivation =====
|
||||
/**
|
||||
* Derives an AES-GCM key from a password using PBKDF2.
|
||||
* @param {string} password - User-supplied password.
|
||||
* @param {Uint8Array} salt - Randomly generated salt.
|
||||
* @returns {Promise<CryptoKey>} - Derived cryptographic key.
|
||||
*/
|
||||
export async function deriveKey(password, salt) {
|
||||
const encoder = new TextEncoder();
|
||||
const keyMaterial = await crypto.subtle.importKey(
|
||||
'raw',
|
||||
encoder.encode(password),
|
||||
{ name: 'PBKDF2' },
|
||||
false,
|
||||
['deriveKey']
|
||||
);
|
||||
|
||||
return crypto.subtle.deriveKey(
|
||||
{
|
||||
name: 'PBKDF2',
|
||||
salt,
|
||||
iterations: PBKDF2_ITERATIONS,
|
||||
hash: 'SHA-256'
|
||||
},
|
||||
keyMaterial,
|
||||
{ name: 'AES-GCM', length: KEY_LENGTH },
|
||||
false,
|
||||
['encrypt', 'decrypt']
|
||||
);
|
||||
}
|
||||
|
||||
// ===== Encryption =====
|
||||
/**
|
||||
* Encrypts a message using AES-GCM with a derived key.
|
||||
* @param {string} message - Plaintext message to encrypt.
|
||||
* @param {string} password - User password for key derivation.
|
||||
* @returns {Promise<string>} - Base64-encoded encrypted string.
|
||||
*/
|
||||
export async function encryptAdvanced(message, password) {
|
||||
const encoder = new TextEncoder();
|
||||
const salt = crypto.getRandomValues(new Uint8Array(SALT_LENGTH));
|
||||
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
||||
const key = await deriveKey(password, salt);
|
||||
const encoded = encoder.encode(message);
|
||||
|
||||
const ciphertext = await crypto.subtle.encrypt(
|
||||
{ name: 'AES-GCM', iv },
|
||||
key,
|
||||
encoded
|
||||
);
|
||||
|
||||
const output = new Uint8Array(salt.length + iv.length + ciphertext.byteLength);
|
||||
output.set(salt);
|
||||
output.set(iv, salt.length);
|
||||
output.set(new Uint8Array(ciphertext), salt.length + iv.length);
|
||||
|
||||
return base64Encode(output.buffer);
|
||||
}
|
||||
|
||||
// ===== Decryption =====
|
||||
/**
|
||||
* Decrypts an AES-GCM encrypted string.
|
||||
* @param {string} encryptedData - Base64-encoded ciphertext.
|
||||
* @param {string} password - Password used to derive the decryption key.
|
||||
* @returns {Promise<string>} - Decrypted plaintext.
|
||||
*/
|
||||
export async function decryptAdvanced(encryptedData, password) {
|
||||
const encrypted = base64Decode(encryptedData);
|
||||
|
||||
const salt = encrypted.slice(0, SALT_LENGTH);
|
||||
const iv = encrypted.slice(SALT_LENGTH, SALT_LENGTH + IV_LENGTH);
|
||||
const ciphertext = encrypted.slice(SALT_LENGTH + IV_LENGTH);
|
||||
const key = await deriveKey(password, salt);
|
||||
|
||||
const decrypted = await crypto.subtle.decrypt(
|
||||
{ name: 'AES-GCM', iv },
|
||||
key,
|
||||
ciphertext
|
||||
);
|
||||
|
||||
return new TextDecoder().decode(decrypted);
|
||||
}
|
||||
|
||||
// ===== Module Initialization =====
|
||||
/**
|
||||
* Initializes the encryption module and logs its status.
|
||||
*/
|
||||
export function setupEncryption() {
|
||||
console.log('[Encryption] Module loaded');
|
||||
}
|
||||
+48
-120
@@ -1,39 +1,38 @@
|
||||
import { deriveKey } from "./encryption.js"; // assuming shared deriveKey()
|
||||
|
||||
const SALT_LENGTH = 16;
|
||||
const IV_LENGTH = 12;
|
||||
const KEY_LENGTH = 256;
|
||||
/**
|
||||
* File operations using the new Python backend APIs
|
||||
*/
|
||||
|
||||
/**
|
||||
* Encrypts a full file and downloads the encrypted version.
|
||||
* Encrypts a full file using the backend API and downloads the encrypted version.
|
||||
*/
|
||||
export async function encryptFile(fileInput, password) {
|
||||
const file = fileInput.files[0];
|
||||
if (!file) return;
|
||||
|
||||
const algorithm = document.getElementById("algorithm")?.value || "aes_cbc";
|
||||
|
||||
try {
|
||||
const salt = crypto.getRandomValues(new Uint8Array(SALT_LENGTH));
|
||||
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
||||
const key = await deriveKey(password, salt);
|
||||
const fileBuffer = new Uint8Array(await file.arrayBuffer());
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('enc_password', password);
|
||||
formData.append('algorithm', algorithm);
|
||||
|
||||
const ciphertext = await crypto.subtle.encrypt(
|
||||
{ name: "AES-GCM", iv },
|
||||
key,
|
||||
fileBuffer
|
||||
);
|
||||
const response = await fetch('/api/encrypt', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
|
||||
const ctBytes = new Uint8Array(ciphertext);
|
||||
const result = new Uint8Array(salt.length + iv.length + ctBytes.length);
|
||||
result.set(salt);
|
||||
result.set(iv, salt.length);
|
||||
result.set(ctBytes, salt.length + iv.length);
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
throw new Error(errorData.error || `HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const blob = new Blob([result], { type: "application/octet-stream" });
|
||||
// Download the encrypted file
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = file.name + ".encrypted";
|
||||
a.download = `${file.name}.${algorithm}.encrypted`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
@@ -43,28 +42,43 @@ export async function encryptFile(fileInput, password) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts a file using the backend API and downloads the decrypted version.
|
||||
*/
|
||||
export async function decryptFile(fileInput, password) {
|
||||
const file = fileInput.files[0];
|
||||
if (!file) return;
|
||||
|
||||
try {
|
||||
const data = new Uint8Array(await file.arrayBuffer());
|
||||
const salt = data.slice(0, SALT_LENGTH);
|
||||
const iv = data.slice(SALT_LENGTH, SALT_LENGTH + IV_LENGTH);
|
||||
const ciphertext = data.slice(SALT_LENGTH + IV_LENGTH);
|
||||
const key = await deriveKey(password, salt);
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('enc_password', password);
|
||||
|
||||
const decrypted = await crypto.subtle.decrypt(
|
||||
{ name: "AES-GCM", iv },
|
||||
key,
|
||||
ciphertext
|
||||
);
|
||||
const response = await fetch('/api/decrypt', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
|
||||
const blob = new Blob([decrypted], { type: "application/octet-stream" });
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
throw new Error(errorData.error || `HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
// Download the decrypted file
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = file.name.replace(".encrypted", "");
|
||||
|
||||
// Clean up filename - remove algorithm-specific extensions
|
||||
let filename = file.name;
|
||||
const algorithms = ["aes_cbc", "aes_gcm", "xchacha", "rsa_hybrid"];
|
||||
for (const algo of algorithms) {
|
||||
filename = filename.replace(`.${algo}.encrypted`, "");
|
||||
}
|
||||
filename = filename.replace(".encrypted", "");
|
||||
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
@@ -74,89 +88,3 @@ export async function decryptFile(fileInput, password) {
|
||||
}
|
||||
}
|
||||
|
||||
// ===== File Processing =====
|
||||
async function processFile(file, password, isEncrypt) {
|
||||
const chunks = [];
|
||||
const totalChunks = Math.ceil(file.size / CHUNK_SIZE);
|
||||
let processedChunks = 0;
|
||||
|
||||
for (let start = 0; start < file.size; start += CHUNK_SIZE) {
|
||||
const chunk = file.slice(start, start + CHUNK_SIZE);
|
||||
const arrayBuffer = await chunk.arrayBuffer();
|
||||
const uint8Array = new Uint8Array(arrayBuffer);
|
||||
|
||||
const processedChunk = await processChunk(uint8Array, password, isEncrypt);
|
||||
chunks.push(processedChunk);
|
||||
|
||||
processedChunks++;
|
||||
updateProgress(processedChunks, totalChunks);
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
async function processChunk(data, password, isEncrypt) {
|
||||
const payload = {
|
||||
"encryption-type": "advanced",
|
||||
operation: isEncrypt ? "encrypt" : "decrypt",
|
||||
message: Array.from(data).join(','),
|
||||
password: password
|
||||
};
|
||||
|
||||
const response = await fetch("/", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
return new Uint8Array(result.result.split(',').map(Number));
|
||||
}
|
||||
|
||||
// ===== File Download =====
|
||||
function downloadEncryptedFile(chunks, originalName) {
|
||||
const blob = new Blob(chunks, { type: 'application/octet-stream' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = originalName + '.encrypted';
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
function downloadDecryptedFile(chunks, originalName) {
|
||||
const blob = new Blob(chunks, { type: 'application/octet-stream' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = originalName.replace('.encrypted', '');
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
// ===== Progress Tracking =====
|
||||
function updateProgress(processed, total) {
|
||||
const progressBar = document.getElementById("file-progress");
|
||||
const progressText = document.getElementById("file-progress-text");
|
||||
|
||||
if (progressBar && progressText) {
|
||||
const percent = Math.round((processed / total) * 100);
|
||||
progressBar.style.width = percent + "%";
|
||||
progressText.textContent = `Processing: ${percent}%`;
|
||||
|
||||
if (processed === total) {
|
||||
setTimeout(() => {
|
||||
progressBar.style.width = "0%";
|
||||
progressText.textContent = "";
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+296
-47
@@ -14,9 +14,9 @@ export function setupUI() {
|
||||
initializeEventListeners();
|
||||
}
|
||||
|
||||
function initializeEventListeners() {
|
||||
async function initializeEventListeners() {
|
||||
const elements = {
|
||||
encryptionType: document.getElementById("encryption-type"),
|
||||
algorithm: document.getElementById("algorithm"),
|
||||
inputText: document.getElementById("input-text"),
|
||||
form: document.getElementById("crypto-form"),
|
||||
removeFileBtn: document.getElementById("remove-file-btn"),
|
||||
@@ -26,22 +26,30 @@ function initializeEventListeners() {
|
||||
copyOutputBtn: document.getElementById("copy-output-btn"),
|
||||
toggleSwitch: document.getElementById("operation-toggle"),
|
||||
copyShareBtn: document.getElementById("copy-share-btn"),
|
||||
shareLink: document.getElementById("share-link")
|
||||
shareLink: document.getElementById("share-link"),
|
||||
generateKeypairBtn: document.getElementById("generate-keypair-btn"),
|
||||
loadPublicKeyBtn: document.getElementById("load-public-key-btn"),
|
||||
loadPrivateKeyBtn: document.getElementById("load-private-key-btn"),
|
||||
publicKeyFile: document.getElementById("public-key-file"),
|
||||
privateKeyFile: document.getElementById("private-key-file")
|
||||
};
|
||||
|
||||
if (validateElements(elements)) {
|
||||
setupElementListeners(elements);
|
||||
}
|
||||
await loadAvailableAlgorithms();
|
||||
// Initialize algorithm options on page load after algorithms are loaded
|
||||
toggleAlgorithmOptions();
|
||||
}
|
||||
|
||||
function validateElements(elements) {
|
||||
return elements.encryptionType && elements.inputText && elements.form &&
|
||||
return elements.algorithm && elements.inputText && elements.form &&
|
||||
elements.removeFileBtn && elements.clearAllBtn && elements.generateBtn &&
|
||||
elements.copyPasswordBtn && elements.toggleSwitch;
|
||||
}
|
||||
|
||||
function setupElementListeners(elements) {
|
||||
elements.encryptionType.addEventListener("change", toggleEncryptionOptions);
|
||||
elements.algorithm?.addEventListener("change", toggleAlgorithmOptions);
|
||||
elements.inputText.addEventListener("input", handleInputChange);
|
||||
elements.form.addEventListener("submit", handleSubmit);
|
||||
elements.removeFileBtn.addEventListener("click", removeFile);
|
||||
@@ -53,6 +61,13 @@ function setupElementListeners(elements) {
|
||||
console.log("Mode:", elements.toggleSwitch.checked ? "Decrypt" : "Encrypt");
|
||||
});
|
||||
|
||||
// Key pair management listeners
|
||||
elements.generateKeypairBtn?.addEventListener("click", generateAndDownloadKeyPair);
|
||||
elements.loadPublicKeyBtn?.addEventListener("click", () => elements.publicKeyFile?.click());
|
||||
elements.loadPrivateKeyBtn?.addEventListener("click", () => elements.privateKeyFile?.click());
|
||||
elements.publicKeyFile?.addEventListener("change", handlePublicKeyLoad);
|
||||
elements.privateKeyFile?.addEventListener("change", handlePrivateKeyLoad);
|
||||
|
||||
const fileInput = document.getElementById("file-input");
|
||||
if (fileInput) {
|
||||
fileInput.addEventListener("change", () => {
|
||||
@@ -87,40 +102,10 @@ function setupShareLinkListeners(elements) {
|
||||
}
|
||||
}
|
||||
|
||||
function toggleEncryptionOptions() {
|
||||
const type = document.getElementById("encryption-type").value.trim().toLowerCase();
|
||||
const passwordInputWrapper = document.getElementById("password-input");
|
||||
const fileSection = document.querySelector("#encoding-section #file-section");
|
||||
const isAdvanced = type.includes("advanced");
|
||||
|
||||
if (passwordInputWrapper) {
|
||||
passwordInputWrapper.classList.toggle("hidden", !isAdvanced);
|
||||
}
|
||||
|
||||
if (fileSection) {
|
||||
fileSection.classList.toggle("hidden", !isAdvanced);
|
||||
}
|
||||
|
||||
updateToggleLabels();
|
||||
toggleInputMode();
|
||||
}
|
||||
|
||||
function updateToggleLabels() {
|
||||
const type = document.getElementById("encryption-type")?.value;
|
||||
const leftLabel = document.getElementById("toggle-left-label");
|
||||
const rightLabel = document.getElementById("toggle-right-label");
|
||||
|
||||
if (!type || !leftLabel || !rightLabel) return;
|
||||
|
||||
const isAdvanced = type.toLowerCase().includes("advanced");
|
||||
leftLabel.textContent = isAdvanced ? "Encrypt" : "Encode";
|
||||
rightLabel.textContent = isAdvanced ? "Decrypt" : "Decode";
|
||||
}
|
||||
|
||||
function toggleInputMode() {
|
||||
const fileInput = document.getElementById("file-input");
|
||||
const textValue = document.getElementById("input-text")?.value.trim();
|
||||
const isAdvanced = document.getElementById("encryption-type")?.value === "advanced";
|
||||
|
||||
const textSection = document.getElementById("text-section");
|
||||
const fileSection = document.getElementById("file-section");
|
||||
@@ -131,23 +116,39 @@ function toggleInputMode() {
|
||||
const fileSelected = fileInput.files.length > 0;
|
||||
|
||||
textSection.style.display = fileSelected ? "none" : "flex";
|
||||
fileSection.style.display = (isAdvanced && !textValue) ? "flex" : "none";
|
||||
fileSection.style.display = !textValue ? "flex" : "none";
|
||||
removeBtn.style.display = fileSelected ? "inline-block" : "none";
|
||||
}
|
||||
|
||||
async function handleSubmit(event) {
|
||||
event.preventDefault();
|
||||
|
||||
const encryptionType = document.getElementById("encryption-type")?.value;
|
||||
const algorithm = document.getElementById("algorithm")?.value;
|
||||
const password = document.getElementById("password")?.value;
|
||||
const fileInput = document.getElementById("file-input");
|
||||
const isDecrypt = document.getElementById("operation-toggle").checked;
|
||||
const operation = isDecrypt ? "decrypt" : "encrypt";
|
||||
|
||||
if (!encryptionType || !fileInput) return;
|
||||
if (!algorithm || !fileInput) return;
|
||||
|
||||
if (encryptionType === "advanced" && !password) {
|
||||
return alert("Password is required for advanced encryption.");
|
||||
// Check requirements based on algorithm
|
||||
let requiresKeypair = false;
|
||||
if (window.availableAlgorithms && window.availableAlgorithms[algorithm]) {
|
||||
requiresKeypair = window.availableAlgorithms[algorithm].requires_keypair || false;
|
||||
} else {
|
||||
requiresKeypair = algorithm.includes("hybrid");
|
||||
}
|
||||
|
||||
if (requiresKeypair) {
|
||||
const globalKeys = window.getGlobalKeys ? window.getGlobalKeys() : {};
|
||||
if (operation === "encrypt" && !globalKeys.publicKey) {
|
||||
return alert("Please load a public key in the Key Pairs Management section for encryption with this algorithm.");
|
||||
}
|
||||
if (operation === "decrypt" && !globalKeys.privateKey) {
|
||||
return alert("Please load a private key in the Key Pairs Management section for decryption with this algorithm.");
|
||||
}
|
||||
} else if (!password) {
|
||||
return alert("Password is required for this algorithm.");
|
||||
}
|
||||
|
||||
if (fileInput.files.length > 0) {
|
||||
@@ -156,19 +157,39 @@ async function handleSubmit(event) {
|
||||
: decryptFile(fileInput, password);
|
||||
}
|
||||
|
||||
await handleTextOperation(encryptionType, operation, password);
|
||||
await handleTextOperation(operation, password);
|
||||
}
|
||||
|
||||
async function handleTextOperation(encryptionType, operation, password) {
|
||||
async function handleTextOperation(operation, password) {
|
||||
const algorithm = document.getElementById("algorithm")?.value || "aes_gcm";
|
||||
|
||||
const payload = {
|
||||
"encryption-type": encryptionType,
|
||||
operation: operation,
|
||||
message: document.getElementById("input-text")?.value,
|
||||
password: password
|
||||
algorithm: algorithm
|
||||
};
|
||||
|
||||
// Add appropriate authentication based on algorithm
|
||||
let requiresKeypair = false;
|
||||
if (window.availableAlgorithms && window.availableAlgorithms[algorithm]) {
|
||||
requiresKeypair = window.availableAlgorithms[algorithm].requires_keypair || false;
|
||||
} else {
|
||||
requiresKeypair = algorithm.includes("hybrid");
|
||||
}
|
||||
|
||||
if (requiresKeypair) {
|
||||
const globalKeys = window.getGlobalKeys ? window.getGlobalKeys() : {};
|
||||
if (operation === "encrypt" && globalKeys.publicKey) {
|
||||
payload.public_key = globalKeys.publicKey;
|
||||
} else if (operation === "decrypt" && globalKeys.privateKey) {
|
||||
payload.private_key = globalKeys.privateKey;
|
||||
}
|
||||
} else {
|
||||
payload.password = password;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch("/", {
|
||||
const endpoint = operation === "encrypt" ? "/api/encrypt" : "/api/decrypt";
|
||||
const response = await fetch(endpoint, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload)
|
||||
@@ -178,7 +199,11 @@ async function handleTextOperation(encryptionType, operation, password) {
|
||||
|
||||
const outputField = document.getElementById("output-text");
|
||||
if (outputField) {
|
||||
outputField.value = data.result || "[Error] No response received.";
|
||||
if (data.error) {
|
||||
outputField.value = `[Error] ${data.error}`;
|
||||
} else {
|
||||
outputField.value = data.result || "[Error] No response received.";
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
alert("Error processing request: " + err.message);
|
||||
@@ -328,5 +353,229 @@ function showCopyFeedback(feedbackEl) {
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
// ===== Algorithm Management =====
|
||||
async function loadAvailableAlgorithms() {
|
||||
try {
|
||||
const response = await fetch('/api/algorithms');
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok && data.algorithms) {
|
||||
// Store algorithms globally for use in other functions
|
||||
window.availableAlgorithms = data.algorithms;
|
||||
updateAlgorithmDropdown(data.algorithms);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load algorithms:', error);
|
||||
}
|
||||
}
|
||||
|
||||
function updateAlgorithmDropdown(algorithms) {
|
||||
const algorithmSelect = document.getElementById('algorithm');
|
||||
const shareAlgorithmSelect = document.getElementById('share-algorithm');
|
||||
|
||||
// Update main encryption/decryption algorithm dropdown
|
||||
if (algorithmSelect) {
|
||||
algorithmSelect.innerHTML = '';
|
||||
|
||||
let firstOption = null;
|
||||
for (const [key, algo] of Object.entries(algorithms)) {
|
||||
if (algo.supports_text) {
|
||||
const option = document.createElement('option');
|
||||
option.value = key;
|
||||
option.textContent = `${algo.name}${algo.requires_keypair ? ' (requires keypair)' : ''}`;
|
||||
algorithmSelect.appendChild(option);
|
||||
|
||||
// Remember the first option (should be a non-keypair algorithm)
|
||||
if (!firstOption) {
|
||||
firstOption = key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the first option is selected
|
||||
if (firstOption) {
|
||||
algorithmSelect.value = firstOption;
|
||||
}
|
||||
}
|
||||
|
||||
// Update PacShare algorithm dropdown (for file uploads)
|
||||
if (shareAlgorithmSelect) {
|
||||
shareAlgorithmSelect.innerHTML = '';
|
||||
|
||||
let firstFileOption = null;
|
||||
for (const [key, algo] of Object.entries(algorithms)) {
|
||||
if (algo.supports_file) {
|
||||
const option = document.createElement('option');
|
||||
option.value = key;
|
||||
option.textContent = `${algo.name}${algo.requires_keypair ? ' (requires keypair)' : ''}`;
|
||||
shareAlgorithmSelect.appendChild(option);
|
||||
|
||||
// Remember the first file-supporting option
|
||||
if (!firstFileOption) {
|
||||
firstFileOption = key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set the first file-supporting option as selected
|
||||
if (firstFileOption) {
|
||||
shareAlgorithmSelect.value = firstFileOption;
|
||||
}
|
||||
}
|
||||
|
||||
// Update Key Pairs Management dropdown
|
||||
const keypairAlgorithmSelect = document.getElementById('keypair-algorithm');
|
||||
if (keypairAlgorithmSelect) {
|
||||
// Clear existing options except the hardcoded ones
|
||||
const options = keypairAlgorithmSelect.querySelectorAll('option');
|
||||
options.forEach(option => {
|
||||
if (option.value !== 'rsa_hybrid' && option.value !== 'pqcrypto') {
|
||||
option.remove();
|
||||
}
|
||||
});
|
||||
|
||||
// Show/hide post-quantum option based on availability
|
||||
const pqOption = document.getElementById('pqcrypto-option');
|
||||
if (pqOption) {
|
||||
pqOption.style.display = algorithms.pqcrypto ? 'block' : 'none';
|
||||
}
|
||||
|
||||
// If rsa_hybrid is not available, hide it
|
||||
const rsaOption = keypairAlgorithmSelect.querySelector('option[value="rsa_hybrid"]');
|
||||
if (rsaOption) {
|
||||
rsaOption.style.display = algorithms.rsa_hybrid ? 'block' : 'none';
|
||||
}
|
||||
}
|
||||
|
||||
// Call toggleAlgorithmOptions after dropdown is populated
|
||||
toggleAlgorithmOptions();
|
||||
}
|
||||
|
||||
function toggleAlgorithmOptions() {
|
||||
const algorithm = document.getElementById("algorithm")?.value;
|
||||
const keypairSection = document.getElementById("keypair-section");
|
||||
const passwordInput = document.getElementById("password-input");
|
||||
|
||||
if (!algorithm) return;
|
||||
|
||||
// Check if algorithm requires keypair by looking at available algorithms data
|
||||
let requiresKeypair = false;
|
||||
if (window.availableAlgorithms && window.availableAlgorithms[algorithm]) {
|
||||
requiresKeypair = window.availableAlgorithms[algorithm].requires_keypair || false;
|
||||
} else {
|
||||
// Fallback to checking name for "hybrid"
|
||||
requiresKeypair = algorithm.includes("hybrid");
|
||||
}
|
||||
|
||||
// Show/hide keypair section only for algorithms that require it
|
||||
if (keypairSection) {
|
||||
keypairSection.style.display = requiresKeypair ? "block" : "none";
|
||||
}
|
||||
|
||||
// Show/hide password input (opposite of keypair section)
|
||||
if (passwordInput) {
|
||||
passwordInput.style.display = requiresKeypair ? "none" : "block";
|
||||
}
|
||||
|
||||
// Update key status based on global keys
|
||||
const globalKeys = window.getGlobalKeys ? window.getGlobalKeys() : {};
|
||||
const publicStatus = document.getElementById("public-key-status");
|
||||
const privateStatus = document.getElementById("private-key-status");
|
||||
|
||||
if (!requiresKeypair) {
|
||||
if (publicStatus) publicStatus.style.display = "none";
|
||||
if (privateStatus) privateStatus.style.display = "none";
|
||||
} else {
|
||||
// Show key status if keys are loaded in global store
|
||||
if (publicStatus) publicStatus.style.display = globalKeys.publicKey ? "block" : "none";
|
||||
if (privateStatus) privateStatus.style.display = globalKeys.privateKey ? "block" : "none";
|
||||
}
|
||||
}
|
||||
|
||||
// ===== File-based Key Management =====
|
||||
async function generateAndDownloadKeyPair() {
|
||||
const algorithm = document.getElementById("algorithm")?.value;
|
||||
|
||||
let requiresKeypair = false;
|
||||
if (window.availableAlgorithms && window.availableAlgorithms[algorithm]) {
|
||||
requiresKeypair = window.availableAlgorithms[algorithm].requires_keypair || false;
|
||||
} else {
|
||||
requiresKeypair = algorithm.includes("hybrid");
|
||||
}
|
||||
|
||||
if (!algorithm || !requiresKeypair) {
|
||||
alert("Key pair generation is only available for algorithms that require key pairs");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/generate-keypair', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ algorithm: algorithm })
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok) {
|
||||
// Download public key
|
||||
downloadTextAsFile(data.public_key, `${algorithm}_public_key.pub`, 'text/plain');
|
||||
|
||||
// Download private key
|
||||
downloadTextAsFile(data.private_key, `${algorithm}_private_key.key`, 'text/plain');
|
||||
|
||||
alert("✅ Key pair generated and downloaded!\n\n📁 Files saved:\n• Public Key: " + `${algorithm}_public_key.pub` + "\n• Private Key: " + `${algorithm}_private_key.key` + "\n\n🔐 Use public key for encryption, private key for decryption.");
|
||||
} else {
|
||||
alert(`Error generating key pair: ${data.error}`);
|
||||
}
|
||||
} catch (error) {
|
||||
alert(`Error: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
function downloadTextAsFile(text, filename, mimeType) {
|
||||
const blob = new Blob([text], { type: mimeType });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
function handlePublicKeyLoad(event) {
|
||||
const file = event.target.files[0];
|
||||
if (!file) return;
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(e) {
|
||||
// Update global keys instead of window variables
|
||||
if (window.setGlobalKeys) {
|
||||
window.setGlobalKeys({ publicKey: e.target.result });
|
||||
}
|
||||
document.getElementById("public-key-status").style.display = "block";
|
||||
console.log("Public key loaded successfully and synced to global store");
|
||||
};
|
||||
reader.readAsText(file);
|
||||
}
|
||||
|
||||
function handlePrivateKeyLoad(event) {
|
||||
const file = event.target.files[0];
|
||||
if (!file) return;
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(e) {
|
||||
// Update global keys instead of window variables
|
||||
if (window.setGlobalKeys) {
|
||||
window.setGlobalKeys({ privateKey: e.target.result });
|
||||
}
|
||||
document.getElementById("private-key-status").style.display = "block";
|
||||
console.log("Private key loaded successfully and synced to global store");
|
||||
};
|
||||
reader.readAsText(file);
|
||||
}
|
||||
|
||||
function startPacman() { }
|
||||
function exitGame() { }
|
||||
Reference in New Issue
Block a user