Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple enhancements / Build v1.0.4 #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ module.exports = function (grunt) {
var pkg = grunt.file.readJSON('package.json');
grunt.initConfig({
pkg: pkg,
config: config,
banner: '/*\n * <%= pkg.name %> v<%= pkg.version %> <%= grunt.template.today("yyyy-mm-dd") %>\n *\n * Copyright (C) 2012 Peter Eigenschink (http://www.peter-eigenschink.at/)\n * Dual-licensed under MIT and Beerware license.\n*/\n',
config: config,
banner: '/*\n * <%= pkg.name %> v<%= pkg.version %> <%= grunt.template.today("yyyy-mm-dd") %>\n *\n * Copyright (C) 2018 Peter Eigenschink (http://www.peter-eigenschink.at/) and Contributors\n * Dual-licensed under MIT and Beerware license.\n*/\n',

echo: {
help: 'README.md'
Expand Down Expand Up @@ -154,4 +154,4 @@ module.exports = function (grunt) {

// Setup default task that runs when you just run 'grunt'
grunt.registerTask('default', ['build']);
};
};
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Copyright (c) 2016 Peter Eigenschink
Copyright (c) 2018 Peter Eigenschink and Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
200 changes: 101 additions & 99 deletions build/steganography.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* steganography.js v1.0.3 2017-09-22
* steganography.js v1.0.4 2018-11-14
*
* Copyright (C) 2012 Peter Eigenschink (http://www.peter-eigenschink.at/)
* Copyright (C) 2018 Peter Eigenschink (http://www.peter-eigenschink.at/) and Contributors
* Dual-licensed under MIT and Beerware license.
*/
;(function (name, context, factory) {
Expand All @@ -18,45 +18,45 @@
})("steg", this, function () {
var Cover = function Cover() {};
var util = {
"isPrime" : function(n) {
if (isNaN(n) || !isFinite(n) || n%1 || n<2) return false;
if (n%2===0) return (n===2);
if (n%3===0) return (n===3);
var m=Math.sqrt(n);
for (var i=5;i<=m;i+=6) {
if (n%i===0) return false;
if (n%(i+2)===0) return false;
"isPrime": function (n) {
if (isNaN(n) || !isFinite(n) || n % 1 || n < 2) return false;
if (n % 2 === 0) return (n === 2);
if (n % 3 === 0) return (n === 3);
var m = Math.sqrt(n);
for (var i = 5; i <= m; i += 6) {
if (n % i === 0) return false;
if (n % (i + 2) === 0) return false;
}
return true;
},
"findNextPrime" : function(n) {
for(var i=n; true; i+=1)
if(util.isPrime(i)) return i;
"findNextPrime": function (n) {
for (var i = n; true; i += 1)
if (util.isPrime(i)) return i;
},
"sum" : function(func, end, options) {
"sum": function (func, end, options) {
var sum = 0;
options = options || {};
for(var i = options.start || 0; i < end; i+=(options.inc||1))
for (var i = options.start || 0; i < end; i += (options.inc || 1))
sum += func(i) || 0;

return (sum === 0 && options.defValue ? options.defValue : sum);
},
"product" : function(func, end, options) {
"product": function (func, end, options) {
var prod = 1;
options = options || {};
for(var i = options.start || 0; i < end; i+=(options.inc||1))
for (var i = options.start || 0; i < end; i += (options.inc || 1))
prod *= func(i) || 1;

return (prod === 1 && options.defValue ? options.defValue : prod);
},
"createArrayFromArgs" : function(args,index,threshold) {
var ret = new Array(threshold-1);
for(var i = 0; i < threshold; i+=1)
ret[i] = args(i >= index ? i+1:i);
"createArrayFromArgs": function (args, index, threshold) {
var ret = new Array(threshold - 1);
for (var i = 0; i < threshold; i += 1)
ret[i] = args(i >= index ? i + 1 : i);

return ret;
},
"loadImg": function(url) {
"loadImg": function (url) {
var image = new Image();
image.src = url;
return image;
Expand All @@ -67,39 +67,41 @@ Cover.prototype.config = {
"t": 3,
"threshold": 1,
"codeUnitSize": 16,
"args": function(i) { return i+1; },
"messageDelimiter": function(modMessage,threshold) {
var delimiter = new Array(threshold*3);
for(var i = 0; i < delimiter.length; i+=1)
delimiter[i] = 255;
return delimiter;
},
"messageCompleted": function(data, i, threshold) {
var done = true;
for(var j = 0; j < 16 && done; j+=1) {
done = done && (data[i+j*4] === 255);
}
return done;
}
"args": function (i) { return i + 1; },
"messageDelimiter": function (modMessage, threshold) {
var delimiter = new Array(threshold * 3);
for (var i = 0; i < delimiter.length; i += 1)
delimiter[i] = 255;

return delimiter;
},
"messageCompleted": function (data, i, threshold) {
var done = true;
for (var j = 0; j < 16 && done; j += 1) {
done = done && (data[i + j * 4] === 255);
}
return done;
}
};
Cover.prototype.getHidingCapacity = function(image, options) {

Cover.prototype.getHidingCapacity = function (image, options) {
options = options || {};
var config = this.config;

var width = options.width || image.width,
height = options.height || image.height,
var width = options.width || image.width || image.naturalWidth,
height = options.height || image.height || image.naturalHeight,
t = options.t || config.t,
codeUnitSize = options.codeUnitSize || config.codeUnitSize;
return t*width*height/codeUnitSize >> 0;
return t * width * height / codeUnitSize >> 0;
};
Cover.prototype.encode = function(message, image, options) {

Cover.prototype.encode = function (message, image, options) {
// Handle image url
if(image.length) {
if (image.length) {
image = util.loadImg(image);
} else if(image.src) {
} else if (image.src) {
image = util.loadImg(image.src);
} else if(!(image instanceof HTMLImageElement)) {
} else if (!(image instanceof HTMLImageElement)) {
throw new Error('IllegalInput: The input image is neither an URL string nor an image.');
}

Expand All @@ -109,20 +111,20 @@ Cover.prototype.encode = function(message, image, options) {
var t = options.t || config.t,
threshold = options.threshold || config.threshold,
codeUnitSize = options.codeUnitSize || config.codeUnitSize,
prime = util.findNextPrime(Math.pow(2,t)),
prime = util.findNextPrime(Math.pow(2, t)),
args = options.args || config.args,
messageDelimiter = options.messageDelimiter || config.messageDelimiter;

if(!t || t < 1 || t > 7) throw new Error('IllegalOptions: Parameter t = " + t + " is not valid: 0 < t < 8');
if (!t || t < 1 || t > 7) throw new Error('IllegalOptions: Parameter t = " + t + " is not valid: 0 < t < 8');

var shadowCanvas = document.createElement('canvas'),
shadowCtx = shadowCanvas.getContext('2d');

shadowCanvas.style.display = 'none';
shadowCanvas.width = options.width || image.width;
shadowCanvas.height = options.height || image.height;
if(options.height && options.width) {
shadowCtx.drawImage(image, 0, 0, options.width, options.height );
shadowCanvas.width = options.width || image.width || image.naturalWidth;
shadowCanvas.height = options.height || image.height || image.naturalHeight;
if (options.height && options.width) {
shadowCtx.drawImage(image, 0, 0, options.width, options.height);
} else {
shadowCtx.drawImage(image, 0, 0);
}
Expand All @@ -135,90 +137,90 @@ Cover.prototype.encode = function(message, image, options) {
// dec ... UTF-16 Unicode of the i-th character of the message
// curOverlapping ... The count of the bits of the previous character not handled in the previous run
// mask ... The raw initial bitmask, will be changed every run and if bits are overlapping
var bundlesPerChar = codeUnitSize/t >> 0,
overlapping = codeUnitSize%t,
var bundlesPerChar = codeUnitSize / t >> 0,
overlapping = codeUnitSize % t,
modMessage = [],
decM, oldDec, oldMask, left, right,
dec, curOverlapping, mask;

var i, j;
for(i=0; i<=message.length; i+=1) {
for (i = 0; i <= message.length; i += 1) {
dec = message.charCodeAt(i) || 0;
curOverlapping = (overlapping*i)%t;
if(curOverlapping > 0 && oldDec) {
curOverlapping = (overlapping * i) % t;
if (curOverlapping > 0 && oldDec) {
// Mask for the new character, shifted with the count of overlapping bits
mask = Math.pow(2,t-curOverlapping) - 1;
mask = Math.pow(2, t - curOverlapping) - 1;
// Mask for the old character, i.e. the t-curOverlapping bits on the right
// of that character
oldMask = Math.pow(2, codeUnitSize) * (1 - Math.pow(2, -curOverlapping));
left = (dec & mask) << curOverlapping;
right = (oldDec & oldMask) >> (codeUnitSize - curOverlapping);
modMessage.push(left+right);
modMessage.push(left + right);

if(i<message.length) {
mask = Math.pow(2,2*t-curOverlapping) * (1 - Math.pow(2, -t));
for(j=1; j<bundlesPerChar; j+=1) {
if (i < message.length) {
mask = Math.pow(2, 2 * t - curOverlapping) * (1 - Math.pow(2, -t));
for (j = 1; j < bundlesPerChar; j += 1) {
decM = dec & mask;
modMessage.push(decM >> (((j-1)*t)+(t-curOverlapping)));
modMessage.push(decM >> (((j - 1) * t) + (t - curOverlapping)));
mask <<= t;
}
if((overlapping*(i+1))%t === 0) {
mask = Math.pow(2, codeUnitSize) * (1 - Math.pow(2,-t));
if ((overlapping * (i + 1)) % t === 0) {
mask = Math.pow(2, codeUnitSize) * (1 - Math.pow(2, -t));
decM = dec & mask;
modMessage.push(decM >> (codeUnitSize-t));
modMessage.push(decM >> (codeUnitSize - t));
}
else if(((((overlapping*(i+1))%t) + (t-curOverlapping)) <= t)) {
else if (((((overlapping * (i + 1)) % t) + (t - curOverlapping)) <= t)) {
decM = dec & mask;
modMessage.push(decM >> (((bundlesPerChar-1)*t)+(t-curOverlapping)));
modMessage.push(decM >> (((bundlesPerChar - 1) * t) + (t - curOverlapping)));
}
}
}
else if(i<message.length) {
mask = Math.pow(2,t) - 1;
for(j=0; j<bundlesPerChar; j+=1) {
else if (i < message.length) {
mask = Math.pow(2, t) - 1;
for (j = 0; j < bundlesPerChar; j += 1) {
decM = dec & mask;
modMessage.push(decM >> (j*t));
modMessage.push(decM >> (j * t));
mask <<= t;
}
}
oldDec = dec;
}

// Write Data
var offset, index, subOffset, delimiter = messageDelimiter(modMessage,threshold),
var offset, index, subOffset, delimiter = messageDelimiter(modMessage, threshold),
q, qS;
for(offset = 0; (offset+threshold)*4 <= data.length && (offset+threshold) <= modMessage.length; offset += threshold) {
qS=[];
for(i=0; i<threshold && i+offset < modMessage.length; i+=1) {
for (offset = 0; (offset + threshold) * 4 <= data.length && (offset + threshold) <= modMessage.length; offset += threshold) {
qS = [];
for (i = 0; i < threshold && i + offset < modMessage.length; i += 1) {
q = 0;
for(j=offset; j<threshold+offset && j<modMessage.length; j+=1)
q+=modMessage[j]*Math.pow(args(i),j-offset);
qS[i] = (255-prime+1)+(q%prime);
for (j = offset; j < threshold + offset && j < modMessage.length; j += 1)
q += modMessage[j] * Math.pow(args(i), j - offset);
qS[i] = (255 - prime + 1) + (q % prime);
}
for(i=offset*4; i<(offset+qS.length)*4 && i<data.length; i+=4)
data[i+3] = qS[(i/4)%threshold];
for (i = offset * 4; i < (offset + qS.length) * 4 && i < data.length; i += 4)
data[i + 3] = qS[(i / 4) % threshold];

subOffset = qS.length;
}
// Write message-delimiter
for(index = (offset+subOffset); index-(offset+subOffset)<delimiter.length && (offset+delimiter.length)*4<data.length; index+=1)
data[(index*4)+3]=delimiter[index-(offset+subOffset)];
for (index = (offset + subOffset); index - (offset + subOffset) < delimiter.length && (offset + delimiter.length) * 4 < data.length; index += 1)
data[(index * 4) + 3] = delimiter[index - (offset + subOffset)];
// Clear remaining data
for(i=((index+1)*4)+3; i<data.length; i+=4) data[i] = 255;
for (i = ((index + 1) * 4) + 3; i < data.length; i += 4) data[i] = 255;

imageData.data = data;
shadowCtx.putImageData(imageData, 0, 0);

return shadowCanvas.toDataURL();
};

Cover.prototype.decode = function(image, options) {
Cover.prototype.decode = function (image, options) {
// Handle image url
if(image.length) {
if (image.length) {
image = util.loadImg(image);
} else if(image.src) {
} else if (image.src) {
image = util.loadImg(image.src);
} else if(!(image instanceof HTMLImageElement)) {
} else if (!(image instanceof HTMLImageElement)) {
throw new Error('IllegalInput: The input image is neither an URL string nor an image.');
}

Expand All @@ -232,16 +234,16 @@ Cover.prototype.decode = function(image, options) {
args = options.args || config.args,
messageCompleted = options.messageCompleted || config.messageCompleted;

if(!t || t < 1 || t > 7) throw new Error('IllegalOptions: Parameter t = " + t + " is not valid: 0 < t < 8');
if (!t || t < 1 || t > 7) throw new Error('IllegalOptions: Parameter t = " + t + " is not valid: 0 < t < 8');

var shadowCanvas = document.createElement('canvas'),
shadowCtx = shadowCanvas.getContext('2d');

shadowCanvas.style.display = 'none';
shadowCanvas.width = options.width || image.width;
shadowCanvas.height = options.width || image.height;
if(options.height && options.width) {
shadowCtx.drawImage(image, 0, 0, options.width, options.height );
shadowCanvas.width = options.width || image.width || image.naturalWidth;
shadowCanvas.height = options.width || image.height || image.naturalHeight;
if (options.height && options.width) {
shadowCtx.drawImage(image, 0, 0, options.width, options.height);
} else {
shadowCtx.drawImage(image, 0, 0);
}
Expand All @@ -253,9 +255,9 @@ Cover.prototype.decode = function(image, options) {

var i, k, done;
if (threshold === 1) {
for(i=3, done=false; !done && i<data.length && !done; i+=4) {
for (i = 3, done = false; !done && i < data.length && !done; i += 4) {
done = messageCompleted(data, i, threshold);
if(!done) modMessage.push(data[i]-(255-prime+1));
if (!done) modMessage.push(data[i] - (255 - prime + 1));
}
} else {
/*for(k = 0, done=false; !done; k+=1) {
Expand Down Expand Up @@ -306,17 +308,17 @@ Cover.prototype.decode = function(image, options) {
}
*/}

var message = "", charCode = 0, bitCount = 0, mask = Math.pow(2, codeUnitSize)-1;
for(i = 0; i < modMessage.length; i+=1) {
var message = "", charCode = 0, bitCount = 0, mask = Math.pow(2, codeUnitSize) - 1;
for (i = 0; i < modMessage.length; i += 1) {
charCode += modMessage[i] << bitCount;
bitCount += t;
if(bitCount >= codeUnitSize) {
if (bitCount >= codeUnitSize) {
message += String.fromCharCode(charCode & mask);
bitCount %= codeUnitSize;
charCode = modMessage[i] >> (t-bitCount);
charCode = modMessage[i] >> (t - bitCount);
}
}
if(charCode !== 0) message += String.fromCharCode(charCode & mask);
if (charCode !== 0) message += String.fromCharCode(charCode & mask);

return message;
};
Expand Down
Loading