broadfield-dev commited on
Commit
6a04a72
·
verified ·
1 Parent(s): 423febb

Delete server

Browse files
Files changed (4) hide show
  1. server/.env +0 -1
  2. server/decoder.js +0 -114
  3. server/package.json +0 -20
  4. server/server.js +0 -56
server/.env DELETED
@@ -1 +0,0 @@
1
- PLUGIN_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDdTXZuWytZyimJ\n2iNWbNlx1uT47BEBtTV0nKuVxxJDJ6NdIJ4FmN7FU7Xb1TIE/6779OxBKnu3azcO\nhFpKrqxfybExVzBgoH/pQ+YSLAEhEa50OAzMccRXNibx/OclLBmv6SPijH6uTbGi\nkHrjAhsSoZpj8YLUleb7x1FSHyH/lk7F+1FeYgXbHA+NMRXuMw4YiEKA9DQl/7K4\nB8aaPX/Uv2y7klb5TOAJuJiQNZxxSqdrjIGl2PJuBeztmFtFAyOrJqRL+qdeUtKH\n8W8P86LFv1SIGlElLeOZMyepthTX0+KBWSbuSljTO5umLKnanxQvs44OrGdYJ62N\nDpve3ItTAgMBAAECggEALLyqmt+KUXH5WLlE2IVTpdxD2IBcZcR8pHDBjRs7uWCr\neTmNSGNhcXGdO24LWRBZDu0xz3CnpuohLKmb2SobhZev3uIZ0QbRJNZJjdXgeo9o\n/WyRRckf4GN9lwDBt3WFUdbnHra5K0JgjRT5CqgT0f3iSJ77jD8rfS2LZncNt+2J\n4I41vKlE8/A56XqVkEFqcxuaAOD5aWTZ7Ok8C308i+nP+0WZSUwOCwOxtaoHNhqX\nDAPU1kVkFm1HECsEeh0h1AJNw72te3BzvSawnv/MzGFHVrXLzRCyMpMu3cb6Tfpc\nkvUmPcVqOn0Vs1GtkYyQO9ZA7es4+BGgYbMyyCmpuQKBgQD88rUhB7vSjhVlVFFm\nveETEKQYNUNmz1Z/4ixrZ/yRJsTcgIthB8eMed9sbdgxbQyM+5D431q4qfGD6+Zd\ncyHi5N9BgDKqaP6wM/eOnxQZzhyzI6xwtuROh/X2wge8gasTvK7zLY4Tp8QwDmyO\nsk9ax+tcKKv/x7UqZXakAqfqWQKBgQDf+QKcYCL8WiIYw8bc+kFKkEgBNK/l4ZZ6\nZwlk3PTsw4RUDqt5oZ0lIKZeONeyLi/XC/C8UTyvWXzBF1/CiNZJ0xjZ2nyxJHpX\nfjyabiP4nJ9rKaVuD51fBA2tU4GNiwsHr/woFVaBhGdoVrY0ix3iiUTXkeytkM2N\npn7ZRk8ViwKBgBQxjCRG6ce1cB/Wy2jYX8+V1IPaPLdIdi9fbwxkENXFspGn8Xid\nlmCwmhWd71SC0C2xJ7G2oxd6+bz+aDWtq1bhzrD7+w9/slV+0JSiPoiQ29mxT2Lw\nNunOEOTSOKvj7jR1K8JeMKmBCziK7j+2HH+y1idVV+Xjm6L21rHhRGaBAoGBAMzO\nruWCP9vVLCLLPBksOvJNN5rqrAKho/HwMLXsG4g6TdJzy7gg4ktmLNwyTIOiGpsi\nTxRVkRGj2iHUFvwUFyhRAgOM+MMf4UQKqq5pEzmvN1ROM/mOC9EsKrmZh/l/8Skp\nc4LP6FftVIYj7N/lawGRlsMtrGkW5fCnFI3o7j57AoGAcBrZrt4F3iSFV5nW2vWd\n0KtZHTO97zjG9tOJ6m9STyqaREt5sxnXf/FVERSkqlCrZhIKAiEiERODuHcaQMHZ\nilZoDRgvqoScP7LOCMB3SjqPX9cU6p1Mt188Zr+e/LhYnnkIg1yS5yAqIlznlQWB\nlYc6JH6EG1RkZx0hB4CmWqQ=\n-----END PRIVATE KEY-----"
 
 
server/decoder.js DELETED
@@ -1,114 +0,0 @@
1
- const sharp = require('sharp');
2
- const { webcrypto } = require('crypto');
3
-
4
- // --- Helper to convert PEM to a format Web Crypto API can use ---
5
- function pemToDer(pem) {
6
- const b64 = pem
7
- .replace(/-----BEGIN PRIVATE KEY-----/g, '')
8
- .replace(/-----END PRIVATE KEY-----/g, '')
9
- .replace(/\\n/g, '') // Handle escaped newlines from env vars
10
- .replace(/\s/g, '');
11
- const binaryDer = atob(b64);
12
- const buffer = new ArrayBuffer(binaryDer.length);
13
- const bytes = new Uint8Array(buffer);
14
- for (let i = 0; i < binaryDer.length; i++) {
15
- bytes[i] = binaryDer.charCodeAt(i);
16
- }
17
- return buffer;
18
- }
19
-
20
- // --- Extracts hidden data from an image buffer using Sharp ---
21
- async function extractDataFromImage(imageBuffer) {
22
- const { data: pixelData, info } = await sharp(imageBuffer).raw().toBuffer({ resolveWithObject: true });
23
-
24
- const HEADER_BITS = 32; // 4 bytes for the length header
25
- const channels = info.channels; // Typically 3 (RGB) or 4 (RGBA)
26
-
27
- if (pixelData.length < Math.ceil(HEADER_BITS / channels) * channels) {
28
- throw new Error("Image is too small to contain a valid header.");
29
- }
30
-
31
- let headerBinaryString = '';
32
- for (let i = 0; i < HEADER_BITS; i++) {
33
- const pixelIndex = Math.floor(i / 3);
34
- const channelIndex = i % 3;
35
- headerBinaryString += pixelData[pixelIndex * channels + channelIndex] & 1;
36
- }
37
-
38
- const headerBytes = new Uint8Array(HEADER_BITS / 8);
39
- for (let i = 0; i < HEADER_BITS / 8; i++) {
40
- headerBytes[i] = parseInt(headerBinaryString.substring(i * 8, (i + 1) * 8), 2);
41
- }
42
-
43
- const dataView = new DataView(headerBytes.buffer);
44
- const dataLengthInBytes = dataView.getUint32(0, false);
45
-
46
- if (dataLengthInBytes === 0) return new Uint8Array(0);
47
-
48
- const dataLengthInBits = dataLengthInBytes * 8;
49
- const bitOffset = HEADER_BITS;
50
- const totalBitsToRead = bitOffset + dataLengthInBits;
51
-
52
- if (pixelData.length < Math.ceil(totalBitsToRead / 3) * channels) {
53
- throw new Error("Image is too small for the data specified in the header. File may be corrupt.");
54
- }
55
-
56
- let dataBinaryString = '';
57
- for (let i = 0; i < dataLengthInBits; i++) {
58
- const bitPosition = bitOffset + i;
59
- const pixelIndex = Math.floor(bitPosition / 3);
60
- const channelIndex = bitPosition % 3;
61
- dataBinaryString += pixelData[pixelIndex * channels + channelIndex] & 1;
62
- }
63
-
64
- const dataBytes = new Uint8Array(dataLengthInBytes);
65
- for (let i = 0; i < dataLengthInBytes; i++) {
66
- dataBytes[i] = parseInt(dataBinaryString.substring(i * 8, (i + 1) * 8), 2);
67
- }
68
-
69
- return dataBytes;
70
- }
71
-
72
- // --- Decrypts the payload using RSA and AES ---
73
- async function decryptHybridPayload(cryptoPayload, privateKeyPem) {
74
- const ENCRYPTED_AES_KEY_LEN_SIZE = 4;
75
- const AES_GCM_NONCE_SIZE = 12;
76
-
77
- const privateKey = await webcrypto.subtle.importKey(
78
- 'pkcs8', pemToDer(privateKeyPem), { name: 'RSA-OAEP', hash: 'SHA-256' }, true, ['decrypt']
79
- );
80
-
81
- const encryptedAesKeyLen = new DataView(cryptoPayload.buffer, 0, ENCRYPTED_AES_KEY_LEN_SIZE).getUint32(0, false);
82
- let offset = ENCRYPTED_AES_KEY_LEN_SIZE;
83
- const encryptedAesKey = cryptoPayload.slice(offset, offset + encryptedAesKeyLen);
84
- offset += encryptedAesKeyLen;
85
- const nonce = cryptoPayload.slice(offset, offset + AES_GCM_NONCE_SIZE);
86
- offset += AES_GCM_NONCE_SIZE;
87
- const ciphertextWithTag = cryptoPayload.slice(offset);
88
-
89
- const decryptedAesKeyBytes = await webcrypto.subtle.decrypt({ name: 'RSA-OAEP' }, privateKey, encryptedAesKey);
90
- const aesKey = await webcrypto.subtle.importKey('raw', decryptedAesKeyBytes, { name: 'AES-GCM', length: 256 }, true, ['decrypt']);
91
- const decryptedDataBuffer = await webcrypto.subtle.decrypt({ name: 'AES-GCM', iv: nonce }, aesKey, ciphertextWithTag);
92
-
93
- const decryptedText = new TextDecoder().decode(decryptedDataBuffer);
94
- return JSON.parse(decryptedText);
95
- }
96
-
97
- // --- Main export function ---
98
- async function decodeAuthFromImage(imageBuffer, privateKeyPem) {
99
- try {
100
- const cryptoPayload = await extractDataFromImage(imageBuffer);
101
- if (cryptoPayload.length === 0) {
102
- throw new Error("No data found in image.");
103
- }
104
- return await decryptHybridPayload(cryptoPayload, privateKeyPem);
105
- } catch (error) {
106
- console.error("Decoding failed:", error);
107
- if (error.message.includes('decryption failed')) {
108
- throw new Error("Decryption failed. The data may be corrupt or was not encrypted with the correct public key.");
109
- }
110
- throw error;
111
- }
112
- }
113
-
114
- module.exports = { decodeAuthFromImage };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
server/package.json DELETED
@@ -1,20 +0,0 @@
1
-
2
- {
3
- "name": "keylock-decoder-server",
4
- "version": "1.0.0",
5
- "description": "Server-side API to decode KeyLock images.",
6
- "main": "server.js",
7
- "scripts": {
8
- "start": "node server.js",
9
- "test": "echo \"Error: no test specified\" && exit 1"
10
- },
11
- "author": "",
12
- "license": "ISC",
13
- "dependencies": {
14
- "cors": "^2.8.5",
15
- "dotenv": "^16.4.5",
16
- "express": "^4.19.2",
17
- "multer": "^1.4.5-lts.1",
18
- "sharp": "^0.33.3"
19
- }
20
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
server/server.js DELETED
@@ -1,56 +0,0 @@
1
- require('dotenv').config();
2
- const express = require('express');
3
- const multer = require('multer');
4
- const cors = require('cors');
5
- const { decodeAuthFromImage } = require('./decoder');
6
-
7
- const app = express();
8
- const port = process.env.PORT || 7860;
9
-
10
- // --- CORS Configuration ---
11
- const allowedOrigins = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
12
- const corsOptions = {
13
- origin: (origin, callback) => {
14
- // Allow requests with no origin (like mobile apps or curl) and from allowed origins
15
- if (!origin || allowedOrigins.indexOf(origin) !== -1 || allowedOrigins.includes('*')) {
16
- callback(null, true);
17
- } else {
18
- callback(new Error('Not allowed by CORS'));
19
- }
20
- },
21
- };
22
- app.use(cors(corsOptions));
23
-
24
- // --- Multer for file uploads ---
25
- const upload = multer({
26
- storage: multer.memoryStorage(),
27
- limits: { fileSize: 5 * 1024 * 1024 }, // 5MB limit
28
- });
29
-
30
- // --- API Endpoint ---
31
- app.post('/api/decode', upload.single('authImage'), async (req, res) => {
32
- const privateKey = process.env.PLUGIN_PRIVATE_KEY;
33
-
34
- if (!privateKey) {
35
- console.error("FATAL: PLUGIN_PRIVATE_KEY environment variable is not set.");
36
- return res.status(500).json({ success: false, error: 'Server configuration error.' });
37
- }
38
-
39
- if (!req.file) {
40
- return res.status(400).json({ success: false, error: 'No image file provided. Please upload a file with the key "authImage".' });
41
- }
42
-
43
- try {
44
- const credentials = await decodeAuthFromImage(req.file.buffer, privateKey);
45
- res.json({ success: true, data: credentials });
46
- } catch (error) {
47
- res.status(400).json({ success: false, error: error.message });
48
- }
49
- });
50
-
51
- app.listen(port, () => {
52
- console.log(`KeyLock Decoder Plugin listening at http://localhost:${port}`);
53
- if (allowedOrigins.length > 0) {
54
- console.log(`CORS enabled for: ${allowedOrigins.join(', ')}`);
55
- }
56
- });