Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
138 lines (121 loc) · 4.92 KB
/
Copy pathscript.js
File metadata and controls
138 lines (121 loc) · 4.92 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
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
async function hashRaw(algorithm, text) {
const encoder = new TextEncoder();
const data = encoder.encode(text);
const start = performance.now();
const hashBuffer = await crypto.subtle.digest(algorithm, data);
const end = performance.now();
const timeMs = (end - start).toFixed(2);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hex = hashArray.map(b => b.toString(16).padStart(2, "0")).join("");
return { hex, timeMs };
}
function formatHash(hex, format) {
if (format === "hex") return hex;
if (format === "hex-upper") return hex.toUpperCase();
if (format === "base64") {
const bytes = hex.match(/.{1,2}/g).map(b => parseInt(b, 16));
const uint8 = new Uint8Array(bytes);
let binary = "";
uint8.forEach(b => binary += String.fromCharCode(b));
return btoa(binary);
}
return hex;
}
function updateHashLength(formattedHash) {
const lengthSpan = document.getElementById("hashLength");
if (!formattedHash) {
lengthSpan.textContent = "Hash length: —";
} else {
lengthSpan.textContent = `Hash length: ${formattedHash.length} characters`;
}
}
async function computeAndDisplayHash() {
const password = document.getElementById("passwordInput").value;
const salt = document.getElementById("saltInput").value;
const algorithm = document.getElementById("algorithmSelect").value;
const format = document.getElementById("formatSelect").value;
if (!password) {
document.getElementById("hashOutput").value = "";
updateHashLength("");
document.getElementById("hashTiming").textContent = "Hash time: — ms";
return;
}
const combined = salt + password;
const { hex, timeMs } = await hashRaw(algorithm, combined);
const formatted = formatHash(hex, format);
document.getElementById("hashOutput").value = formatted;
document.getElementById("hashTiming").textContent = `Hash time: ${timeMs} ms`;
updateHashLength(formatted);
}
document.getElementById("hashButton").addEventListener("click", async () => {
await computeAndDisplayHash();
});
document.getElementById("copyButton").addEventListener("click", () => {
const output = document.getElementById("hashOutput").value;
if (!output) return;
navigator.clipboard.writeText(output);
});
document.getElementById("copyJsonButton").addEventListener("click", () => {
const salt = document.getElementById("saltInput").value;
const hash = document.getElementById("hashOutput").value;
if (!hash) return;
const json = JSON.stringify({ salt, hash }, null, 2);
navigator.clipboard.writeText(json);
});
document.getElementById("togglePassword").addEventListener("click", () => {
const input = document.getElementById("passwordInput");
const btn = document.getElementById("togglePassword");
if (input.type === "password") {
input.type = "text";
btn.textContent = "Hide";
} else {
input.type = "password";
btn.textContent = "Show";
}
});
document.getElementById("generateSalt").addEventListener("click", () => {
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*";
let salt = "";
for (let i = 0; i < 10; i++) {
salt += chars[Math.floor(Math.random() * chars.length)];
}
document.getElementById("saltInput").value = salt;
});
document.getElementById("clearAllButton").addEventListener("click", () => {
document.getElementById("passwordInput").value = "";
document.getElementById("saltInput").value = "";
document.getElementById("hashOutput").value = "";
document.getElementById("hashTiming").textContent = "Hash time: — ms";
updateHashLength("");
});
document.getElementById("instantHashToggle").addEventListener("change", () => {
const instant = document.getElementById("instantHashToggle").checked;
const hashButton = document.getElementById("hashButton");
if (instant) {
hashButton.style.display = "none";
computeAndDisplayHash();
} else {
hashButton.style.display = "block";
}
});
["passwordInput", "saltInput", "algorithmSelect", "formatSelect"].forEach(id => {
document.getElementById(id).addEventListener("input", () => {
const instant = document.getElementById("instantHashToggle").checked;
if (instant) {
computeAndDisplayHash();
}
});
document.getElementById(id).addEventListener("change", () => {
const instant = document.getElementById("instantHashToggle").checked;
if (instant) {
computeAndDisplayHash();
}
});
});
document.getElementById("themeToggle").addEventListener("click", () => {
const body = document.body;
const current = body.getAttribute("data-theme");
const next = current === "dark" ? "light" : "dark";
body.setAttribute("data-theme", next);
document.getElementById("themeToggle").textContent = next === "dark" ? "Dark" : "Light";
});