-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset-password.js
More file actions
59 lines (43 loc) · 1.75 KB
/
Copy pathset-password.js
File metadata and controls
59 lines (43 loc) · 1.75 KB
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
// Saves the browser-lock password to chrome.storage.
// SECURITY: the password is hashed with SHA-256 before being stored, so the
// raw password is never persisted to disk.
const LS = {
getAllItems: () => chrome.storage.local.get(),
getItem: key => chrome.storage.local.get(key)[key],
setItem: (key, val) => chrome.storage.local.set({[key]: val}),
removeItems: keys => chrome.storage.local.remove(keys),
};
// Returns the lowercase hex SHA-256 digest of the given string.
async function sha256(text) {
const data = new TextEncoder().encode(text);
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
return Array.from(new Uint8Array(hashBuffer))
.map(b => b.toString(16).padStart(2, '0'))
.join('');
}
async function save_options() {
var old = document.getElementById('old').value;
var newPassVal = document.getElementById('new').value;
document.getElementById('note').style.display='block';
if(old == "" || newPassVal =="" ){
document.getElementById('note').style.display='block';
document.getElementById('note').innerHTML='enter both fields';
}
else if(newPassVal == old){
// Store only the hash, never the plaintext password.
const hashed = await sha256(newPassVal);
chrome.storage.local.set({ key: hashed }, () => {
});
document.getElementById('note').innerHTML='password saved';
document.getElementById('old').value='';
document.getElementById('new').value='';
document.getElementById('save').style.display='none';
chrome.windows.getCurrent({}, function(window) {
chrome.windows.remove(window.id);
});
}
else{
document.getElementById('note').innerHTML='both passwords need to match';
}
}
document.getElementById('save').addEventListener('click',save_options);