streams/view/js/crypto.js

134 lines
3.7 KiB
JavaScript
Raw Normal View History

2013-11-11 03:58:08 +00:00
function str_rot13 (str) {
// http://kevin.vanzonneveld.net
// + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// + improved by: Ates Goral (http://magnetiq.com)
// + bugfixed by: Onno Marsman
// + improved by: Rafa? Kukawski (http://blog.kukawski.pl)
// * example 1: str_rot13('Kevin van Zonneveld');
// * returns 1: 'Xriva ina Mbaariryq'
// * example 2: str_rot13('Xriva ina Mbaariryq');
// * returns 2: 'Kevin van Zonneveld'
// * example 3: str_rot13(33);
// * returns 3: '33'
return (str + '').replace(/[a-z]/gi, function (s) {
return String.fromCharCode(s.charCodeAt(0) + (s.toLowerCase() < 'n' ? 13 : -13));
});
}
// Arrays for pluggable encryptors/decryptors
var red_encryptors = new Array();
var red_decryptors = new Array();
2020-08-23 09:28:38 +00:00
function hz_encrypt(alg, elem) {
2013-11-11 03:58:08 +00:00
var enc_text = '';
var newdiv = '';
2013-11-12 01:50:36 +00:00
if(typeof tinyMCE !== "undefined")
tinyMCE.triggerSave(false,true);
2013-11-11 07:19:44 +00:00
var text = $(elem).val();
2013-11-11 08:21:00 +00:00
// key and hint need to be localised
var passphrase = prompt(aStr['passphrase']);
// let the user cancel this dialogue
if (passphrase == null)
return false;
var enc_key = bin2hex(passphrase);
2013-11-11 07:19:44 +00:00
2013-11-11 08:21:00 +00:00
// If you don't provide a key you get rot13, which doesn't need a key
// but consequently isn't secure.
2013-11-11 07:19:44 +00:00
if(! enc_key)
2013-11-11 03:58:08 +00:00
alg = 'rot13';
if((alg == 'rot13') || (alg == 'triple-rot13'))
2020-08-23 09:28:38 +00:00
newdiv = "[crypt alg='rot13']" + window.btoa(str_rot13(text)) + '[/crypt]';
2013-11-11 08:21:00 +00:00
2020-08-23 09:28:38 +00:00
if(alg == 'AES-128-CCM') {
// This is the prompt we're going to use when the receiver tries to open it.
// Maybe "Grandma's maiden name" or "our secret place" or something.
2013-11-11 08:21:00 +00:00
var enc_hint = bin2hex(prompt(aStr['passhint']));
2013-11-11 03:58:08 +00:00
2020-08-23 09:28:38 +00:00
enc_text = sjcl.encrypt(enc_key, text);
encrypted = enc_text.toString();
2013-11-11 03:58:08 +00:00
2020-08-23 09:28:38 +00:00
newdiv = "[crypt alg='AES-128-CCM' hint='" + enc_hint + "']" + window.btoa(encrypted) + '[/crypt]';
}
2013-11-11 03:58:08 +00:00
if((red_encryptors.length) && (! newdiv.length)) {
for(var i = 0; i < red_encryptors.length; i ++) {
newdiv = red_encryptors[i](alg,text);
if(newdiv.length)
break;
}
}
2013-11-11 08:22:51 +00:00
enc_key = '';
2013-11-12 01:50:36 +00:00
// This might be a comment box on a page with a tinymce editor
// so check if there is a tinymce editor but also check the display
// property of our source element - because a tinymce instance
// will have display "none". If a normal textarea such as in a comment
// box has display "none" you wouldn't be able to type in it.
if($(elem).css('display') == 'none' && typeof tinyMCE !== "undefined") {
tinyMCE.activeEditor.setContent(newdiv);
}
else {
$(elem).val(newdiv);
}
2013-11-11 07:19:44 +00:00
2013-11-11 03:58:08 +00:00
}
2020-08-23 09:28:38 +00:00
function hz_decrypt(alg, hint, text, elem) {
2013-11-11 03:58:08 +00:00
var dec_text = '';
2013-11-11 03:58:08 +00:00
2020-08-23 09:28:38 +00:00
text = window.atob(text);
2013-11-11 03:58:08 +00:00
if(alg == 'rot13' || alg == 'triple-rot13')
dec_text = str_rot13(text);
else {
var enc_key = bin2hex(prompt((hint.length) ? hex2bin(hint) : aStr['passphrase']));
}
2013-11-11 03:58:08 +00:00
2020-08-23 09:28:38 +00:00
if(alg == 'AES-128-CCM') {
dec_text = sjcl.decrypt(enc_key, text);
}
2013-11-11 03:58:08 +00:00
if((red_decryptors.length) && (! dec_text.length)) {
for(var i = 0; i < red_decryptors.length; i ++) {
dec_text = red_decryptors[i](alg,text,enc_key);
if(dec_text.length)
break;
}
}
2013-11-11 08:22:51 +00:00
enc_key = '';
2013-11-11 08:21:00 +00:00
// Not sure whether to drop this back in the conversation display.
// It probably needs a lightbox or popup window because any conversation
// updates could
// wipe out the text and make you re-enter the key if it was in the
// conversation. For now we do that so you can read it.
2013-11-11 03:58:08 +00:00
2020-08-23 09:28:38 +00:00
var dec_result = dec_text.toString();
delete dec_text;
// incorrect decryptions *usually* but don't always have zero length
// If the person typo'd let them try again without reloading the page
// otherwise they'll have no "padlock" to click to try again.
if(dec_result.length) {
$(elem).html(b2h(dec_result));
dec_result = '';
}
2013-11-11 03:58:08 +00:00
}