Repository navigation
Expand file tree
/
Copy pathsteg.html
More file actions
93 lines (93 loc) · 9.17 KB
/
Copy pathsteg.html
File metadata and controls
93 lines (93 loc) · 9.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="brand/icon-32.png"><link rel="apple-touch-icon" href="brand/icon-180.png"><title>Comb Lab · Hide text in an image</title>
<!-- Comb Code, Copyright (c) 2026 Avi Shor. Free to use, with or without credit. See LICENSE.md. No warranty. -->
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'self' 'unsafe-inline' 'wasm-unsafe-eval'; style-src 'unsafe-inline'; img-src 'self' blob: data:">
<style>
*{box-sizing:border-box}body{margin:0;font:16px/1.5 system-ui,-apple-system,Segoe UI,Roboto,sans-serif;color:#111;background:#fff}
main{max-width:640px;margin:0 auto;padding:24px 18px 60px}h1{font-size:26px;margin:8px 0 4px}p.s{color:#555;margin:0 0 20px}
.tabs{display:flex;gap:6px;margin:16px 0}.tabs button{flex:1;padding:10px;border:1px solid #ddd;background:#f6f6f8;border-radius:10px;font:inherit;cursor:pointer}
.tabs button[aria-selected=true]{background:#111;color:#fff;border-color:#111}
section[hidden]{display:none}label{display:block;font-weight:600;margin:16px 0 6px}
input[type=file]{width:100%;padding:10px;border:1px dashed #bbb;border-radius:10px;background:#fafafa}
textarea{width:100%;min-height:120px;padding:10px;border:1px solid #ccc;border-radius:10px;font:inherit;resize:vertical}
button.p{margin-top:14px;padding:12px 18px;border:0;border-radius:10px;background:#2b5cff;color:#fff;font:inherit;font-weight:600;cursor:pointer}
button.p:disabled{background:#b9c5f5;cursor:default}
.info{color:#555;font-size:14px;margin-top:6px}.err{color:#b3261e}.ok{color:#17692f}
canvas,img.pv{max-width:100%;height:auto;margin-top:12px;border:1px solid #eee;border-radius:10px;display:block}
.bar{height:6px;background:#eee;border-radius:3px;margin-top:6px;overflow:hidden}.bar i{display:block;height:100%;background:#2b5cff;width:0;transition:width .25s}
.out{white-space:pre-wrap;word-break:break-word;padding:12px;border:1px solid #ddd;border-radius:10px;background:#fafafa;margin-top:12px}
a.dl{display:inline-block;margin-top:14px;padding:12px 18px;border-radius:10px;background:#111;color:#fff;text-decoration:none;font-weight:600}
</style></head><body><main>
<h1>Hide text in an image</h1><p class="s">Everything runs on your device. Nothing is uploaded. Output is a PNG, so the hidden text survives exactly.</p>
<div class="tabs" role="tablist"><button role="tab" id="t1" aria-selected="true">Hide</button><button role="tab" id="t2" aria-selected="false">Reveal</button></div>
<section id="enc">
<label for="fi">Cover image</label><input type="file" id="fi" accept="image/*,.heic,.heif">
<div class="info" id="cap">Pick an image to see how much text fits.</div>
<label for="tx">Secret text</label><textarea id="tx" placeholder="Type your message"></textarea>
<div class="bar"><i id="bar"></i></div><div class="info" id="used"></div>
<button class="p" id="go" disabled>Hide text and make PNG</button><div class="info" id="em"></div><div id="eo"></div>
</section>
<section id="dec" hidden>
<label for="fd">Image with hidden text</label><input type="file" id="fd" accept="image/png">
<div class="info">Use the PNG made here. JPEG or re-compressed copies lose the text.</div>
<div id="dout"></div>
</section>
<p class="info" style="margin-top:34px">Method: the lowest bit of each red, green and blue value holds one bit of the message, behind a small header and a checksum. The picture looks the same to the eye. This hides text, it does not encrypt it. Transparent pixels are made opaque. Build 0.1.0</p>
</main>
<script src="comb-heic.js"></script>
<script>
(function(){'use strict';
var MAGIC=[0x53,0x54,0x47,0x31],HDR=8,TRL=4; // magic(4) + length(4) + data + crc32(4)
var $=function(i){return document.getElementById(i)};
var crcT=(function(){var t=new Uint32Array(256);for(var n=0;n<256;n++){var c=n;for(var k=0;k<8;k++)c=c&1?0xEDB88320^(c>>>1):c>>>1;t[n]=c>>>0}return t})();
function crc32(b){var c=0xFFFFFFFF;for(var i=0;i<b.length;i++)c=crcT[(c^b[i])&255]^(c>>>8);return(c^0xFFFFFFFF)>>>0}
function capacity(w,h){return Math.max(0,Math.floor(w*h*3/8)-HDR-TRL)}
function frame(bytes){var n=bytes.length,o=new Uint8Array(HDR+n+TRL),c;o.set(MAGIC,0);
o[4]=n>>>24&255;o[5]=n>>>16&255;o[6]=n>>>8&255;o[7]=n&255;o.set(bytes,HDR);c=crc32(bytes);
o[HDR+n]=c>>>24&255;o[HDR+n+1]=c>>>16&255;o[HDR+n+2]=c>>>8&255;o[HDR+n+3]=c&255;return o}
// embed framed bytes into RGBA pixel array (in place); alpha forced to 255
function embed(px,w,h,bytes){var f=frame(bytes),bits=f.length*8,total=w*h*3;
if(bits>total)throw new Error('Too much text for this image');
var bi=0,p;for(p=0;p<px.length;p+=4){px[p+3]=255}
for(p=0;p<px.length&&bi<bits;p+=4){for(var ch=0;ch<3&&bi<bits;ch++,bi++){
var bit=f[bi>>3]>>(7-(bi&7))&1;px[p+ch]=(px[p+ch]&0xFE)|bit}}
return px}
function readBits(px,startBit,nBytes){var o=new Uint8Array(nBytes),nb=nBytes*8;
for(var i=0;i<nb;i++){var g=startBit+i,pi=Math.floor(g/3),ch=g%3,bit=px[pi*4+ch]&1;o[i>>3]|=bit<<(7-(i&7))}return o}
function extract(px,w,h){var total=w*h*3;if(total<(HDR+TRL)*8)throw new Error('Image too small to hold hidden text');
var hd=readBits(px,0,HDR);for(var i=0;i<4;i++)if(hd[i]!==MAGIC[i])throw new Error('No hidden text found. Use the PNG made by this tool, unedited.');
var n=((hd[4]<<24)|(hd[5]<<16)|(hd[6]<<8)|hd[7])>>>0;
if((HDR+n+TRL)*8>total)throw new Error('Hidden text header is damaged');
var all=readBits(px,0,HDR+n+TRL),data=all.subarray(HDR,HDR+n),c=((all[HDR+n]<<24)|(all[HDR+n+1]<<16)|(all[HDR+n+2]<<8)|all[HDR+n+3])>>>0;
if(crc32(data)!==c)throw new Error('Checksum failed. The image was changed after hiding (resized or re-compressed).');
return new TextDecoder('utf-8',{fatal:true}).decode(data)}
window.STEG={capacity:capacity,embed:embed,extract:extract,crc32:crc32};
function load(file){return (window.COMBHEIC?COMBHEIC.open(file):Promise.resolve(null)).then(function(h){if(h)return h;return new Promise(function(res,rej){var u=URL.createObjectURL(file),im=new Image();
im.onload=function(){URL.revokeObjectURL(u);res(im)};im.onerror=function(){URL.revokeObjectURL(u);rej(new Error('Could not read that image. Use a JPEG, PNG or HEIC photo.'))};im.src=u})})}
function toCanvas(im,flat){var c=document.createElement('canvas');c.width=im.naturalWidth;c.height=im.naturalHeight;
var x=c.getContext('2d',{willReadFrequently:true});if(flat){x.fillStyle='#fff';x.fillRect(0,0,c.width,c.height)}x.drawImage(im,0,0);return c}
var cover=null,url=null;
function setTab(a){$('enc').hidden=!a;$('dec').hidden=a;$('t1').setAttribute('aria-selected',a);$('t2').setAttribute('aria-selected',!a)}
$('t1').onclick=function(){setTab(true)};$('t2').onclick=function(){setTab(false)};
function refresh(){var n=new TextEncoder().encode($('tx').value).length,cap=cover?capacity(cover.w,cover.h):0;
$('bar').style.width=(cap?Math.min(100,n/cap*100):0)+'%';
if(!cover){$('used').textContent='';$('go').disabled=true;return}
var over=n>cap;$('used').innerHTML=(over?'<span class="err">':'')+n.toLocaleString()+' of '+cap.toLocaleString()+' bytes used'+(over?'. Too long for this image: shorten the text or use a larger image.</span>':'');
$('go').disabled=over||n===0}
$('fi').onchange=function(){var f=this.files[0];$('eo').innerHTML='';$('em').textContent='';cover=null;refresh();if(!f){$('cap').textContent='Pick an image to see how much text fits.';return}
load(f).then(function(im){cover={im:im,w:im.naturalWidth,h:im.naturalHeight,name:f.name};
$('cap').textContent=cover.w+' x '+cover.h+' px. Room for about '+capacity(cover.w,cover.h).toLocaleString()+' bytes of text (plain English is about 1 byte per letter; emoji and other scripts use 2 to 4).';refresh()}).catch(function(e){$('cap').innerHTML='<span class="err">'+e.message+'</span>'})};
$('tx').oninput=refresh;
$('go').onclick=function(){if(!cover)return;$('em').textContent='Working...';$('eo').innerHTML='';var b=$('go');b.disabled=true;
setTimeout(function(){try{var bytes=new TextEncoder().encode($('tx').value),c=toCanvas(cover.im,true),x=c.getContext('2d'),d=x.getImageData(0,0,c.width,c.height);
embed(d.data,c.width,c.height,bytes);x.putImageData(d,0,0);
c.toBlob(function(bl){if(url)URL.revokeObjectURL(url);url=URL.createObjectURL(bl);
var nm=(cover.name||'image').replace(/\.[^.]*$/,'')+'-hidden.png';
$('em').innerHTML='<span class="ok">Done. '+bytes.length.toLocaleString()+' bytes hidden.</span> Save the PNG as is. Sharing apps that compress photos will destroy the text; send it as a file.';
$('eo').innerHTML='<img class="pv" alt="Encoded image preview" src="'+url+'"><a class="dl" download="'+nm.replace(/"/g,'')+'" href="'+url+'">Download PNG</a>';refresh()},'image/png')}
catch(e){$('em').innerHTML='<span class="err">'+e.message+'</span>';refresh()}},30)};
$('fd').onchange=function(){var f=this.files[0],o=$('dout');o.innerHTML='';if(!f)return;
load(f).then(function(im){var c=toCanvas(im,false),d=c.getContext('2d',{willReadFrequently:true}).getImageData(0,0,c.width,c.height);
var t=extract(d.data,c.width,c.height);o.innerHTML='<div class="info ok">Hidden text found ('+new TextEncoder().encode(t).length.toLocaleString()+' bytes):</div><div class="out"></div>';o.lastChild.textContent=t})
.catch(function(e){o.innerHTML='<div class="info err"></div>';o.firstChild.textContent=e.message})};
})();
</script></body></html>