Skip to content

Commit a86e275

Browse files
committed
more secure pass
1 parent a646bd4 commit a86e275

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

scripts/auto_lockWebsite.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BADGES } from "./helpers/badge.js";
2+
import { md5 } from "./libs/crypto/md5.js";
23

34
const managerBtn = '<i class="fa-solid fa-unlock-keyhole"></i>';
45

@@ -33,11 +34,11 @@ export default {
3334
en: "Open manager",
3435
},
3536
onClick: async function openManager() {
36-
let curPass = await locker.password.get();
37-
if (curPass == null) {
38-
curPass = await initPassword();
37+
let hasPass = await locker.password.has();
38+
if (!hasPass) {
39+
hasPass = await initPassword();
3940
}
40-
if (curPass) {
41+
if (hasPass) {
4142
window.open("/scripts/auto_lockWebsite.html", "_self");
4243
}
4344
},
@@ -61,8 +62,8 @@ export default {
6162
},
6263
onClick: async () => {
6364
const { t } = await import("../popup/helpers/lang.js");
64-
let password = await locker.password.get();
65-
if (password == null) {
65+
let hasPass = await locker.password.has();
66+
if (!hasPass) {
6667
Swal.fire({
6768
icon: "warning",
6869
title: t({
@@ -92,11 +93,10 @@ export default {
9293
},
9394
onClick: async () => {
9495
try {
95-
let password = await locker.password.get();
96-
if (password == null) {
97-
return;
98-
}
99-
lockCurrentWebsite(password);
96+
const pass = await locker.password.get();
97+
if (pass == null) return;
98+
99+
lockCurrentWebsite(pass);
100100
locker.sites.add(location.hostname);
101101
} catch (e) {
102102
console.error(e);
@@ -124,11 +124,17 @@ export const _storage = {
124124
export const locker = {
125125
password: {
126126
storageKey: "auto_lock_website_manager_password",
127+
async compare(pass) {
128+
return md5(pass) === (await _storage.get(this.storageKey));
129+
},
130+
async has() {
131+
return (await _storage.get(this.storageKey)) != null;
132+
},
127133
get() {
128134
return _storage.get(this.storageKey);
129135
},
130136
set(pass) {
131-
return _storage.set(this.storageKey, pass);
137+
return _storage.set(this.storageKey, md5(pass));
132138
},
133139
remove() {
134140
return _storage.remove(this.storageKey);
@@ -192,8 +198,8 @@ export async function initPassword(createNew = false) {
192198
export async function checkPass(reason) {
193199
const { t } = await import("../popup/helpers/lang.js");
194200

195-
let curPass = await locker.password.get();
196-
if (curPass == null) return "not init";
201+
let hasPass = await locker.password.has();
202+
if (!hasPass) return "not init";
197203

198204
const { value: pass } = await Swal.fire({
199205
icon: "info",
@@ -221,7 +227,7 @@ export async function checkPass(reason) {
221227
},
222228
});
223229

224-
if (pass === curPass) return true;
230+
if (await locker.password.compare(pass)) return true;
225231
if (pass != null) {
226232
await Swal.fire(
227233
t({ vi: "Sai mật khẩu", en: "Wrong password!" }),
@@ -359,7 +365,7 @@ function lockCurrentWebsite(pass, matchedPattern) {
359365
const unlockTemporarly = overlay.querySelector("input#unlock-temporarly");
360366
const inputPass = overlay.querySelector("input#password");
361367
inputPass.addEventListener("input", (e) => {
362-
if (e.target.value == pass) {
368+
if (md5(e.target.value) == pass) {
363369
overlay.style.top = "-100vh";
364370
style.disabled = true;
365371
inputPass.value = "";

scripts/content-scripts/ufs_global.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const UfsGlobal = {
4747
getWatchingVideoSrc,
4848
},
4949
Utils: {
50+
hashString,
5051
lerp,
5152
getNumberFormatter,
5253
deepClone,
@@ -913,6 +914,20 @@ function getWatchingVideoSrc() {
913914

914915
// #region Utils
915916

917+
// https://stackoverflow.com/a/7616484/23648002
918+
function hashString(str) {
919+
let hash = 0,
920+
i,
921+
chr;
922+
if (str.length === 0) return hash;
923+
for (i = 0; i < str.length; i++) {
924+
chr = str.charCodeAt(i);
925+
hash = (hash << 5) - hash + chr;
926+
hash |= 0; // Convert to 32bit integer
927+
}
928+
return hash;
929+
}
930+
916931
function lerp(from, to, speed) {
917932
return from + (to - from) * speed;
918933
}

0 commit comments

Comments
 (0)