diff options
author | Laurent Ghigonis <laurent@p1sec.com> | 2013-06-16 14:18:59 +0200 |
---|---|---|
committer | Laurent Ghigonis <laurent@p1sec.com> | 2013-06-16 14:18:59 +0200 |
commit | bd2a4fa89142c19b2c14a375e463e7836f8e188c (patch) | |
tree | 5a7af4e4e783cbbaa4cf7625d960b5cd923dfd5f /jsaccess/jsa/jsa.js | |
parent | bashrc: add gitunpushed alias (diff) | |
download | laurent-tools-bd2a4fa89142c19b2c14a375e463e7836f8e188c.tar.xz laurent-tools-bd2a4fa89142c19b2c14a375e463e7836f8e188c.zip |
add jsaccess - download and decrypt files in the browser
Diffstat (limited to '')
-rw-r--r-- | jsaccess/jsa/jsa.js | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/jsaccess/jsa/jsa.js b/jsaccess/jsa/jsa.js new file mode 100644 index 0000000..67b55b9 --- /dev/null +++ b/jsaccess/jsa/jsa.js @@ -0,0 +1,71 @@ +/* jsaccess - download and decrypt files in the browser + * 2013, Laurent Ghigonis <laurent@gouloum.fr> */ + +/* Reference code */ +//console.log(JSON.stringify(xhr)); + +/* Called on "body" load */ +function jsainit() { + /* XXX get content of jsa/files/ directory to present + a list to the user */ +} + +/* Called on "Download" click */ +function jsadl() { + document.getElementById('status').innerHTML = ""; + var pass = document.getElementById('password').value; + var file = $('input[name=files]:checked').val(); + + obj = _dl(file, pass); +} + +function _dl(file, pass) { + window.URL = window.URL || window.webkitURL; + _status("Downloading \""+file+"\" ..."); + + dlreq = $.ajax({ + url: 'files/' + file, + beforeSend: function ( xhr ) { + xhr.overrideMimeType("application/base64"); + } + }).done(function ( data ) { + _status("Download complete, decrypting ..."); + _decrypt(data, pass, file); + }); +} + +function hex2a(hex) { + var str = ''; + for (var i = 0; i < hex.length; i += 2) + str += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); + return str; +} + +function _decrypt(obj, pass, name) { + try { + var decrypted = GibberishAES.dec(obj, pass); + } catch(err) { + _status(err.toString()); + } + out = $.base64.decode(decrypted.toString()); + _status("Decrypted successfuly, saving ..."); + _save(out, name); +} + +function _save(obj, name) { + var ab = new ArrayBuffer(obj.length); + var ia = new Uint8Array(ab); + for (var i = 0; i < obj.length; i++) { + ia[i] = obj.charCodeAt(i); + } + var blob = new Blob([ia], {type:"application/octet-binary"}); + saveAs(blob, name); + _status("File \""+name+"\" saved."); + _status("Have a good day."); +} + +function _status(txt) { + var div = document.getElementById('status'); + div.innerHTML = div.innerHTML + '<br/>' + txt; +} + |