Spaces:
Sleeping
Sleeping
Create decoder.js
Browse files- decoder.js +58 -0
decoder.js
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const { webcrypto } = require('crypto');
|
2 |
+
const sharp = require('sharp');
|
3 |
+
|
4 |
+
function pemToDer(pem) {
|
5 |
+
const b64 = pem.replace(/-----BEGIN PRIVATE KEY-----/, '').replace(/-----END PRIVATE KEY-----/, '').replace(/\\n/g, '').replace(/\s/g, '');
|
6 |
+
const binaryDer = atob(b64);
|
7 |
+
const buffer = new ArrayBuffer(binaryDer.length);
|
8 |
+
const bytes = new Uint8Array(buffer);
|
9 |
+
for (let i = 0; i < binaryDer.length; i++) { bytes[i] = binaryDer.charCodeAt(i); }
|
10 |
+
return buffer;
|
11 |
+
}
|
12 |
+
|
13 |
+
async function decodeFromImageBuffer(imageBuffer, privateKeyPem) {
|
14 |
+
// 1. Extract data from image using Sharp
|
15 |
+
const { data: pixelData } = await sharp(imageBuffer).raw().toBuffer({ resolveWithObject: true });
|
16 |
+
const HEADER_BITS = 32;
|
17 |
+
let headerBinaryString = '';
|
18 |
+
for (let i = 0; headerBinaryString.length < HEADER_BITS; i++) {
|
19 |
+
const pixelIndex = i * 4;
|
20 |
+
if (pixelData.length < pixelIndex + 3) throw new Error("Image too small for header.");
|
21 |
+
headerBinaryString += pixelData[pixelIndex] & 1;
|
22 |
+
if (headerBinaryString.length < HEADER_BITS) headerBinaryString += pixelData[pixelIndex + 1] & 1;
|
23 |
+
if (headerBinaryString.length < HEADER_BITS) headerBinaryString += pixelData[pixelIndex + 2] & 1;
|
24 |
+
}
|
25 |
+
const headerBytes = new Uint8Array(HEADER_BITS / 8);
|
26 |
+
for (let i = 0; i < HEADER_BITS / 8; i++) { headerBytes[i] = parseInt(headerBinaryString.substring(i * 8, (i + 1) * 8), 2); }
|
27 |
+
const dataLengthInBytes = new DataView(headerBytes.buffer).getUint32(0, false);
|
28 |
+
if (dataLengthInBytes === 0) return {};
|
29 |
+
|
30 |
+
const dataLengthInBits = dataLengthInBytes * 8;
|
31 |
+
const bitOffset = HEADER_BITS;
|
32 |
+
let dataBinaryString = '';
|
33 |
+
for (let i = 0; dataBinaryString.length < dataLengthInBits; i++) {
|
34 |
+
const bitPosition = bitOffset + i;
|
35 |
+
const pixelIndex = Math.floor(bitPosition / 3) * 4;
|
36 |
+
const channelOffset = bitPosition % 3;
|
37 |
+
if (pixelData.length < pixelIndex + channelOffset) throw new Error("Image data is corrupt or truncated.");
|
38 |
+
dataBinaryString += pixelData[pixelIndex + channelOffset] & 1;
|
39 |
+
}
|
40 |
+
const cryptoPayload = new Uint8Array(dataLengthInBytes);
|
41 |
+
for (let i = 0; i < dataLengthInBytes; i++) { cryptoPayload[i] = parseInt(dataBinaryString.substring(i * 8, (i + 1) * 8), 2); }
|
42 |
+
|
43 |
+
// 2. Decrypt payload using Web Crypto API
|
44 |
+
const privateKey = await webcrypto.subtle.importKey('pkcs8', pemToDer(privateKeyPem), { name: 'RSA-OAEP', hash: 'SHA-256' }, true, ['decrypt']);
|
45 |
+
const encryptedAesKeyLen = new DataView(cryptoPayload.buffer, 0, 4).getUint32(0, false);
|
46 |
+
let offset = 4;
|
47 |
+
const encryptedAesKey = cryptoPayload.slice(offset, offset + encryptedAesKeyLen);
|
48 |
+
offset += encryptedAesKeyLen;
|
49 |
+
const nonce = cryptoPayload.slice(offset, offset + 12);
|
50 |
+
offset += 12;
|
51 |
+
const ciphertextWithTag = cryptoPayload.slice(offset);
|
52 |
+
const decryptedAesKeyBytes = await webcrypto.subtle.decrypt({ name: 'RSA-OAEP' }, privateKey, encryptedAesKey);
|
53 |
+
const aesKey = await webcrypto.subtle.importKey('raw', decryptedAesKeyBytes, { name: 'AES-GCM', length: 256 }, true, ['decrypt']);
|
54 |
+
const decryptedDataBuffer = await webcrypto.subtle.decrypt({ name: 'AES-GCM', iv: nonce }, aesKey, ciphertextWithTag);
|
55 |
+
return JSON.parse(new TextDecoder().decode(decryptedDataBuffer));
|
56 |
+
}
|
57 |
+
|
58 |
+
module.exports = { decodeFromImageBuffer };
|