-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
180 lines (111 loc) · 3.51 KB
/
index.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
(function(global) {
const notify = (message) => {
let list = global.document.querySelector('#crypto ul');
if (list !== null) {
let item = global.document.createElement('li');
item.innerHTML = message;
list.appendChild(item);
setTimeout(() => {
item.setAttribute('data-show', true);
}, 100);
}
};
const render = (data) => {
let template = global.document.createElement('template');
template.innerHTML = data;
setTimeout(() => {
let sections = Array.from(template.content.querySelectorAll('section'));
if (sections.length > 0) {
sections.forEach((section) => {
global.document.querySelector('body').appendChild(section);
});
}
}, 0);
};
global.addEventListener('DOMContentLoaded', () => {
const crypto = global.crypto;
const element = global.document.querySelector('#crypto input[type="password"]');
const ENCODER = new TextEncoder();
const DECODER = new TextDecoder();
if (global.crypto !== undefined) {
notify('Web Crypto API initialized.');
element.removeAttribute('disabled');
} else {
notify('Web Crypto API not available!');
element.setAttribute('disabled', true);
}
const digest = async (password) => {
let temp = await crypto.subtle.digest('SHA-256', ENCODER.encode(password));
let digest = new Uint8Array(temp);
let str = '';
for (let d = 0, dl = digest.byteLength; d < dl; d++) {
str += String.fromCharCode(digest[d]);
}
let hex = '';
for (let s = 0; s < str.length; s++) {
let val = str.charCodeAt(s).toString(16);
if (val.length < 2) {
val = '0' + val;
}
hex += val;
}
return hex;
};
const decrypt = async (buffer, password) => {
let salt = buffer.slice( 0, 16);
let iv = buffer.slice(16, 16 + 12);
let pw_key = await crypto.subtle.importKey('raw', ENCODER.encode(password), 'PBKDF2', false, [ 'deriveKey' ]);
let aes_key = await crypto.subtle.deriveKey({
name: 'PBKDF2',
salt: salt,
iterations: 250000,
hash: 'SHA-256'
}, pw_key, {
name: 'AES-GCM',
length: 256
}, false, [ 'decrypt' ]);
let content = await crypto.subtle.decrypt({
name: 'AES-GCM',
iv: iv
}, aes_key, buffer.slice(16 + 12));
return DECODER.decode(content);
};
element.addEventListener('blur', async () => {
let password = element.value.trim();
if (password.length > 3) {
let filename = await digest(password);
notify('Downloading "' + filename + '.cv" ...');
let xhr = new XMLHttpRequest();
xhr.open('GET', '/cv/source/' + filename + '.cv');
xhr.responseType = 'arraybuffer';
xhr.onerror = () => {
notify('Encrypted CV not found.');
notify('Password wrong or deprecated.');
};
xhr.onload = async () => {
let decrypted = await decrypt(xhr.response, password);
if (decrypted.length > 0) {
if (
decrypted.includes('<section id="profile">')
|| decrypted.includes('<section id="open-source">')
|| decrypted.includes('<section id="teaching">')
) {
notify('Encrypted CV found.');
notify('Password correct.');
notify('Decrypting CV ...');
render(decrypted);
notify('Done. H4v3 fun :)');
setTimeout(() => {
let wrapper = global.document.querySelector('#crypto');
if (wrapper !== null) {
wrapper.parentNode.removeChild(wrapper);
}
}, 1000);
}
}
};
xhr.send();
}
});
});
})(typeof window !== 'undefined' ? window : this);