-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
254 lines (213 loc) · 7.87 KB
/
main.js
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// initialize
window.onload = function() {
// add action to the file input
var input = document.getElementById('img');
input.addEventListener('change', importImage);
// add action to the encode button
var encodeButton = document.getElementById('encode');
encodeButton.addEventListener('click', encode);
// add action to the decode button
var decodeButton = document.getElementById('decode');
decodeButton.addEventListener('click', decode);
document.getElementById('secretd').innerHTML = '';
};
// artificially limit the message size
var maxMessageSize = 1000;
// put image in the canvas and display it
var importImage = function(e) {
var reader = new FileReader();
reader.onload = function(event) {
// wipe all the fields clean
document.getElementById('secrete').value = '';
document.getElementById('password').value = '';
document.getElementById('password').value = '';
document.getElementById('secretd').innerHTML = '';
// read the data into the canvas element
var img = new Image();
img.onload = function() {
var ctx = document.getElementById('canvas').getContext('2d');
ctx.canvas.width = img.width;
ctx.canvas.height = img.height;
ctx.drawImage(img, 0, 0);
decode();
};
img.src = event.target.result;
};
reader.readAsDataURL(e.target.files[0]);
};
// encode the image and save it
var encode = function() {
var message = document.getElementById('secrete').value;
var password = document.getElementById('password').value;
var output = document.getElementById('output');
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
// encrypt the message with supplied password if necessary
if (password.length > 0) {
message = sjcl.encrypt(password, message);
} else {
message = JSON.stringify({'text': message});
}
// exit early if the message is too big for the image
var pixelCount = ctx.canvas.width * ctx.canvas.height;
if ((message.length + 1) * 16 > pixelCount * 4 * 0.75) {
alert('Message is too big for the image.');
return;
}
// exit early if the message is above an artificial limit
if (message.length > maxMessageSize) {
alert('Message is too big.');
return;
}
// encode the encrypted message with the supplied password
var imgData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
encodeMessage(imgData.data, sjcl.hash.sha256.hash(password), message);
ctx.putImageData(imgData, 0, 0);
// view the new image
alert('Done! When the image appears, save and share it with someone.');
output.src = canvas.toDataURL();
};
// decode the image and display the contents if there is anything
var decode = function() {
var password = document.getElementById('password').value;
var passwordFail = 'Password is incorrect or there is nothing here.';
// decode the message with the supplied password
var ctx = document.getElementById('canvas').getContext('2d');
var imgData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
var message = decodeMessage(imgData.data, sjcl.hash.sha256.hash(password));
// try to parse the JSON
var obj = null;
try {
obj = JSON.parse(message);
} catch (e) {
if (password.length > 0) {
alert(passwordFail);
}
}
// display the "reveal" view
if (obj) {
// decrypt if necessary
if (obj.ct) {
try {
obj.text = sjcl.decrypt(password, message);
} catch (e) {
alert(passwordFail);
}
}
// escape special characters
var escChars = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
'\'': ''',
'/': '/',
'\n': '<br/>'
};
var escHtml = function(string) {
return String(string).replace(/[&<>"'\/\n]/g, function (c) {
return escChars[c];
});
};
document.getElementById('secretd').innerHTML = escHtml(obj.text);
document.getElementById('secretz').innerHTML = '';
}
};
// returns a 1 or 0 for the bit in 'location'
var getBit = function(number, location) {
return ((number >> location) & 1);
};
// sets the bit in 'location' to 'bit' (either a 1 or 0)
var setBit = function(number, location, bit) {
return (number & ~(1 << location)) | (bit << location);
};
// returns an array of 1s and 0s for a 2-byte number
var getBitsFromNumber = function(number) {
var bits = [];
for (var i = 0; i < 16; i++) {
bits.push(getBit(number, i));
}
return bits;
};
// returns the next 2-byte number
var getNumberFromBits = function(bytes, history, hash) {
var number = 0, pos = 0;
while (pos < 16) {
var loc = getNextLocation(history, hash, bytes.length);
var bit = getBit(bytes[loc], 0);
number = setBit(number, pos, bit);
pos++;
}
return number;
};
// returns an array of 1s and 0s for the string 'message'
var getMessageBits = function(message) {
var messageBits = [];
for (var i = 0; i < message.length; i++) {
var code = message.charCodeAt(i);
messageBits = messageBits.concat(getBitsFromNumber(code));
}
return messageBits;
};
// gets the next location to store a bit
var getNextLocation = function(history, hash, total) {
var pos = history.length;
var loc = Math.abs(hash[pos % hash.length] * (pos + 1)) % total;
while (true) {
if (loc >= total) {
loc = 0;
} else if (history.indexOf(loc) >= 0) {
loc++;
} else if ((loc + 1) % 4 === 0) {
loc++;
} else {
history.push(loc);
return loc;
}
}
};
// encodes the supplied 'message' into the CanvasPixelArray 'colors'
var encodeMessage = function(colors, hash, message) {
// make an array of bits from the message
var messageBits = getBitsFromNumber(message.length);
messageBits = messageBits.concat(getMessageBits(message));
// this will store the color values we've already modified
var history = [];
// encode the bits into the pixels
var pos = 0;
while (pos < messageBits.length) {
// set the next color value to the next bit
var loc = getNextLocation(history, hash, colors.length);
colors[loc] = setBit(colors[loc], 0, messageBits[pos]);
// set the alpha value in this pixel to 255
// we have to do this because browsers do premultiplied alpha
while ((loc + 1) % 4 !== 0) {
loc++;
}
colors[loc] = 255;
pos++;
}
};
// returns the message encoded in the CanvasPixelArray 'colors'
var decodeMessage = function(colors, hash) {
// this will store the color values we've already read from
var history = [];
// get the message size
var messageSize = getNumberFromBits(colors, history, hash);
// exit early if the message is too big for the image
if ((messageSize + 1) * 16 > colors.length * 0.75) {
return '';
}
// exit early if the message is above an artificial limit
if (messageSize === 0 || messageSize > maxMessageSize) {
return '';
}
// put each character into an array
var message = [];
for (var i = 0; i < messageSize; i++) {
var code = getNumberFromBits(colors, history, hash);
message.push(String.fromCharCode(code));
}
// the characters should parse into valid JSON
return message.join('');
};