aboutsummaryrefslogtreecommitdiffstats
path: root/jsaccess/jsa/jsa.js
blob: 5cac8076996b8d4951ca752ce1d06e9ec86166fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* jsaccess - download and decrypt files in the browser
 * 2013, Laurent Ghigonis <laurent@gouloum.fr> */

/* Reference code */
//console.log(JSON.stringify(xhr));

/* ===== PUBLIC - called by html ===== */

/* Called on "body" load */
function jsainit() {

}

/* Called on "Get files list" click */

function jsagetlist() {
	var pass = document.getElementById('password').value;
	var RMD160 = new Hashes.RMD160;
	var hash = RMD160.hex(pass);

	_status("Getting file list ...");
	listreq = $.ajax({
		url: 'files/' + hash + '/index.txt',
		beforeSend: function ( xhr ) {
			xhr.overrideMimeType("application/base64");
		},
		success: function ( data ) {
			_status("We have file list");
			_showfiles(data, pass, hash);
		},
		error: function (xhr, opts, err) {
			_status("Bad password");
			throw(err);
		}
	});
}

/* Called on "Download" click */
function jsadl() {
	var pass = document.getElementById('password').value;
	var file = $('input[name=file]:checked').val();
	
	obj = _dl(file, pass);
}

/* ===== PRIVATE - called within this javascript file ===== */

function _showfiles(data, pass, hash) {
	try {
		var decrypted = GibberishAES.dec(data, pass);
	} catch(err) {
		_status(err.toString());
		throw err;
	}

	document.getElementById('files').innerHTML = "";
	lines = decrypted.split("\n").filter(function(n){return n});
	$.each(lines,
		function( idx, obj ){
			obj = obj.trim();
			var btn = $('<input type="radio" name="file" value="'+obj+'"/>'+obj+'<br/>');
			btn.appendTo('#files');
		});
}

function _dl(file, pass) {
	var RMD160 = new Hashes.RMD160;

	var dirhash = RMD160.hex(pass);
	var path = 'files/' + dirhash + '/' + RMD160.hex(dirhash + file);
	_status("Downloading \""+file+"\" ...");

	dlreq = $.ajax({
		url: path,
		beforeSend: function ( xhr ) {
			xhr.overrideMimeType("application/base64");
		},
		success: function ( data ) {
			_status("Download complete, decrypting ...");
			_decrypt(data, pass, file);
		},
		error: function (xhr, opts, err) {
			_status("Dowload failed (status="+xhr.status+")");
			throw(err);
		}
	});
}

function _decrypt(obj, pass, name) {
	try {
		var decrypted = GibberishAES.dec(obj, pass);
	} catch(err) {
		_status(err.toString());
		throw err;
	}
	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;
}