-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_worker.js
129 lines (122 loc) · 3.14 KB
/
auth_worker.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const cl = console.log,
ce = console.error;
const utils = {
get: function(obj, cb){
fetch(obj.url, {
method: 'GET',
headers: {
'Accept-Encoding': 'gzip',
'Sec-Fetch-Dest': 'object',
'Sec-Fetch-mode': 'cors',
'Sec-Fetch-Site': 'cross-site'
}
})
.then(function(res){
if (res.status >= 200 && res.status < 300) {
return res[obj.encode]();
} else {
return Promise.reject(new Error(res.statusText))
}
})
.then(function(data) {
cb(false, data)
})
.catch(function(err){
cb(false)
})
},
U82HEX: function(byteArray) {
return Array.prototype.map.call(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
},
sha: function(buff,cb){
crypto.subtle.digest({name: "SHA-512"},new Uint8Array(buff))
.then(function(hash){
hash = utils.U82HEX(new Uint8Array(hash));
cb(false, hash);
})
.catch(function(err){
cb(true);
});
},
get_hash: function(url, cb){
utils.get({url: url, encode: 'arrayBuffer'}, function(err,res){
if(err){return cb(null)}
utils.sha(res, function(err,hash){
if(err){return cb(null)}
cb(false,hash)
})
})
},
build: function(){
utils.get({url: './app/data/hash.json', encode: 'json'}, function(err,res){
if(err){return cl(err)}
let len = res.length;
for (let i = 0; i < len; i++) {
utils.get_hash(res[i].src, function(err,data){
if(err){return cb(null)}
res[i].hash = data;
if(i === len -1){
cl(JSON.stringify(res))
}
})
}
})
},
verify: function(cb){
//let url = 'https://glcdn.rawgit.org/angeal185/auth/master/crypto-messenger/hash.json'
let url = './app/data/hash.json'
utils.get({url: url, encode: 'json'}, function(err,res){
if(err){return cb(undefined)}
//return utils.build();
let len = res.length,
arr = [];
for (let i = 0; i < len; i++) {
utils.get_hash(res[i].src, function(err,data){
if(err){
arr.push(res[i].src)
} else if(res[i].hash !== data){
arr.push(res[i].src)
}
if(i === len -1){
return cb(arr)
}
})
}
})
}
}
onmessage = function(evt) {
if(evt.isTrusted && evt.userActivation === null){
let data = evt.data;
if(data.type === 'verify'){
utils.verify(function(res){
if(!res || typeof res !== 'object'){
return postMessage({
type: 'verify',
status: false,
msg: 'unable to verify the file integrity of site data'
});
}
if(res.length > 0){
return postMessage({
type: 'verify',
status: false,
msg: 'Site file integrity test failed'
});
}
return postMessage({
type: 'verify',
status: true,
msg: 'Site file integrity pass'
});
})
}
} else {
postMessage({
type: 'error',
msg: 'untrusted worker event blocked'
});
}
};