From 799b261c02fe000400aa4e8603d1b6b81499489c Mon Sep 17 00:00:00 2001 From: VisualCryptoLab Date: Tue, 13 Jan 2026 14:00:05 +0100 Subject: [PATCH 1/7] New project structure. Refactor from Antigravity --- src/App.jsx | 1764 ++-------------------- src/components/DraggableBox.jsx | 399 +++++ src/components/Port.jsx | 52 + src/components/Toolbar.jsx | 65 + src/components/icons/BitShiftIcon.jsx | 21 + src/components/icons/XORIcon.jsx | 21 + src/components/ui/StatusNotification.jsx | 21 + src/components/ui/ToolbarButton.jsx | 19 + src/constants/appConstants.js | 80 + src/styles/globalStyles.js | 49 + src/utils/canvasUtils.js | 18 + src/utils/cryptoUtils.js | 390 +++++ src/utils/formatUtils.js | 162 ++ src/utils/graphUtils.js | 246 +++ src/utils/projectUtils.js | 39 + vite.config.js | 7 +- 16 files changed, 1719 insertions(+), 1634 deletions(-) create mode 100644 src/components/DraggableBox.jsx create mode 100644 src/components/Port.jsx create mode 100644 src/components/Toolbar.jsx create mode 100644 src/components/icons/BitShiftIcon.jsx create mode 100644 src/components/icons/XORIcon.jsx create mode 100644 src/components/ui/StatusNotification.jsx create mode 100644 src/components/ui/ToolbarButton.jsx create mode 100644 src/constants/appConstants.js create mode 100644 src/styles/globalStyles.js create mode 100644 src/utils/canvasUtils.js create mode 100644 src/utils/cryptoUtils.js create mode 100644 src/utils/formatUtils.js create mode 100644 src/utils/graphUtils.js create mode 100644 src/utils/projectUtils.js diff --git a/src/App.jsx b/src/App.jsx index cfde89b..bc72a32 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,1653 +1,151 @@ import React, { useState, useCallback, useRef, useEffect, useMemo } from 'react'; -import { LayoutGrid, Cpu, Key, Zap, Settings, Lock, Unlock, Hash, Clipboard, X, ArrowLeft, ArrowRight, Download, Upload, Camera, ChevronDown, ChevronUp, CheckCheck, Fingerprint, Signature, ZoomIn, ZoomOut, Info, Split } from 'lucide-react'; +import { + NODE_DEFINITIONS, + PROJECT_SCHEMA_VERSION, + INITIAL_NODES, + INITIAL_CONNECTIONS, + NODE_DIMENSIONS +} from './constants/appConstants'; +import { globalStyles } from './styles/globalStyles'; +import { migrateProjectData, downloadFile } from './utils/projectUtils'; +import { getLinePath } from './utils/canvasUtils'; +import { recalculateGraph } from './utils/graphUtils'; +import { getOutputFormat } from './utils/cryptoUtils'; +import Toolbar from './components/Toolbar'; +import DraggableBox from './components/DraggableBox'; +import StatusNotification from './components/ui/StatusNotification'; -// --- Global Configuration --- -const PROJECT_SCHEMA_VERSION = '1.2'; - -// --- CSS Styles --- -const globalStyles = ` - @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap'); - - html, body, #root { - height: 100%; - margin: 0; - padding: 0; - font-family: 'Inter', sans-serif; - } - - @keyframes animate-pulse-slow { - 0%, 100% { opacity: 1; } - 50% { opacity: 0.5; } - } - .animate-pulse-slow { - animation: animate-pulse-slow 2s cubic-bezier(0.4, 0, 0.6, 1) infinite; - } - - .connection-line-visible { - stroke: #059669; /* Emerald 600 */ - fill: none; - pointer-events: none; - } - .connection-hitbox { - stroke: transparent; - fill: none; - cursor: pointer; - pointer-events: stroke; - } - .connection-hitbox:hover { - stroke: rgba(248, 113, 129, 0.5); - } - - /* Custom scrollbar for panels */ - ::-webkit-scrollbar { - width: 6px; - height: 6px; - } - ::-webkit-scrollbar-track { - background: #f1f1f1; - } - ::-webkit-scrollbar-thumb { - background: #c1c1c1; - border-radius: 3px; - } - ::-webkit-scrollbar-thumb:hover { - background: #a8a8a8; - } -`; - -// --- Custom Icons --- - -function XORIcon(props) { - return ( - - - - - - ); -} - -function BitShiftIcon(props) { - return ( - - - - - - ); -} - -// --- Constants & Maps --- - -const BORDER_CLASSES = { - blue: 'border-blue-600', red: 'border-red-600', orange: 'border-orange-600', cyan: 'border-cyan-600', pink: 'border-pink-500', - teal: 'border-teal-600', gray: 'border-gray-600', lime: 'border-lime-600', indigo: 'border-indigo-600', - purple: 'border-purple-600', maroon: 'border-red-800', rose: 'border-pink-700', amber: 'border-amber-500', - yellow: 'border-yellow-400', fuchsia: 'border-fuchsia-600', green: 'border-green-600', -}; - -const HOVER_BORDER_CLASSES = { - blue: 'hover:border-blue-500', red: 'hover:border-red-500', orange: 'hover:border-orange-500', cyan: 'hover:border-cyan-500', pink: 'hover:border-pink-500', - teal: 'hover:border-teal-500', gray: 'hover:border-gray-500', lime: 'hover:border-lime-500', indigo: 'hover:border-indigo-500', - purple: 'hover:border-purple-500', maroon: 'hover:border-red-700', rose: 'hover:border-pink-600', amber: 'hover:border-amber-400', - yellow: 'hover:border-yellow-300', fuchsia: 'hover:border-fuchsia-500', green: 'hover:border-green-500', -}; - -const TEXT_ICON_CLASSES = { - blue: 'text-blue-600', red: 'text-red-600', orange: 'text-orange-600', cyan: 'text-cyan-600', pink: 'text-pink-500', - teal: 'text-teal-600', gray: 'text-gray-600', lime: 'text-lime-600', indigo: 'text-indigo-600', - purple: 'text-purple-600', maroon: 'text-red-800', rose: 'text-pink-700', amber: 'text-amber-500', - yellow: 'text-yellow-400', fuchsia: 'text-fuchsia-600', green: 'text-green-600', -}; - -const HOVER_BORDER_TOOLBAR_CLASSES = { - blue: 'hover:border-blue-400', red: 'hover:border-red-400', orange: 'hover:border-orange-400', cyan: 'hover:border-cyan-400', - pink: 'hover:border-pink-400', teal: 'hover:border-teal-400', gray: 'hover:border-gray-400', lime: 'hover:border-lime-400', - indigo: 'hover:border-indigo-400', purple: 'hover:border-purple-400', maroon: 'hover:border-red-600', rose: 'hover:border-pink-600', - amber: 'hover:border-amber-400', yellow: 'hover:border-yellow-300', fuchsia: 'hover:border-fuchsia-400', green: 'hover:border-green-400', -}; - -const PORT_SIZE = 4; -const INPUT_PORT_COLOR = 'bg-stone-500'; -const OPTIONAL_PORT_COLOR = 'bg-gray-400'; -const OUTPUT_PORT_COLOR = 'bg-emerald-500'; -const PUBLIC_KEY_COLOR = 'bg-lime-500'; -const PRIVATE_KEY_COLOR = 'bg-red-800'; -const SIGNATURE_COLOR = 'bg-fuchsia-500'; - -const HASH_ALGORITHMS = ['SHA-256', 'SHA-512']; -const SYM_ALGORITHMS = ['AES-GCM']; -const ASYM_ALGORITHMS = ['RSA-OAEP']; -const ALL_FORMATS = ['Text (UTF-8)', 'Base64', 'Hexadecimal', 'Binary', 'Decimal']; - -const NODE_DEFINITIONS = { - DATA_INPUT: { label: 'Data Input', color: 'blue', icon: LayoutGrid, inputPorts: [], outputPorts: [{ name: 'Data Output', type: 'data', keyField: 'dataOutput' }] }, - OUTPUT_VIEWER: { label: 'Output Viewer', color: 'red', icon: Zap, inputPorts: [{ name: 'Data Input', type: 'data', mandatory: true, id: 'data' }], outputPorts: [{ name: 'Viewer Data Output', type: 'data', keyField: 'dataOutput' }] }, - HASH_FN: { label: 'Hash Function', color: 'gray', icon: Hash, inputPorts: [{ name: 'Data Input', type: 'data', mandatory: true, id: 'data' }], outputPorts: [{ name: 'Hash Output', type: 'data', keyField: 'dataOutput' }] }, - XOR_OP: { label: 'XOR Operation', color: 'lime', icon: XORIcon, inputPorts: [{ name: 'Input A', type: 'data', mandatory: true, id: 'dataA' }, { name: 'Input B', type: 'data', mandatory: true, id: 'dataB' }], outputPorts: [{ name: 'Result', type: 'data', keyField: 'dataOutput' }] }, - SHIFT_OP: { label: 'Bit Shift', color: 'indigo', icon: BitShiftIcon, inputPorts: [{ name: 'Data Input', type: 'data', mandatory: true, id: 'data' }], outputPorts: [{ name: 'Result', type: 'data', keyField: 'dataOutput' }] }, - DATA_SPLIT: { label: 'Data Split', color: 'green', icon: Split, inputPorts: [{ name: 'Data Input', type: 'data', mandatory: true, id: 'data' }], outputPorts: [{ name: 'Chunk 1', type: 'data', keyField: 'chunk1' }, { name: 'Chunk 2', type: 'data', keyField: 'chunk2' }] }, - DATA_CONCAT: { label: 'Data Concatenate', color: 'teal', icon: Cpu, inputPorts: [{ name: 'Data A', type: 'data', mandatory: true, id: 'dataA' }, { name: 'Data B', type: 'data', mandatory: true, id: 'dataB' }], outputPorts: [{ name: 'Concatenated Output', type: 'data', keyField: 'dataOutput' }] }, - CAESAR_CIPHER: { label: 'Caesar Cipher', color: 'amber', icon: Lock, inputPorts: [{ name: 'Plaintext', type: 'data', mandatory: true, id: 'plaintext' }], outputPorts: [{ name: 'Ciphertext', type: 'data', keyField: 'dataOutput' }] }, - VIGENERE_CIPHER: { label: 'Vigenère Cipher', color: 'yellow', icon: Lock, inputPorts: [{ name: 'Plaintext/Ciphertext', type: 'data', mandatory: true, id: 'data' }], outputPorts: [{ name: 'Result', type: 'data', keyField: 'dataOutput' }] }, - KEY_GEN: { label: 'Sym Key Generator', color: 'orange', icon: Key, inputPorts: [], outputPorts: [{ name: 'Key Output (AES)', type: 'key', keyField: 'dataOutput' }] }, - SIMPLE_RSA_KEY_GEN: { label: 'Simple RSA PrivKey Gen', color: 'purple', icon: Key, inputPorts: [], outputPorts: [{ name: 'Private Key (d)', type: 'private', keyField: 'dataOutputPrivate' }] }, - SIMPLE_RSA_PUBKEY_GEN: { label: 'Simple RSA PubKey Gen', color: 'lime', icon: Unlock, inputPorts: [{ name: 'Private Key Source', type: 'private', mandatory: false, id: 'keySource' }], outputPorts: [{ name: 'Public Key (n, e)', type: 'public', keyField: 'dataOutputPublic' }] }, - SIMPLE_RSA_ENC: { label: 'Simple RSA Encrypt', color: 'maroon', icon: Lock, inputPorts: [{ name: 'Message (m)', type: 'data', mandatory: true, id: 'message' }, { name: 'Public Key (n, e)', type: 'public', mandatory: true, id: 'publicKey' }], outputPorts: [{ name: 'Ciphertext (c)', type: 'data', keyField: 'dataOutput' }] }, - SIMPLE_RSA_DEC: { label: 'Simple RSA Decrypt', color: 'rose', icon: Unlock, inputPorts: [{ name: 'Ciphertext (c)', type: 'data', mandatory: true, id: 'cipher' }, { name: 'Private Key (d)', type: 'private', mandatory: true, id: 'privateKey' }], outputPorts: [{ name: 'Plaintext (m)', type: 'data', keyField: 'dataOutput' }] }, - SIMPLE_RSA_SIGN: { label: 'Simple RSA Sign', color: 'fuchsia', icon: Signature, inputPorts: [{ name: 'Message (m)', type: 'data', mandatory: true, id: 'message' }, { name: 'Private Key (d)', type: 'private', mandatory: true, id: 'privateKey' }], outputPorts: [{ name: 'Signature (s)', type: 'data', keyField: 'dataOutput' }] }, - SIMPLE_RSA_VERIFY: { label: 'Simple RSA Verify', color: 'fuchsia', icon: CheckCheck, inputPorts: [{ name: 'Message (m)', type: 'data', mandatory: true, id: 'message' }, { name: 'Signature (s)', type: 'data', mandatory: true, id: 'signature' }, { name: 'Public Key (n, e)', type: 'public', mandatory: true, id: 'publicKey' }], outputPorts: [{ name: 'Verification Result', type: 'data', keyField: 'dataOutput' }] }, - SYM_ENC: { label: 'Sym Encrypt', color: 'red', icon: Lock, inputPorts: [{ name: 'Data Input', type: 'data', mandatory: true, id: 'data' }, { name: 'Key Input', type: 'key', mandatory: true, id: 'key' }], outputPorts: [{ name: 'Ciphertext', type: 'data', keyField: 'dataOutput' }] }, - SYM_DEC: { label: 'Sym Decrypt', color: 'pink', icon: Unlock, inputPorts: [{ name: 'Cipher Input', type: 'data', mandatory: true, id: 'cipher' }, { name: 'Key Input', type: 'key', mandatory: true, id: 'key' }], outputPorts: [{ name: 'Plaintext', type: 'data', keyField: 'dataOutput' }] }, - ASYM_ENC: { label: 'Asym Encrypt', color: 'cyan', icon: Lock, inputPorts: [{ name: 'Data Input', type: 'data', mandatory: true, id: 'data' }, { name: 'Public Key', type: 'public', mandatory: true, id: 'publicKey' }], outputPorts: [{ name: 'Ciphertext', type: 'data', keyField: 'dataOutput' }] }, - ASYM_DEC: { label: 'Asym Decrypt', color: 'teal', icon: Unlock, inputPorts: [{ name: 'Cipher Input', type: 'data', mandatory: true, id: 'cipher' }, { name: 'Private Key', type: 'private', mandatory: true, id: 'privateKey' }], outputPorts: [{ name: 'Plaintext', type: 'data', keyField: 'dataOutput' }] }, -}; - -const ORDERED_NODE_GROUPS = [ - { name: 'CORE TOOLS', types: ['DATA_INPUT', 'OUTPUT_VIEWER', 'HASH_FN', 'XOR_OP', 'SHIFT_OP', 'DATA_SPLIT', 'DATA_CONCAT'] }, - { name: 'CLASSIC CIPHERS', types: ['CAESAR_CIPHER', 'VIGENERE_CIPHER'] }, - { name: 'SIMPLE RSA', types: ['SIMPLE_RSA_KEY_GEN', 'SIMPLE_RSA_PUBKEY_GEN', 'SIMPLE_RSA_ENC', 'SIMPLE_RSA_DEC', 'SIMPLE_RSA_SIGN', 'SIMPLE_RSA_VERIFY'] }, - { name: 'SYMMETRIC CRYPTO (AES)', types: ['KEY_GEN', 'SYM_ENC', 'SYM_DEC'] }, -]; - -const INITIAL_NODES = []; -const INITIAL_CONNECTIONS = []; -const NODE_DIMENSIONS = { initialWidth: 300, initialHeight: 280, minWidth: 250, minHeight: 250 }; - -// --- Logic & Helpers --- - -const modPow = (base, exponent, modulus) => { - if (modulus === BigInt(1)) return BigInt(0); - let result = BigInt(1); - base = base % modulus; - while (exponent > BigInt(0)) { - if (exponent % BigInt(2) === BigInt(1)) { - result = (result * base) % modulus; - } - exponent = exponent >> BigInt(1); - base = (base * base) % modulus; - } - return result; -}; - -const gcd = (a, b) => { - while (b) { - [a, b] = [b, a % b]; - } - return a; -}; - -const modInverse = (a, m) => { - let m0 = m; - let x0 = BigInt(0); - let x1 = BigInt(1); - if (m === BigInt(1)) return BigInt(0); - while (a > BigInt(1)) { - let q = a / m; - let t = m; - m = a % m; - a = t; - t = x0; - x0 = x1 - q * x0; - x1 = t; - } - if (x1 < BigInt(0)) { - x1 += m0; - } - return x1; -}; - -const DEMO_PRIMES = [167, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283]; - -const generateSmallPrimes = () => { - let p = 0; - let q = 0; - while (p === q) { - p = DEMO_PRIMES[Math.floor(Math.random() * DEMO_PRIMES.length)]; - q = DEMO_PRIMES[Math.floor(Math.random() * DEMO_PRIMES.length)]; - } - return { p: BigInt(p), q: BigInt(q) }; -}; - -const generateSmallE = (phiN) => { - let e = BigInt(0); - do { - e = BigInt(Math.floor(Math.random() * (Number(phiN) - 3)) + 2); - } while (gcd(e, phiN) !== BigInt(1)); - return e; -}; - -const caesarEncrypt = (inputData, inputFormat, k) => { - if (inputFormat !== 'Text (UTF-8)') { - return { output: `ERROR: Caesar Cipher requires Text (UTF-8) input. Received: ${inputFormat}`, format: inputFormat }; - } - let ciphertext = ''; - const shift = (k % 26 + 26) % 26; - const plaintext = inputData; - for (let i = 0; i < plaintext.length; i++) { - const char = plaintext[i]; - const charCode = char.charCodeAt(0); - if (charCode >= 65 && charCode <= 90) { - const encryptedCode = ((charCode - 65 + shift) % 26) + 65; - ciphertext += String.fromCharCode(encryptedCode); - } else if (charCode >= 97 && charCode <= 122) { - const encryptedCode = ((charCode - 97 + shift) % 26) + 97; - ciphertext += String.fromCharCode(encryptedCode); - } else { - ciphertext += char; - } - } - return { output: ciphertext, format: 'Text (UTF-8)' }; -}; - -const vigenereEncryptDecrypt = (inputData, keyWord, mode = 'ENCRYPT') => { - if (!keyWord || keyWord.length === 0) return { output: "ERROR: Keyword cannot be empty.", format: 'Text (UTF-8)' }; - if (inputData.startsWith('ERROR')) return { output: inputData, format: 'Text (UTF-8)' }; - - let result = ''; - let keyIndex = 0; - const plaintext = inputData; - const alphabetSize = 26; - - for (let i = 0; i < plaintext.length; i++) { - const char = plaintext[i]; - const charCode = char.charCodeAt(0); - if ((charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122)) { - const keyChar = keyWord[keyIndex % keyWord.length]; - let keyShift = keyChar.toUpperCase().charCodeAt(0) - 65; - let base = (charCode >= 65 && charCode <= 90) ? 65 : 97; - let charOffset = charCode - base; - let encryptedOffset; - if (mode === 'ENCRYPT') { - encryptedOffset = (charOffset + keyShift) % alphabetSize; - } else { - encryptedOffset = (charOffset - keyShift + alphabetSize) % alphabetSize; - } - result += String.fromCharCode(encryptedOffset + base); - keyIndex++; - } else { - result += char; - } - } - return { output: result, format: 'Text (UTF-8)' }; -}; - -const arrayBufferToBase64 = (buffer) => { - const bytes = new Uint8Array(buffer); - let binary = ''; - for (let i = 0; i < bytes.byteLength; i++) { - binary += String.fromCharCode(bytes[i]); - } - return btoa(binary); -}; - -const base64ToArrayBuffer = (base64) => { - const binary_string = atob(base64); - const len = binary_string.length; - const bytes = new Uint8Array(len); - for (let i = 0; i < len; i++) { - bytes[i] = binary_string.charCodeAt(i); - } - return bytes.buffer; -}; - -const arrayBufferToBigIntString = (buffer) => { - const hex = arrayBufferToHex(buffer); - if (hex.length === 0) return '0'; - try { - return BigInt(`0x${hex}`).toString(10); - } catch (e) { - return `ERROR: Data too large for BigInt conversion (${buffer.byteLength} bytes).`; - } -}; - -const arrayBufferToHexBig = (buffer) => { - return arrayBufferToHex(buffer).toUpperCase(); -}; - -const arrayBufferToBinaryBig = (buffer) => { - const byteArray = new Uint8Array(buffer); - let binary = ''; - for (const byte of byteArray) { - binary += byte.toString(2).padStart(8, '0'); - } - return binary; -}; - -const arrayBufferToHex = (buffer) => { - const byteArray = new Uint8Array(buffer); - return Array.from(byteArray).map(byte => byte.toString(16).padStart(2, '0')).join(''); -}; - -const arrayBufferToBinary = (buffer) => { - const byteArray = new Uint8Array(buffer); - return Array.from(byteArray).map(byte => byte.toString(2).padStart(8, '0')).join(' '); -}; - -const hexToArrayBuffer = (hex) => { - const cleanedHex = hex.replace(/\s/g, ''); - if (cleanedHex.length === 0) return new ArrayBuffer(0); - const paddedHex = cleanedHex.length % 2 !== 0 ? '0' + cleanedHex : cleanedHex; - const len = paddedHex.length / 2; - const bytes = new Uint8Array(len); - for (let i = 0; i < len; i++) { - bytes[i] = parseInt(paddedHex.substring(i * 2, i * 2 + 2), 16); - } - return bytes.buffer; -}; - -const convertToUint8Array = (dataStr, sourceFormat) => { - if (!dataStr) return new Uint8Array(0); - try { - if (sourceFormat === 'Text (UTF-8)') return new TextEncoder().encode(dataStr); - if (sourceFormat === 'Base64') return new Uint8Array(base64ToArrayBuffer(dataStr)); - if (sourceFormat === 'Hexadecimal') { - const cleanedHex = dataStr.replace(/\s/g, ''); - return new Uint8Array(hexToArrayBuffer(cleanedHex)); - } - if (sourceFormat === 'Binary') { - const binaryArray = dataStr.replace(/\s+/g, '').match(/.{1,8}/g) || []; - const validBytes = binaryArray.map(s => parseInt(s, 2)).filter(b => !isNaN(b)); - return new Uint8Array(validBytes); - } - if (sourceFormat === 'Decimal') { - const decimalArray = dataStr.split(/\s+/).map(s => parseInt(s, 10)); - const validBytes = decimalArray.filter(b => !isNaN(b) && b >= 0 && b >= 255); - return new Uint8Array(validBytes); - } - return new TextEncoder().encode(dataStr); - } catch (e) { - console.error(`Conversion to Uint8Array failed for format ${sourceFormat}:`, e); - return new Uint8Array(0); - } -}; - -const convertDataFormat = (dataStr, sourceFormat, targetFormat, toSingleNumber = false) => { - if (!dataStr) return ''; - if (sourceFormat === targetFormat || dataStr.startsWith('ERROR')) return dataStr; - let buffer; - try { - if (sourceFormat === 'Text (UTF-8)') buffer = new TextEncoder().encode(dataStr).buffer; - else if (sourceFormat === 'Base64') buffer = base64ToArrayBuffer(dataStr); - else if (sourceFormat === 'Hexadecimal') buffer = hexToArrayBuffer(dataStr.replace(/\s/g, '')); - else if (sourceFormat === 'Binary') { - const binaryArray = dataStr.replace(/\s+/g, '').match(/.{1,8}/g) || []; - const validBytes = binaryArray.map(s => parseInt(s, 2)).filter(b => !isNaN(b) && b >= 0 && b <= 255); - buffer = new Uint8Array(validBytes).buffer; - } else if (sourceFormat === 'Decimal') { - const decimalArray = dataStr.split(/\s+/).map(s => parseInt(s, 10)); - const validBytes = decimalArray.filter(b => !isNaN(b) && b >= 0 && b <= 255); - buffer = new Uint8Array(validBytes).buffer; - } else buffer = new TextEncoder().encode(dataStr).buffer; - } catch (e) { return `DECODING ERROR: Failed source format (${sourceFormat}).`; } - - try { - if (toSingleNumber) { - if (targetFormat === 'Decimal') return arrayBufferToBigIntString(buffer); - if (targetFormat === 'Hexadecimal') return arrayBufferToHexBig(buffer); - if (targetFormat === 'Binary') return arrayBufferToBinaryBig(buffer); - } - if (targetFormat === 'Text (UTF-8)') return new TextDecoder().decode(buffer); - if (targetFormat === 'Base64') return arrayBufferToBase64(buffer); - if (targetFormat === 'Hexadecimal') return arrayBufferToHex(buffer).toUpperCase().match(/.{1,2}/g)?.join(' ') || ''; - if (targetFormat === 'Binary') return arrayBufferToBinary(buffer); - if (targetFormat === 'Decimal') return Array.from(new Uint8Array(buffer)).join(' '); - return `ERROR: Unsupported target format (${targetFormat})`; - } catch (e) { return `ENCODING ERROR: Failed conversion to ${targetFormat}.`; } -}; - -const getOutputFormat = (nodeType) => { - switch (nodeType) { - case 'DATA_INPUT': case 'CAESAR_CIPHER': case 'VIGENERE_CIPHER': return 'Text (UTF-8)'; - case 'KEY_GEN': case 'SYM_ENC': case 'DATA_SPLIT': case 'DATA_CONCAT': return 'Binary'; - case 'ASYM_ENC': case 'SIMPLE_RSA_KEY_GEN': case 'RSA_KEY_GEN': case 'SIMPLE_RSA_PUBKEY_GEN': return 'Base64'; - case 'HASH_FN': return 'Hexadecimal'; - case 'SYM_DEC': case 'ASYM_DEC': return 'Text (UTF-8)'; - case 'SIMPLE_RSA_ENC': case 'SIMPLE_RSA_DEC': case 'SIMPLE_RSA_SIGN': return 'Decimal'; - case 'SIMPLE_RSA_VERIFY': return 'Text (UTF-8)'; - default: return 'Text (UTF-8)'; - } -} - -const performBitwiseXor = (dataAStr, formatA, dataBStr, formatB) => { - if (!dataAStr || !dataBStr || dataAStr.startsWith('ERROR') || dataBStr.startsWith('ERROR')) { - return { output: "ERROR: Missing one or both inputs or inputs failed conversion.", format: formatA }; - } - if (formatA !== formatB || !['Binary', 'Hexadecimal'].includes(formatA)) { - const bytesA = convertToUint8Array(dataAStr, formatA); - const bytesB = convertToUint8Array(dataBStr, formatB); - const combinedBytes = performRawXor(bytesA, bytesB); - const finalFormat = formatA === 'N/A' || formatA === 'Decimal' ? 'Base64' : formatA; - const output = convertDataFormat(arrayBufferToBase64(combinedBytes.buffer), 'Base64', finalFormat); - return { output: output, format: finalFormat }; - } - const cleanA = dataAStr.replace(/\s/g, ''); - const cleanB = dataBStr.replace(/\s/g, ''); - const targetLength = Math.max(cleanA.length, cleanB.length); - const paddedA = cleanA.padStart(targetLength, '0'); - const paddedB = cleanB.padStart(targetLength, '0'); - let bigIntA, bigIntB; - try { - if (formatA === 'Binary') { - bigIntA = BigInt(`0b${paddedA}`); - bigIntB = BigInt(`0b${paddedB}`); - } else if (formatA === 'Hexadecimal') { - bigIntA = BigInt(`0x${paddedA}`); - bigIntB = BigInt(`0x${paddedB}`); - } - } catch (e) { return { output: "ERROR: Data too large for BigInt XOR or invalid numerical input.", format: formatA }; } - const resultBigInt = bigIntA ^ bigIntB; - let resultStr; - if (formatA === 'Binary') resultStr = bigIntToString(resultBigInt, 'Binary', targetLength); - else resultStr = bigIntToString(resultBigInt, 'Hexadecimal', targetLength, true); - return { output: resultStr, format: formatA }; -}; +const App = () => { + const [nodes, setNodes] = useState(INITIAL_NODES); + const [connections, setConnections] = useState(INITIAL_CONNECTIONS); + const [connectingPort, setConnectingPort] = useState(null); + const [statusMessage, setStatusMessage] = useState(null); + const [scale, setScale] = useState(1); + const canvasRef = useRef(null); -const performRawXor = (bytesA, bytesB) => { - const len = Math.min(bytesA.length, bytesB.length); - const result = new Uint8Array(len); - for (let i = 0; i < len; i++) result[i] = bytesA[i] ^ bytesB[i]; - return result; -}; + const clearStatusMessage = useCallback(() => setStatusMessage(null), []); -const stringToBigInt = (dataStr, format) => { - if (!dataStr) return null; - if (dataStr.includes(' ') && format !== 'Text (UTF-8)' && format !== 'Base64') return null; - const cleanedStr = dataStr.replace(/\s/g, ''); - try { - if (format === 'Decimal') { if (!/^\d+$/.test(cleanedStr)) return null; return BigInt(cleanedStr); } - if (format === 'Hexadecimal') { if (!/^[0-9a-fA-F]+$/.test(cleanedStr)) return null; return BigInt(`0x${cleanedStr}`); } - if (format === 'Binary') { - if (!/^[01]+$/.test(cleanedStr)) return null; - const paddedBinary = cleanedStr.padStart(Math.ceil(cleanedStr.length / 4) * 4, '0'); - return BigInt(`0b${paddedBinary}`); - } - } catch (e) { return null; } - return null; -}; + useEffect(() => { setNodes(prevNodes => recalculateGraph(prevNodes, connections, null, setNodes)); }, [connections]); -const bigIntToString = (bigIntValue, format, originalLength = 0, isHexLength = false) => { - if (bigIntValue === null) return 'N/A'; - switch (format) { - case 'Decimal': return bigIntValue.toString(10); - case 'Hexadecimal': - let hexString = bigIntValue.toString(16).toUpperCase(); - if (originalLength > 0) { - const hexLength = isHexLength ? originalLength : Math.ceil(originalLength / 4); - hexString = hexString.padStart(hexLength, '0'); - if (hexString.length > hexLength) hexString = hexString.substring(hexString.length - hexLength); - } - return hexString; - case 'Binary': - let binaryString = bigIntValue.toString(2); - if (originalLength > 0) { - binaryString = binaryString.padStart(originalLength, '0'); - if (binaryString.length > originalLength) binaryString = binaryString.substring(binaryString.length - originalLength); + const updateNodeContent = useCallback((id, field, value) => { + setNodes(prevNodes => { + const nextNodes = prevNodes.map(node => node.id === id ? { ...node, [field]: value } : node); + return recalculateGraph(nextNodes, connections, id, setNodes); + }); + }, [connections]); + + const setPosition = useCallback((id, newPos) => setNodes(prev => prev.map(n => n.id === id ? { ...n, position: newPos } : n)), []); + const handleNodeResize = useCallback((id, w, h) => setNodes(prev => prev.map(n => n.id === id ? { ...n, width: Math.max(NODE_DIMENSIONS.minWidth, w), height: Math.max(NODE_DIMENSIONS.minHeight, h) } : n)), []); + + const addNode = useCallback((type, label, color) => { + const newId = `${type}_${Date.now()}`; + const def = NODE_DEFINITIONS[type]; + const initialContent = { dataOutput: '', isProcessing: false, outputFormat: getOutputFormat(type), width: (['SHIFT_OP', 'XOR_OP', 'DATA_SPLIT', 'DATA_CONCAT'].includes(type) ? 300 : NODE_DIMENSIONS.initialWidth), height: (['SHIFT_OP', 'XOR_OP', 'DATA_SPLIT', 'DATA_CONCAT'].includes(type) ? 300 : NODE_DIMENSIONS.initialHeight) }; + const cv = canvasRef.current; + let x = ((cv?.clientWidth || 800) / 2) - 150 + (Math.random() * 200 - 100); + let y = ((cv?.clientHeight || 600) / 2) - 140 + (Math.random() * 200 - 100); + // Explicitly handle initial values for specific node types + if (type === 'DATA_INPUT') { initialContent.content = ''; initialContent.format = 'Binary'; } + else if (type === 'OUTPUT_VIEWER') { initialContent.isConversionExpanded = false; initialContent.convertedFormat = 'Base64'; } + else if (type === 'CAESAR_CIPHER') initialContent.shiftKey = 3; + else if (type === 'VIGENERE_CIPHER') { initialContent.keyword = 'HELLO'; initialContent.vigenereMode = 'ENCRYPT'; } + else if (type === 'SIMPLE_RSA_KEY_GEN') { initialContent.generateKey = true; initialContent.modulusLength = 0; } + else if (type === 'SHIFT_OP') { initialContent.shiftType = 'Left'; initialContent.shiftAmount = 1; } + + setNodes(prev => [...prev, { id: newId, label: def.label, position: { x: Math.max(20, x), y: Math.max(20, y) }, type, color, ...initialContent }]); + }, []); + + const handleDeleteNode = useCallback((id) => { + setNodes(prev => prev.filter(n => n.id !== id)); + setConnections(prev => prev.filter(c => c.source !== id && c.target !== id)); + }, []); + + const handleConnectStart = useCallback((nodeId, portIndex, outputType) => setConnectingPort({ sourceId: nodeId, sourcePortIndex: portIndex, outputType }), []); + const handleConnectEnd = useCallback((targetId, targetPortId) => { + if (connectingPort && targetId && connectingPort.sourceId !== targetId) { + const { sourceId, sourcePortIndex } = connectingPort; + const targetNode = nodes.find(n => n.id === targetId); + const targetNodeDef = NODE_DEFINITIONS[targetNode?.type]; + if (targetNodeDef && targetNodeDef.inputPorts.some(p => p.id === targetPortId)) { + setConnections(prev => [...prev, { source: sourceId, sourcePortIndex, target: targetId, targetPortId }]); } - return binaryString; - default: return bigIntValue.toString(10); - } -}; - -const performBitShiftOperation = (dataStr, shiftType, shiftAmount, inputFormat) => { - let shiftDescription = `Arithmetic/Logical ${shiftType} Shift (${shiftAmount} bits)`; - if (!dataStr) return { output: "ERROR: Missing data input.", description: shiftDescription }; - if (inputFormat === 'Text (UTF-8)' || inputFormat === 'Base64') return { output: `ERROR: Bit Shift requires input data to be a single number (Decimal, Hexadecimal, or Binary). Received: ${inputFormat}.`, description: shiftDescription }; - const cleanedStr = dataStr.replace(/\s/g, ''); - const bigIntData = stringToBigInt(cleanedStr, inputFormat); - if (bigIntData === null) return { output: `ERROR: Data must represent a single, contiguous number in ${inputFormat} format. Spaces are not allowed.`, description: shiftDescription }; - const amount = BigInt(Math.max(0, parseInt(shiftAmount) || 0)); - let resultBigInt; - let bitLength = 0; - const isRotational = inputFormat === 'Binary' || inputFormat === 'Hexadecimal'; - if (isRotational) { - if (inputFormat === 'Binary') bitLength = cleanedStr.length; - else if (inputFormat === 'Hexadecimal') bitLength = cleanedStr.length * 4; - } - const amountMod = amount % BigInt(bitLength || 1); - try { - if (isRotational && bitLength > 0) { - const L = BigInt(bitLength); - const data = bigIntData; - if (shiftType === 'Left') { - const shiftedLeft = data << amountMod; - const shiftedRight = data >> (L - amountMod); - const mask = (BigInt(1) << L) - BigInt(1); - resultBigInt = (shiftedLeft | shiftedRight) & mask; - shiftDescription = `Rotational Left Shift (ROL) (${shiftAmount} bits)`; - } else if (shiftType === 'Right') { - const shiftedRight = data >> amountMod; - const shiftedLeft = data << (L - amountMod); - const mask = (BigInt(1) << L) - BigInt(1); - resultBigInt = (shiftedRight | shiftedLeft) & mask; - shiftDescription = `Rotational Right Shift (ROR) (${shiftAmount} bits)`; - } - } else { - if (shiftType === 'Left') resultBigInt = bigIntData << amount; - else resultBigInt = bigIntData >> amount; } - } catch (error) { return { output: `ERROR: Bit Shift calculation failed. ${error.message}`, description: shiftDescription }; } - const finalLength = isRotational ? bitLength : 0; - return { output: bigIntToString(resultBigInt, inputFormat, finalLength, inputFormat === 'Hexadecimal'), description: shiftDescription }; -}; - -const splitDataIntoChunks = (dataStr, format) => { - if (!dataStr || dataStr.startsWith('ERROR')) { - const error = dataStr || 'Missing data input.'; - return { chunk1: `ERROR: ${error}`, chunk2: `ERROR: ${error}`, outputFormat: format }; - } - - let cleanData = dataStr.replace(/\s/g, ''); - let representation, splitUnit; - if (format === 'Text (UTF-8)' || format === 'Base64') { representation = cleanData; splitUnit = 'char'; } - else if (format === 'Hexadecimal') { representation = cleanData; splitUnit = 'hex'; } - else if (format === 'Decimal') return { chunk1: `ERROR: Cannot split a single Decimal number.`, chunk2: `ERROR: Cannot split a single Decimal number.`, outputFormat: 'Text (UTF-8)' }; - else { representation = cleanData; splitUnit = 'bin'; } - const length = representation.length; - const midPoint = Math.ceil(length / 2); - const chunk1 = representation.substring(0, midPoint); - const chunk2 = representation.substring(midPoint); - const formatChunk = (chunk, originalFormat) => { - if (originalFormat === 'Hexadecimal' && splitUnit === 'hex') return chunk.match(/.{1,2}/g)?.join(' ')?.trim() || chunk; - if (originalFormat === 'Binary' && splitUnit === 'bin') return chunk.match(/.{1,8}/g)?.join(' ')?.trim() || chunk; - return chunk; - }; - return { chunk1: formatChunk(chunk1, format), chunk2: formatChunk(chunk2, format), outputFormat: format }; -}; - -const concatenateData = (dataAStr, formatA, dataBStr, formatB, interpretAsText = false) => { - if (!dataAStr || dataAStr.startsWith('ERROR')) return { output: dataBStr || "ERROR: Missing data input A and B.", format: formatB || 'Binary' }; - if (!dataBStr || dataBStr.startsWith('ERROR')) return { output: dataAStr, format: formatA || 'Binary' }; - - // Strict text concatenation if checkbox is enabled - if (interpretAsText) { - return { output: dataAStr + dataBStr, format: 'Text (UTF-8)' }; - } - - // STRICT TYPE CHECKING - if (formatA !== formatB) { - return { - output: `ERROR: Type Mismatch. Input A is ${formatA} and Input B is ${formatB}. Inputs must be of the same type.`, - format: 'Text (UTF-8)' + setConnectingPort(null); + }, [connectingPort, nodes]); + + const handleRemoveConnection = useCallback((s, t, sIdx, tId) => setConnections(prev => prev.filter(c => !(c.source === s && c.target === t && c.sourcePortIndex === sIdx && c.targetPortId === tId))), []); + + const handleZoomIn = useCallback(() => setScale(prev => Math.min(prev + 0.1, 2)), []); + const handleZoomOut = useCallback(() => setScale(prev => Math.max(prev - 0.1, 0.5)), []); + + const handleDownloadProject = useCallback(() => { + const projectData = { schemaVersion: PROJECT_SCHEMA_VERSION, nodes: nodes, connections: connections }; + downloadFile(JSON.stringify(projectData, null, 2), `visual_crypto_project_v${PROJECT_SCHEMA_VERSION}.json`, 'application/json'); + setStatusMessage({ type: 'success', message: 'Project exported successfully!' }); + setTimeout(clearStatusMessage, 3000); + }, [nodes, connections, clearStatusMessage]); + + const handleUploadProject = useCallback((fileInput) => { + clearStatusMessage(); + const file = fileInput.files?.[0]; + if (!file) return; + const reader = new FileReader(); + reader.onload = (e) => { + try { + const projectData = JSON.parse(e.target.result); + if (!projectData || !Array.isArray(projectData.nodes)) throw new Error("Invalid JSON"); + const { migratedData, wasMigrated } = migrateProjectData(projectData); + setNodes(migratedData.nodes); + setConnections(migratedData.connections); + setStatusMessage({ type: 'success', message: wasMigrated ? 'Project migrated & loaded!' : 'Project loaded successfully!' }); + } catch (error) { setStatusMessage({ type: 'error', message: 'Import failed: Invalid file.' }); } + setTimeout(clearStatusMessage, 3000); }; - } - - const cleanA = dataAStr.replace(/\s/g, ''); - const cleanB = dataBStr.replace(/\s/g, ''); - - if (formatA === 'Binary') return { output: cleanA + cleanB, format: 'Binary' }; - - if (formatA === 'Hexadecimal') { - // Concatenate without spaces and ensure lower case to match visual expectation from user - return { output: (cleanA + cleanB).toLowerCase(), format: 'Hexadecimal' }; - } - - if (formatA === 'Text (UTF-8)') { - return { output: dataAStr + dataBStr, format: 'Text (UTF-8)' }; - } - - try { - const bytesA = convertToUint8Array(dataAStr, formatA); - const bytesB = convertToUint8Array(dataBStr, formatB); - const combinedBytes = new Uint8Array(bytesA.length + bytesB.length); - combinedBytes.set(bytesA, 0); - combinedBytes.set(bytesB, bytesA.length); - const output = convertDataFormat(arrayBufferToBase64(combinedBytes.buffer), 'Base64', formatA); - return { output, format: formatA }; - } catch (e) { return { output: `ERROR: Concatenation failed. Check data formats.`, format: formatA }; } -}; - -// MODIFIED: calculateHash now takes format to convert input correctly -const calculateHash = async (str, format, algorithm) => { - if (!str) return 'Missing data input.'; - if (!HASH_ALGORITHMS.includes(algorithm)) return `ERROR: Algorithm not supported (${algorithm}).`; - try { - // Convert based on the actual format (e.g. 'Hexadecimal' -> bytes) instead of always assuming text - const data = convertToUint8Array(str, format); - const hashBuffer = await crypto.subtle.digest(algorithm.toUpperCase(), data); - return arrayBufferToHex(hashBuffer); - } catch (error) { return `ERROR: Calculation failed with ${algorithm}.`; } -}; - -const generateSymmetricKey = async (algorithm) => { - try { - const key = await crypto.subtle.generateKey({ name: algorithm, length: 256 }, true, ["encrypt", "decrypt"]); - const rawKey = await crypto.subtle.exportKey('raw', key); - return { keyObject: key, keyBase64: arrayBufferToBase64(rawKey) }; - } catch (error) { return { keyObject: null, keyBase64: `ERROR: Key generation failed. ${error.message}` }; } -}; - -const generateAsymmetricKeyPair = async (algorithm, modulusLength, publicExponentDecimal) => { - let publicExponentArray = new Uint8Array([0x01, 0x00, 0x01]); - const exponentValue = publicExponentDecimal || 65537; - try { - const keyPair = await crypto.subtle.generateKey( - { name: algorithm, modulusLength: modulusLength, publicExponent: publicExponentArray, hash: { name: "SHA-256" } }, - true, ["encrypt", "decrypt", "wrapKey", "unwrapKey"] - ); - const publicKey = await crypto.subtle.exportKey('spki', keyPair.publicKey); - const privateKey = await crypto.subtle.exportKey('pkcs8', keyPair.privateKey); - const privateKeyJwk = await crypto.subtle.exportKey('jwk', keyPair.privateKey); - return { - publicKey: arrayBufferToBase64(publicKey), - privateKey: arrayBufferToBase64(privateKey), - keyPairObject: keyPair, - rsaParameters: { n: privateKeyJwk.n, e: privateKeyJwk.e, d: privateKeyJwk.d, p: privateKeyJwk.p, q: privateKeyJwk.q } + reader.readAsText(file); + fileInput.value = ''; + }, [clearStatusMessage]); + + const connectionPaths = useMemo(() => { + let maxX = 0; let maxY = 0; const padding = 50; + nodes.forEach(node => { maxX = Math.max(maxX, node.position.x + node.width); maxY = Math.max(maxY, node.position.y + node.height); }); + const svgWidth = Math.max(maxX + padding, (canvasRef.current?.clientWidth || 0) / scale); + const svgHeight = Math.max(maxY + padding, (canvasRef.current?.clientHeight || 0) / scale); + return { + size: { width: svgWidth, height: svgHeight }, + paths: connections.map(conn => { + const sN = nodes.find(n => n.id === conn.source); + const tN = nodes.find(n => n.id === conn.target); + return (sN && tN) ? { path: getLinePath(sN, tN, conn), source: conn.source, target: conn.target, sourcePortIndex: conn.sourcePortIndex, targetPortId: conn.targetPortId } : null; + }).filter(Boolean) }; - } catch (error) { return { publicKey: `ERROR: ${error.message}`, privateKey: `ERROR: ${error.message}`, keyPairObject: null, rsaParameters: {} }; } -}; - -const asymmetricEncrypt = async (dataStr, base64PublicKey, algorithm) => { - if (!dataStr) return 'Missing Data Input.'; - if (!base64PublicKey || typeof base64PublicKey !== 'string') return 'Missing or invalid Public Key Input.'; - try { - const keyBuffer = base64ToArrayBuffer(base64PublicKey); - const publicKey = await crypto.subtle.importKey('spki', keyBuffer, { name: algorithm, hash: "SHA-256" }, true, ['encrypt']); - const encoder = new TextEncoder(); - const encryptedBuffer = await crypto.subtle.encrypt({ name: algorithm }, publicKey, encoder.encode(dataStr)); - return arrayBufferToBase64(encryptedBuffer); - } catch (error) { return `ERROR: Asymmetric Encryption failed. ${error.message}`; } -}; - -const asymmetricDecrypt = async (base64Ciphertext, base64PrivateKey, algorithm) => { - if (!base64Ciphertext) return 'Missing Ciphertext Input.'; - if (!base64PrivateKey || typeof base64PrivateKey !== 'string') return 'Missing or invalid Private Key Input.'; - try { - const keyBuffer = base64ToArrayBuffer(base64PrivateKey); - const privateKey = await crypto.subtle.importKey('pkcs8', keyBuffer, { name: algorithm, hash: "SHA-256" }, true, ['decrypt']); - const decryptedBuffer = await crypto.subtle.decrypt({ name: algorithm }, privateKey, base64ToArrayBuffer(base64Ciphertext)); - return new TextDecoder().decode(decryptedBuffer); - } catch (error) { return `ERROR: Asymmetric Decryption failed. ${error.message}`; } -}; - -const symmetricEncrypt = async (dataStr, base64Key, algorithm) => { - if (!dataStr) return 'Missing Data Input.'; - if (!base64Key || typeof base64Key !== 'string') return 'Missing or invalid Key Input.'; - try { - const key = await crypto.subtle.importKey('raw', base64ToArrayBuffer(base64Key), { name: algorithm, length: 256 }, true, ['encrypt', 'decrypt']); - const iv = crypto.getRandomValues(new Uint8Array(12)); - const encryptedBuffer = await crypto.subtle.encrypt({ name: algorithm, iv: iv }, key, new TextEncoder().encode(dataStr)); - const fullCipher = new Uint8Array(iv.byteLength + encryptedBuffer.byteLength); - fullCipher.set(new Uint8Array(iv), 0); - fullCipher.set(new Uint8Array(encryptedBuffer), iv.byteLength); - return arrayBufferToBase64(fullCipher.buffer); - } catch (error) { return `ERROR: Encryption failed. ${error.message}`; } -}; - -const symmetricDecrypt = async (base64Ciphertext, base64Key, algorithm) => { - if (!base64Ciphertext) return 'Missing Ciphertext Input.'; - if (!base64Key || typeof base64Key !== 'string') return 'Missing or invalid Key Input.'; - try { - const key = await crypto.subtle.importKey('raw', base64ToArrayBuffer(base64Key), { name: algorithm, length: 256 }, true, ['encrypt', 'decrypt']); - const fullCipherBuffer = base64ToArrayBuffer(base64Ciphertext); - if (fullCipherBuffer.byteLength < 12) throw new Error('Ciphertext is too short.'); - const iv = fullCipherBuffer.slice(0, 12); - const ciphertext = fullCipherBuffer.slice(12); - const decryptedBuffer = await crypto.subtle.decrypt({ name: algorithm, iv: new Uint8Array(iv) }, key, ciphertext); - return new TextDecoder().decode(decryptedBuffer); - } catch (error) { return `ERROR: Decryption failed. ${error.message}.`; } -}; + }, [connections, nodes, scale]); -const isContentCompatible = (content, targetFormat) => { - const cleanedContent = content.replace(/\s+/g, ''); - if (!cleanedContent) return true; - if (targetFormat === 'Text (UTF-8)') return true; - if (targetFormat === 'Binary') return /^[01]*$/.test(cleanedContent); - if (targetFormat === 'Decimal') return /^\d*$/.test(cleanedContent); - if (targetFormat === 'Hexadecimal') return /^[0-9a-fA-F]*$/.test(cleanedContent); - if (targetFormat === 'Base64') return /^[A-Za-z0-9+/=]*$/.test(cleanedContent); - return true; -}; - -const getLinePath = (sourceNode, targetNode, connection) => { - const sourceDef = NODE_DEFINITIONS[sourceNode.type]; - const targetDef = NODE_DEFINITIONS[targetNode.type]; - const getVerticalPosition = (nodeDef, index, isInput, nodeHeight) => { - const numPorts = isInput ? nodeDef.inputPorts.length : nodeDef.outputPorts.length; - const step = nodeHeight / (numPorts + 1); - return (index + 1) * step; - }; - const sourceVerticalPos = getVerticalPosition(sourceDef, connection.sourcePortIndex, false, sourceNode.height); - const targetPortIndex = targetDef.inputPorts.findIndex(p => p.id === connection.targetPortId); - const targetVerticalPos = getVerticalPosition(targetDef, targetPortIndex, true, targetNode.height); - const p1 = { x: sourceNode.position.x + sourceNode.width, y: sourceNode.position.y + sourceVerticalPos }; - const p2 = { x: targetNode.position.x, y: targetNode.position.y + targetVerticalPos }; - const midX = (p1.x + p2.x) / 2; - return `M${p1.x} ${p1.y} C${midX} ${p1.y}, ${midX} ${p2.y}, ${p2.x} ${p2.y}`; -}; - -const migrateProjectData = (projectData) => { - const currentVersion = PROJECT_SCHEMA_VERSION; - const importedVersion = projectData.schemaVersion || '1.0'; - if (importedVersion === currentVersion) return { migratedData: projectData, wasMigrated: false }; - let migratedData = { ...projectData }; - let wasMigrated = false; - if (importedVersion < '1.1') { - wasMigrated = true; - migratedData.nodes = migratedData.nodes.map(node => { - const newNode = { ...node }; - if (newNode.type === 'DATA_INPUT' && newNode.format && newNode.outputFormat === 'Text (UTF-8)') { - if (['Binary', 'Hexadecimal', 'Decimal'].includes(newNode.format)) newNode.outputFormat = newNode.format; - } - if (!newNode.width || newNode.width < NODE_DIMENSIONS.minWidth) newNode.width = NODE_DIMENSIONS.initialWidth; - if (!newNode.height || newNode.height < NODE_DIMENSIONS.minHeight) { - if (newNode.type === 'XOR_OP' || newNode.type === 'SHIFT_OP' || newNode.type === 'DATA_SPLIT' || newNode.type === 'DATA_CONCAT') newNode.height = 300; - else newNode.height = NODE_DIMENSIONS.initialHeight; - } - if (newNode.type === 'XOR_OP') { delete newNode.shiftType; delete newNode.shiftAmount; delete newNode.shiftDescription; } - return newNode; - }); - } - migratedData.schemaVersion = currentVersion; - return { migratedData, wasMigrated }; -}; - -// --- Sub-Components --- + const handleCanvasClick = useCallback(() => { if (connectingPort) handleConnectEnd(null); }, [connectingPort, handleConnectEnd]); -const Port = React.memo(({ nodeId, type, isConnecting, onStart, onEnd, title, isMandatory, portId, portIndex, outputType, nodes }) => { - let interactionClasses = ""; - let clickHandler = () => {}; - let portColor = OUTPUT_PORT_COLOR; - if (outputType === 'public' || outputType === 'private') { - portColor = outputType === 'public' ? PUBLIC_KEY_COLOR : PRIVATE_KEY_COLOR; - } else if (type === 'input') { - portColor = isMandatory ? INPUT_PORT_COLOR : OPTIONAL_PORT_COLOR; - } - if (type === 'output' && outputType === 'key') portColor = TEXT_ICON_CLASSES['orange'].replace('text', 'bg'); - if (type === 'output' && outputType === 'signature') portColor = SIGNATURE_COLOR.replace('border', 'bg'); - - if (type === 'output') { - clickHandler = (e) => { e.stopPropagation(); onStart(nodeId, portIndex, outputType); }; - interactionClasses = isConnecting?.sourceId === nodeId ? 'ring-4 ring-emerald-300 animate-pulse' : 'hover:ring-4 hover:ring-emerald-300 transition duration-150'; - } else if (type === 'input') { - const targetNode = nodes.find(n => n.id === nodeId); - const targetNodeDef = NODE_DEFINITIONS[targetNode?.type]; - const inputPortDef = targetNodeDef.inputPorts.find(p => p.id === portId); - const inputPortType = inputPortDef?.type; - const isTargetCandidate = isConnecting && isConnecting.sourceId !== nodeId && isConnecting.outputType === inputPortType; - if (isTargetCandidate) { - clickHandler = (e) => { e.stopPropagation(); onEnd(nodeId, portId); }; - interactionClasses = 'ring-4 ring-yellow-300 cursor-pointer animate-pulse-slow'; - } else { - interactionClasses = 'hover:ring-4 hover:ring-stone-300 transition duration-150'; - clickHandler = (e) => { e.stopPropagation(); }; - } - } - const stopPropagation = (e) => e.stopPropagation(); return ( -
- ); -}); - -const DraggableBox = ({ node, setPosition, canvasRef, handleConnectStart, handleConnectEnd, connectingPort, updateNodeContent, connections, handleDeleteNode, nodes, scale, handleResize }) => { - const { id, label, position, type, color, content, format, dataOutput, dataOutputPublic, dataOutputPrivate, isProcessing, hashAlgorithm, keyAlgorithm, symAlgorithm, modulusLength, publicExponent, sourceFormat, rawInputData, p, q, e, d, n, phiN, shiftKey, keyword, vigenereMode, dStatus, n_pub, e_pub, isReadOnly, width, height, keyBase64, shiftDescription, chunk1, chunk2, convertedData, convertedFormat, isConversionExpanded, interpretAsText } = node; - const definition = NODE_DEFINITIONS[type]; - const [isDragging, setIsDragging] = useState(false); - const [isResizing, setIsResizing] = useState(false); - const boxRef = useRef(null); - const offset = useRef({ x: 0, y: 0 }); - const resizeOffset = useRef({ x: 0, y: 0 }); - const [copyStatus, setCopyStatus] = useState('Copy'); - - const isDataInput = type === 'DATA_INPUT'; - const isOutputViewer = type === 'OUTPUT_VIEWER'; - const isHashFn = type === 'HASH_FN'; - const isKeyGen = type === 'KEY_GEN'; - const isSimpleRSAKeyGen = type === 'SIMPLE_RSA_KEY_GEN'; - const isSimpleRSAPubKeyGen = type === 'SIMPLE_RSA_PUBKEY_GEN'; - const isRSAKeyGen = type === 'RSA_KEY_GEN'; - const isSimpleRSAEnc = type === 'SIMPLE_RSA_ENC'; - const isSimpleRSADec = type === 'SIMPLE_RSA_DEC'; - const isSimpleRSASign = type === 'SIMPLE_RSA_SIGN'; - const isSimpleRSAVerify = type === 'SIMPLE_RSA_VERIFY'; - const isSymEnc = type === 'SYM_ENC'; - const isSymDec = type === 'SYM_DEC'; - const isAsymEnc = type === 'ASYM_ENC'; - const isAsymDec = type === 'ASYM_DEC'; - const isBitShift = type === 'SHIFT_OP'; - const isCaesarCipher = type === 'CAESAR_CIPHER'; - const isVigenereCipher = type === 'VIGENERE_CIPHER'; - const isDataSplit = type === 'DATA_SPLIT'; - const isDataConcat = type === 'DATA_CONCAT'; - - const FORMATS = ALL_FORMATS; - const isPortSource = connectingPort?.sourceId === id; - const iconTextColorClass = TEXT_ICON_CLASSES[color] || 'text-gray-600'; - let specificClasses = ''; - if (isPortSource) specificClasses = `border-emerald-500 ring-4 ring-emerald-300 cursor-pointer animate-pulse transition duration-200`; - else specificClasses = `${BORDER_CLASSES[color]} ${HOVER_BORDER_CLASSES[color]} ${isDragging ? 'cursor-grabbing' : 'cursor-pointer hover:border-blue-500'}`; - if (isProcessing) specificClasses = `border-yellow-500 ring-4 ring-yellow-300 animate-pulse transition duration-200`; - - let requiredMinHeight = NODE_DIMENSIONS.minHeight; - if (isOutputViewer) requiredMinHeight = isConversionExpanded ? 280 : 250; - if (isBitShift || type === 'XOR_OP' || isDataSplit || isDataConcat) requiredMinHeight = 300; - const effectiveMinHeight = requiredMinHeight; - const baseClasses = `h-auto flex flex-col justify-start items-center p-3 bg-white shadow-xl rounded-xl border-4 transition duration-150 ease-in-out hover:shadow-2xl absolute select-none z-10`; - const boxStyle = { left: `${position.x}px`, top: `${position.y}px`, width: `${width}px`, minHeight: `${effectiveMinHeight}px`, height: `${height}px` }; - const contentHeightExcludingHeader = height - 50; - - const handleDragStart = useCallback((e) => { - if (connectingPort || isResizing) return; - const interactiveTags = ['TEXTAREA', 'SELECT', 'OPTION', 'BUTTON', 'INPUT']; - if (e.target.tagName === 'DIV' && e.target.classList.contains('w-4') && e.target.classList.contains('h-4')) return; - if (interactiveTags.includes(e.target.tagName)) return; - const clientX = e.clientX || (e.touches?.[0]?.clientX ?? 0); - const clientY = e.clientY || (e.touches?.[0]?.clientY ?? 0); - const canvas = canvasRef.current; - if (boxRef.current && canvas) { - const canvasRect = canvas.getBoundingClientRect(); - const unscaledMouseX = (clientX - canvasRect.left) / scale; - const unscaledMouseY = (clientY - canvasRect.y) / scale; - offset.current = { x: unscaledMouseX - position.x, y: unscaledMouseY - position.y }; - setIsDragging(true); - e.preventDefault(); - } - }, [canvasRef, position.x, position.y, connectingPort, isResizing, scale]); - - const handleDragMove = useCallback((e) => { - if (!isDragging) return; - const canvas = canvasRef.current; - if (!canvas) return; - const clientX = e.clientX || (e.touches?.[0]?.clientX ?? 0); - const clientY = e.clientY || (e.touches?.[0]?.clientY ?? 0); - const canvasRect = canvas.getBoundingClientRect(); - const unscaledMouseX = (clientX - canvasRect.left) / scale; - const unscaledMouseY = (clientY - canvasRect.y) / scale; - let newX = unscaledMouseX - offset.current.x; - let newY = unscaledMouseY - offset.current.y; - newX = Math.max(0, newX); - newY = Math.max(0, newY); - setPosition(id, { x: newX, y: newY }); - }, [isDragging, id, setPosition, canvasRef, scale]); - - const handleDragEnd = useCallback(() => setIsDragging(false), []); - - const handleResizeStart = useCallback((e) => { - e.stopPropagation(); setIsResizing(true); - const clientX = e.clientX || (e.touches?.[0]?.clientX ?? 0); - const clientY = e.clientY || (e.touches?.[0]?.clientY ?? 0); - const canvas = canvasRef.current.getBoundingClientRect(); - const unscaledMouseX = (clientX - canvas.left) / scale; - const unscaledMouseY = (clientY - canvas.y) / scale; - resizeOffset.current = { x: unscaledMouseX - (node.position.x + node.width), y: unscaledMouseY - (node.position.y + node.height) }; - }, [node.position.x, node.position.y, node.width, node.height, scale, canvasRef]); - - const handleResizeMove = useCallback((e) => { - if (!isResizing) return; - const canvas = canvasRef.current; - if (!canvas) return; - const clientX = e.clientX || (e.touches?.[0]?.clientX ?? 0); - const clientY = e.clientY || (e.touches?.[0]?.clientY ?? 0); - const canvasRect = canvas.getBoundingClientRect(); - const unscaledMouseX = (clientX - canvasRect.left) / scale; - const unscaledMouseY = (clientY - canvasRect.y) / scale; - let newWidth = unscaledMouseX - node.position.x - resizeOffset.current.x; - let newHeight = unscaledMouseY - node.position.y - resizeOffset.current.y; - handleResize(id, newWidth, newHeight); - e.preventDefault(); - }, [isResizing, id, handleResize, node.position.x, node.position.y, scale]); - - const handleResizeEnd = useCallback(() => setIsResizing(false), []); - - useEffect(() => { - const globalHandleMove = (e) => { if (isDragging) handleDragMove(e); else if (isResizing) handleResizeMove(e); }; - const globalHandleUp = (e) => { if (isDragging) handleDragEnd(e); else if (isResizing) handleResizeEnd(e); }; - if (isDragging || isResizing) { - document.addEventListener('mousemove', globalHandleMove); - document.addEventListener('mouseup', globalHandleUp); - document.addEventListener('touchmove', globalHandleMove, { passive: false }); - document.addEventListener('touchend', globalHandleUp); - } - return () => { - document.removeEventListener('mousemove', globalHandleMove); - document.removeEventListener('mouseup', globalHandleUp); - document.removeEventListener('touchmove', globalHandleMove); - document.removeEventListener('touchend', globalHandleUp); - }; - }, [isDragging, isResizing, handleDragMove, handleDragEnd, handleResizeMove, handleResizeEnd]); - - const handleBoxClick = useCallback((e) => { - if (isDragging || isResizing) return; - if (connectingPort) handleConnectEnd(null); - e.stopPropagation(); - }, [connectingPort, handleConnectEnd, isDragging, isResizing]); - - const handleCopyToClipboard = useCallback((e, textToCopy) => { - e.stopPropagation(); - if (!textToCopy || textToCopy.startsWith('ERROR')) return; - try { - const tempTextArea = document.createElement('textarea'); - tempTextArea.value = textToCopy; - tempTextArea.style.position = 'fixed'; - tempTextArea.style.left = '-9999px'; - document.body.appendChild(tempTextArea); - tempTextArea.select(); - document.execCommand('copy'); - document.body.removeChild(tempTextArea); - setCopyStatus('Copied!'); - setTimeout(() => setCopyStatus('Copy'), 1500); - } catch (err) { console.error('Failed to copy text:', err); setCopyStatus('Error'); setTimeout(() => setCopyStatus('Copy'), 2000); } - }, [setCopyStatus]); - - const renderInputPorts = () => { - if (!definition.inputPorts || definition.inputPorts.length === 0) return null; - const step = height / (definition.inputPorts.length + 1); - return definition.inputPorts.map((portDef, index) => { - const topPosition = (index + 1) * step; - const isInputConnected = connections.some(c => c.target === id && c.targetPortId === portDef.id); - return ( -
- -
- ); - }); - }; - - const renderOutputPorts = () => { - if (!definition.outputPorts || definition.outputPorts.length === 0) return null; - const step = height / (definition.outputPorts.length + 1); - return definition.outputPorts.map((portDef, index) => { - const topPosition = (index + 1) * step; - return ( -
- -
- ); - }); - }; - - return ( -
-
e.stopPropagation()} title="Resize"> -
-
- - {renderInputPorts()} {renderOutputPorts()} -
-
- {definition.icon && ()} - {label} - {isCaesarCipher && k = {node.shiftKey || 0}} - {isVigenereCipher && Keyword: {node.keyword || 'None'}} - {isSimpleRSASign && Signing (m^d mod n)} - {isSimpleRSAVerify && Verifying (s^e mod n)} - {isSimpleRSAEnc && Encryption: (c = m^e mod n)} - {isSimpleRSADec && Decryption: (m = c^d mod n)} - {isHashFn && ( -
- ALGORITHM - -
- )} - {isKeyGen && ({keyAlgorithm})} - {isSimpleRSAKeyGen && ({modulusLength} bits)} - {type === 'XOR_OP' && ({isProcessing ? 'Processing' : 'Bitwise XOR'})} - {isBitShift && ({isProcessing ? 'Processing' : (shiftDescription || 'Bit Shift')})} - {isSimpleRSAPubKeyGen && Public Key Output} - {isDataSplit && Split by: Character/Hex/Bit} - {isDataConcat && Concatenation: Data A + Data B} -
- - {isDataInput && ( -
-