-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoted.js
61 lines (54 loc) · 1.45 KB
/
remoted.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
async function hash(string) {
const utf8 = new TextEncoder().encode(string);
const hashBuffer = await crypto.subtle.digest('SHA-256', utf8);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray
.map((bytes) => bytes.toString(16).padStart(2, '0'))
.join('');
return hashHex;
}
function updateLoginPage(data){
if(data.status===0){
location.replace('main');
}else{
document.querySelector('p#login_messages').innerHTML=data.message;
document.querySelector('form').reset();
}
}
function addFormEventListener(){
let form = document.querySelector('form');
form.addEventListener('submit',function(e){
e.preventDefault();
const payload = new URLSearchParams(new FormData(form));
password=payload.get('login');
//console.log('password: '+password);
(async () => {
let i=1337; // Do it elite number of times.
let hashedPW=password;
while(i--){
hashedPW=await hash(hashedPW);
}
return hashedPW;})
().then(pwHash=>{
payload.set('login',pwHash);
//console.log('hash: '+pwHash);
fetch('remoted.php',{method: "POST",body: payload})
.then(res=>res.json())
.then(data=>updateLoginPage(data))
.catch(e=>console.log(e));
});
});
}
function init(){
addFormEventListener();
}
/*
** Main entry.
*/
if (document.readyState === 'loading') {
// Loading hasn't finished yet.
document.addEventListener('DOMContentLoaded', init);
} else {
// `DOMContentLoaded` has already fired.
init();
}