-
Notifications
You must be signed in to change notification settings - Fork 3
/
encoder.html
221 lines (212 loc) · 5.12 KB
/
encoder.html
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<html>
<script type="text/javascript" src="php.default.min.js"></script>
<script type="text/javascript">
function makeImg()
{
var outerImg=document.getElementById("img");
if(!outerImg.files[0])
{
return;
}
var imgdat=outerImg.files[0];
var reader = new FileReader();
reader.onload=function(event)
{
var img=new Image;
img.onload=function()
{
getFile(this);
}
img.src=event.target.result;
}
reader.readAsDataURL(imgdat);
}
function getFile(img)
{
var innerFile=document.getElementById("file");
if(!innerFile.files[0])
{
return;
}
innerFile=innerFile.files[0];
var reader = new FileReader();
reader.readAsBinaryString(innerFile);
reader.onload=function(event)
{
doThings(img,event.target.result,innerFile.name);
}
}
function getLengthInChars(length)
{
var bigPart=length & (~255);
var tempSmallPart=length & (~bigPart);
bigPart=bigPart >> 8;
var buf=String.fromCharCode(tempSmallPart);
while(bigPart>255)
{
var tempLen=bigPart;
bigPart=bigPart & (~255);
tempSmallPart=tempLen & (~bigPart);
bigPart=bigPart >> 8;
buf=buf+String.fromCharCode(tempSmallPart);
}
buf=buf+String.fromCharCode(bigPart);
if(buf.length<=8)
{
var end=8-buf.length;
for(var x=0;x<end;x++)
{
buf=buf+String.fromCharCode(0);
}
}
else
{
return "";
}
return buf;
}
function typedSplice(array,index,length)
{
var newlength=array.length-length;
var newtypedarr=new Uint32Array(newlength);
newtypedarr.set(array.subarray(0,index),0);
newtypedarr.set(array.subarray(index+length),index);
return newtypedarr;
}
// From http://baagoe.com/en/RandomMusings/javascript/
// Johannes Baagøe <baagoe@baagoe.com>, 2010
function Mash() {
var n = 0xefc8249d;
var mash = function(data) {
data = data.toString();
for (var i = 0; i < data.length; i++) {
n += data.charCodeAt(i);
var h = 0.02519603282416938 * n;
n = h >>> 0;
h -= n;
h *= n;
n = h >>> 0;
h -= n;
n += h * 0x100000000; // 2^32
}
return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
};
mash.version = 'Mash 0.9';
return mash;
}
function Alea() {
return (function(args) {
// Johannes Baagøe <baagoe@baagoe.com>, 2010
var s0 = 0;
var s1 = 0;
var s2 = 0;
var c = 1;
if (args.length == 0) {
args = [+new Date];
}
var mash = Mash();
s0 = mash(' ');
s1 = mash(' ');
s2 = mash(' ');
for (var i = 0; i < args.length; i++) {
s0 -= mash(args[i]);
if (s0 < 0) {
s0 += 1;
}
s1 -= mash(args[i]);
if (s1 < 0) {
s1 += 1;
}
s2 -= mash(args[i]);
if (s2 < 0) {
s2 += 1;
}
}
mash = null;
var random = function() {
var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32
s0 = s1;
s1 = s2;
return s2 = t - (c = t | 0);
};
random.uint32 = function() {
return random() * 0x100000000; // 2^32
};
random.fract53 = function() {
return random() +
(random() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
};
random.version = 'Alea 0.9';
random.args = args;
return random;
} (Array.prototype.slice.call(arguments)));
};
function doThings(img,file,fileName)
{
var fileLen=file.length;
var nameLen=fileName.length;
if(nameLen>249)
{
nameLen=255;
fileName=fileName.substr(0,249)+"~"+fileName.substr(-5);
}
var canvas = null,
ctx = null,
canvas = document.createElement("canvas");
// the canvas should not be visible
canvas.style.display = "none";
var b = document.getElementsByTagName("body")[0];
b.appendChild(canvas);
ctx = canvas.getContext("2d");
//var img = contextNode;
// apply the width and height to the canvas element
canvas.width = img.width;
canvas.height = img.height;
// draw the image into the canvas element
ctx.drawImage(img, 0, 0);
var image = ctx.getImageData(0, 0, canvas.width, canvas.height);
var imageData = image.data;
var length = imageData.length;
var newData= new Array();
var width = canvas.width;
var pass=prompt("please enter a password");
var rng = new Alea(pass);
var redIndicies=[];
for(var i = 0; i<length; i++)
{
newData[i]=i;
}
var bytes=String.fromCharCode(nameLen)+fileName+getLengthInChars(fileLen)+file;
if(imageData.length/4<bytes.length)
{
alert("innerFile too big")
return;
}
for(var i = 0; i <= bytes.length; i++){
var randnum=Math.floor(rng()*newData.length);
var redIndex=randnum-(randnum%4);
if(newData[redIndex]==-1)
{
i--;
continue;
}
//redIndicies.push(redIndex);
imageData[redIndex]=((imageData[redIndex] & (~7)) | ((bytes.charCodeAt(i)>>5)&7))
imageData[redIndex+1]=((imageData[redIndex+1] & (~3)) | ((bytes.charCodeAt(i)>>3)&3))
imageData[redIndex+2]=((imageData[redIndex+2] & (~7)) | ((bytes.charCodeAt(i))&7))
newData[redIndex]=-1;
}
//alert(redIndicies);
image.data=imageData;
ctx.putImageData(image,0,0);
canvas.style.display="";
}
</script>
<body>
<p>select Outer Image(PNG format):</p>
<p><input type="file" id="img" accept="image/*"></p>
<p>select innerFile:</p>
<p><input type="file" id="file"></p>
<button onclick="makeImg()">Do it!</button>
</body>
</html>