1
0
mirror of https://github.com/Tygs/0bin.git synced 2023-08-10 21:13:00 +03:00
0bin/static/js/behavior.js

52 lines
1.2 KiB
JavaScript
Raw Normal View History

2012-04-26 13:38:55 +04:00
;
2012-04-26 23:04:36 +04:00
2012-04-24 22:15:38 +04:00
// Start random number generator seeding ASAP
sjcl.random.startCollectors();
2012-04-26 23:04:36 +04:00
var zerobin = {
encrypt: function(key, content) {
return sjcl.encrypt(key, lzw.compress(content));
},
decrypt: function(key, content) {
return lzw.decompress(sjcl.decrypt(key, content));
},
make_key: function() {
2012-04-26 17:26:58 +04:00
return sjcl.codec.base64.fromBits(sjcl.random.randomWords(8, 0), 0);
2012-04-26 23:04:36 +04:00
}
};
2012-04-26 13:38:55 +04:00
2012-04-26 17:26:58 +04:00
$(function(){
2012-04-26 13:38:55 +04:00
$('button[type=submit]').click(function(e){
e.preventDefault();
var paste = $('textarea').val();
if (paste.trim()) {
var expiration = $('#expiration').val();
2012-04-26 17:26:58 +04:00
var key = zerobin.make_key();
var data = {content: zerobin.encrypt(key, paste), expiration: expiration}
2012-04-24 22:15:38 +04:00
2012-04-26 13:38:55 +04:00
$.post('/paste/create', data)
.error(function(error) {
2012-04-26 13:38:55 +04:00
alert('Paste could not be saved. Please try again later.');
})
.success(function(data) {
window.location = ('/paste/' + data['paste'] + '#' + key);
2012-04-26 13:38:55 +04:00
});
}
2012-04-24 22:15:38 +04:00
2012-04-26 13:38:55 +04:00
});
2012-04-24 22:15:38 +04:00
2012-04-26 17:26:58 +04:00
var content = $('#paste-content').text().trim();
var key = window.location.hash.substring(1);
if (content && key) {
try {
$('#paste-content').text(zerobin.decrypt(key, content));
2012-04-26 23:04:36 +04:00
hljs.highlightBlock($('#paste-content')[0]);
2012-04-26 17:26:58 +04:00
} catch(err) {
alert('Could not decrypt data (Wrong key ?)');
}
}
2012-04-24 22:15:38 +04:00
});