Files
Sharpbin/SharpbinV2.Server/wwwroot/js/index.js
T
whoswhip fddfeb6525 Improve AES encryption with dynamic salt handling
Updated `encryptAES` to use a randomly generated salt and
modified the return value to include the salt, IV, and
encrypted content in a single output. Adjusted `decryptAES`
to extract the salt and IV from the input, ensuring the
decryption key is derived using the same salt used during
encryption. Enhanced decryption process to utilize the
extracted IV for AES-GCM operations.
2025-07-09 01:07:18 -04:00

111 lines
3.2 KiB
JavaScript

const siteInfo = [];
document.addEventListener("DOMContentLoaded", async function () {
var visibility = document.getElementById("visibility");
visibility.value = "0";
getSiteInfo();
});
document.getElementById("create-form").addEventListener("submit", async function (event) {
event.preventDefault();
var title = document.getElementById("title").value;
var content = document.getElementById("content").value;
var visibility = document.getElementById("visibility").value;
var password = document.getElementById("password").value;
var syntax = document.getElementById("syntax").value;
if (content.length > siteInfo.maxFileSize) {
showNotification("Content exceeds maximum size.", "error");
return;
}
if (content.length === 0) {
showNotification("Content cannot be empty.", "error");
return;
}
if (visibility === "2" && password !== "" && password.length >= 6) {
content = await encryptAES(content, password);
}
else if (visibility === "2") {
showNotification("Password must be at least 6 characters long.", "error");
return;
}
fetch(`/api/pastes/create?title=${title}&visibility=${visibility}&syntax=${syntax}`, {
method: "POST",
headers: {
'Content-Type': 'text/plain'
},
body: content
})
.then(response => response.json())
.then(data => {
const paste = data.paste;
window.location.href = `/${paste.id}`;
});
});
function changeVisibility() {
var visibility = document.getElementById("visibility").value;
var password = document.getElementById("password");
if (visibility === "2") {
password.style.display = "block";
} else {
password.style.display = "none";
}
}
function getSiteInfo() {
fetch("/api/site/info")
.then(response => response.json())
.then(data => {
siteInfo = data.info;
var content = document.getElementById("content")
content.setAttribute("maxlength", siteInfo.maxFileSize);
});
}
async function encryptAES(content, password) {
const enc = new TextEncoder();
const salt = crypto.getRandomValues(new Uint8Array(16));
const keyMaterial = await window.crypto.subtle.importKey(
"raw",
enc.encode(password),
{ name: "PBKDF2" },
false,
["deriveKey"]
);
const key = await window.crypto.subtle.deriveKey(
{
name: "PBKDF2",
salt: salt,
iterations: 100000,
hash: "SHA-256"
},
keyMaterial,
{ name: "AES-GCM", length: 256 },
false,
["encrypt"]
);
const iv = window.crypto.getRandomValues(new Uint8Array(12));
const encryptedContent = await window.crypto.subtle.encrypt(
{
name: "AES-GCM",
iv: iv
},
key,
enc.encode(content)
);
const combined = new Uint8Array(salt.length + iv.length + encryptedContent.byteLength);
combined.set(salt, 0);
combined.set(iv, salt.length);
combined.set(new Uint8Array(encryptedContent), salt.length + iv.length);
return btoa(String.fromCharCode.apply(null, combined));
}