forked from markszabo/PostQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signin.js
executable file
·113 lines (106 loc) · 3.6 KB
/
signin.js
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
//scrypt parameters
var N = 1024, r = 80, p = 1;
var dkLen = 32;
var inputEmail;
var inputPassword;
var inputSalt;
var password;
var salt;
var authenticationkey
var decryptionkey;
var privatekey;
function prepareInput() {
inputEmail = $('#inputEmail').val();
inputPassword = $('#inputPassword').val();
inputSalt = inputEmail + "someRandomsrzujhnfgbdf";
password = new buffer.SlowBuffer(inputPassword.normalize('NFKC'));
salt = new buffer.SlowBuffer(inputSalt.normalize('NFKC'));
}
function signin() {
hideLoginAlert();
prepareInput();
if(inputEmail == "" || inputPassword == "") {
displayLoginAlert("danger","Username and password must be set.");
return;
}
$('.progress').show();
scrypt(password, salt, N, r, p, dkLen, function(error, progress, hash) {
if (error) {
displayLoginAlert("danger","Calculating the scrypt hash of the password failed. Try again. Detailed error: " + error.toString());
} else if (hash) {
decryptionkey = hash.slice(0,16);
authenticationkey = encodeURIComponent(btoa(String.fromCharCode.apply(null,hash.slice(16,32))));
$.get("login.php?username=" + inputEmail + "&password=" + authenticationkey,
function(data, status){
if(data.substring(0,1) == '1') { //successfull login
$('#signin').hide();
$('#main').show();
privatekey = AESdecrypt(data.substr(1), decryptionkey); //login.php returns '1'.privatekey_aes
handleFriendRequests();
generateMenu();
} else {
displayLoginAlert("danger",data);
}
});
} else {
updateInterface(progress);
}
});
}
function register() {
hideLoginAlert();
prepareInput();
if(inputEmail == "" || inputPassword == "") {
displayLoginAlert("danger","Username and password must be set.");
return;
}
$('.progress').show();
scrypt(password, salt, N, r, p, dkLen, function(error, progress, hash) {
if (error) {
displayLoginAlert("danger","Calculating the scrypt hash of the password failed. Try again. Detailed error: " + error.toString());
} else if (hash) {
decryptionkey = hash.slice(0,16);
var authenticationkey = btoa(String.fromCharCode.apply(null,hash.slice(16,32)));
keys = generateNTRUKeys(decryptionkey, function(keys){
updateInterface(0.9);
$.post("register.php", {
username: inputEmail,
password: authenticationkey,
privatekey: keys[0],
publickey: btoa(keys[1])
},
function(data, status){
updateInterface(1);
if(data == "1") { //success
displayLoginAlert("success","Registration successful, you can login now.");
} else {
displayLoginAlert("danger",data);
}
});
});
} else {
updateInterface(0.8*progress); //creating the hash is the progress up to 80%, then NTRU keygen (90%) and call register.php (100%)
}
});
}
function displayLoginAlert(type, text) {
displayAlert('#loginalert', type, text);
}
function hideLoginAlert() {
$('#loginalert').hide(500);
}
function displayAlert(divid, type, text) {
$(divid).hide();
$(divid).empty();
$(divid).html('<div class="alert alert-' + type + '"><a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> ' + text + '</div>');
$(divid).show(500);
}
function updateInterface(progress) {
$('#scryptprogress').width(progress*100+"%");
$('#scryptprogress').attr("aria-valuenow",progress);
if(progress == 1) {
$('.progress').hide();
$('#scryptprogress').width(0);
$('#scryptprogress').attr("aria-valuenow",0);
}
}