2016-05-28 04:34:52 +00:00
|
|
|
var clipboard = new Clipboard('.btn');
|
|
|
|
|
|
|
|
clipboard.on('success', function(e) {
|
|
|
|
$('#copyFeedback').css('display', 'inherit');
|
|
|
|
$('#copyFeedback').css('color', 'green');
|
|
|
|
$('#copyFeedback').html('Copied!');
|
|
|
|
e.clearSelection();
|
|
|
|
});
|
|
|
|
|
|
|
|
clipboard.on('error', function(e) {
|
|
|
|
$('#copyFeedback').css('display', 'inherit');
|
|
|
|
$('#copyFeedback').css('color', 'red');
|
2016-08-05 11:08:47 +00:00
|
|
|
$('#copyFeedback').html('Your browser doesn\'t seem to support automatic copying. Get a better one.');
|
2016-05-28 04:34:52 +00:00
|
|
|
e.clearSelection();
|
|
|
|
});
|
|
|
|
|
2016-08-05 11:08:47 +00:00
|
|
|
if ($('#useEncrypt').is(':checked') == false)
|
|
|
|
{
|
|
|
|
$('#encryptArea').css('display', 'none');
|
|
|
|
}
|
|
|
|
|
2016-05-28 04:34:52 +00:00
|
|
|
$('#modalClose').click(function(){
|
|
|
|
$('#copyFeedback').css('display', 'none');
|
|
|
|
});
|
|
|
|
|
2016-08-05 11:08:47 +00:00
|
|
|
window.snowMode = 'encode';
|
|
|
|
|
|
|
|
$("#output").on("click", function () {
|
|
|
|
$(this).select();
|
|
|
|
});
|
2016-05-28 04:34:52 +00:00
|
|
|
|
|
|
|
$('#toggle').click(function(){
|
|
|
|
|
2016-08-05 11:08:47 +00:00
|
|
|
if (window.snowMode == 'encode')
|
2016-05-28 04:34:52 +00:00
|
|
|
{
|
2016-08-05 11:08:47 +00:00
|
|
|
window.snowMode = 'decode';
|
|
|
|
$('#toggle').html("Decode <i class='fa fa-unlock'></i>");
|
2016-05-28 04:34:52 +00:00
|
|
|
$('#confirmPass').css('display', 'none');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-05 11:08:47 +00:00
|
|
|
window.snowMode = 'encode';
|
|
|
|
$('#toggle').html("Encode <i class='fa fa-lock'></i>");
|
2016-05-28 04:34:52 +00:00
|
|
|
$('#confirmPass').css('display', 'inline');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-08-05 11:08:47 +00:00
|
|
|
/* based on stackoverflow.com/questions/14430633/how-to-convert-text-to-binary-code-in-javascript */
|
|
|
|
function binToText(str) {
|
|
|
|
var str = str.replace(/ /g, "1");
|
|
|
|
var str = str.replace(/\t/g, "0");
|
|
|
|
if(str.match(/[10]{8}/g)){
|
|
|
|
var wordFromBinary = str.match(/([10]{8}|\s+)/g).map(function(fromBinary){
|
|
|
|
return String.fromCharCode(parseInt(fromBinary, 2) );
|
|
|
|
}).join('');
|
|
|
|
return wordFromBinary;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* based on stackoverflow.com/questions/21354235/converting-binary-to-text-using-javascript */
|
|
|
|
function textToBin(text) {
|
|
|
|
var output = '';
|
|
|
|
var length = text.length,
|
|
|
|
output = [];
|
|
|
|
for (var i = 0;i < length; i++) {
|
|
|
|
var bin = text[i].charCodeAt().toString(2);
|
|
|
|
output.push(Array(8-bin.length+1).join("0") + bin);
|
|
|
|
}
|
|
|
|
return output.join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#useEncrypt').click(function(){
|
|
|
|
if (! this.checked)
|
2016-05-28 04:34:52 +00:00
|
|
|
{
|
2016-08-05 11:08:47 +00:00
|
|
|
$('#encryptArea').css('display', 'none');
|
2016-05-28 04:34:52 +00:00
|
|
|
}
|
2016-08-05 11:08:47 +00:00
|
|
|
else
|
2016-05-28 04:34:52 +00:00
|
|
|
{
|
2016-08-05 11:08:47 +00:00
|
|
|
$('#encryptArea').css('display', 'inherit');
|
|
|
|
}
|
|
|
|
})
|
2016-05-28 04:34:52 +00:00
|
|
|
|
2016-08-05 11:08:47 +00:00
|
|
|
function verifyPass(mode)
|
|
|
|
{
|
|
|
|
if ($('#password').val() == '')
|
|
|
|
{
|
|
|
|
alert('You must provide a password.');
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-28 04:34:52 +00:00
|
|
|
|
2016-08-05 11:08:47 +00:00
|
|
|
if (mode == 'encrypt')
|
|
|
|
{
|
|
|
|
if ($('#password').val() != $('#confirmPass').val())
|
2016-05-28 04:34:52 +00:00
|
|
|
{
|
2016-08-05 11:08:47 +00:00
|
|
|
alert('Passwords must match.');
|
|
|
|
return false;
|
2016-05-28 04:34:52 +00:00
|
|
|
}
|
2016-08-05 11:08:47 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#go').click(function(){
|
|
|
|
var output = '';
|
|
|
|
|
|
|
|
var input = $('#text').val();
|
|
|
|
|
|
|
|
if (input == '') { return false; }
|
2016-05-28 04:34:52 +00:00
|
|
|
|
|
|
|
|
2016-08-05 11:08:47 +00:00
|
|
|
// If we're encoding:
|
|
|
|
if (window.snowMode == 'encode')
|
|
|
|
{
|
|
|
|
// If we should use encryption, encrypt first:
|
|
|
|
if ($('#useEncrypt').is(':checked'))
|
|
|
|
{
|
|
|
|
// verify password first
|
|
|
|
if (verifyPass('encrypt'))
|
|
|
|
{
|
|
|
|
input = CryptoJS.AES.encrypt(input, $('#password').val()).toString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-28 04:34:52 +00:00
|
|
|
}
|
2016-08-05 11:08:47 +00:00
|
|
|
// convert result to binary
|
2017-03-28 04:04:07 +00:00
|
|
|
output = textToBin(btoa(input));
|
2016-08-05 11:08:47 +00:00
|
|
|
$('#output').val(output.toString().replace(/1/g, " ").replace(/0/g, "\t"));
|
2016-05-28 04:34:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-05 11:08:47 +00:00
|
|
|
var output = binToText(input);
|
|
|
|
if ($('#useEncrypt').is(':checked'))
|
|
|
|
{
|
|
|
|
if (verifyPass('decrypt'))
|
2016-05-28 04:34:52 +00:00
|
|
|
{
|
2016-08-05 11:08:47 +00:00
|
|
|
output = CryptoJS.AES.decrypt(output, $('#password').val()).toString(CryptoJS.enc.Utf8);
|
2016-05-28 04:34:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-05 11:08:47 +00:00
|
|
|
return false;
|
2016-05-28 04:34:52 +00:00
|
|
|
}
|
2016-08-05 11:08:47 +00:00
|
|
|
}
|
2017-03-28 04:04:07 +00:00
|
|
|
$('#output').val(atob(output.toString()));
|
2016-05-28 04:34:52 +00:00
|
|
|
}
|
2016-08-05 11:08:47 +00:00
|
|
|
$('#outputModal').modal();
|
2017-03-28 04:04:07 +00:00
|
|
|
});
|