Skip to content

Commit

Permalink
Merge pull request #73 from AskitEndo/impoHashFix
Browse files Browse the repository at this point in the history
Impoorting Hash fix #62
  • Loading branch information
sudo-dpkg authored Oct 16, 2024
2 parents 89e8405 + 97ffbc1 commit 2b95bcb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 46 deletions.
39 changes: 29 additions & 10 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,31 @@ const IV_LENGTH = 16; // For AES, this is always 16
// Encrypt a password
const encrypt = (text) => {
const iv = crypto.randomBytes(IV_LENGTH);
const cipher = crypto.createCipheriv("aes-256-cbc", Buffer.from(ENCRYPTION_KEY), iv);
const cipher = crypto.createCipheriv(
"aes-256-cbc",
Buffer.from(ENCRYPTION_KEY),
iv
);
let encrypted = cipher.update(text, "utf8", "hex");
encrypted += cipher.final("hex");
return iv.toString("hex") + ":" + encrypted; // Store IV with the encrypted password
};

// Decrypt function
function decrypt(text) {
let ivBuffer = Buffer.from(text.iv, "hex");
let encryptedText = text.encryptedData;
const decrypt = (text) => {
const [iv, encryptedData] = text.split(":");
const ivBuffer = Buffer.from(iv, "hex");

const decipher = crypto.createDecipheriv(
"aes-256-cbc",
Buffer.from(ENCRYPTION_KEY),
ivBuffer
);
let decrypted = decipher.update(encryptedData, "hex", "utf-8");

let decipher = crypto.createDecipheriv("aes-256-cbc", Buffer.from(ENCRYPTION_KEY), ivBuffer);
let decrypted = decipher.update(encryptedText, "hex", "utf-8");
decrypted += decipher.final("utf-8");

return decrypted;
}
};

// Connecting to the MongoDB Client
const url = process.env.MONGO_URI;
Expand Down Expand Up @@ -107,7 +115,11 @@ app.post("/", async (req, res) => {
const collection = db.collection("passwords");
// Encrypt the password before saving
const encryptedPassword = encrypt(password);
const result = await collection.insertOne({ site, username, password: encryptedPassword });
const result = await collection.insertOne({
site,
username,
password: encryptedPassword,
});
res.status(201).json({ success: true, result });
} catch (error) {
console.error("Error saving password:", error);
Expand Down Expand Up @@ -196,9 +208,16 @@ app.get("/export", async (req, res) => {
const db = client.db(dbName);
const passwords = await db.collection("passwords").find({}).toArray();

// Decrypt each password before exporting
const decryptedPasswords = passwords.map((password) => ({
site: password.site,
username: password.username,
password: decrypt(password.password), // Directly decrypt the stored password
}));

res.setHeader("content-Type", "application/json");
res.setHeader("content-disposition", "attachment; filename=passwords.json");
res.status(200).json(passwords);
res.status(200).json(decryptedPasswords);
} catch (error) {
console.error("Error exporting passwords:", error);
res
Expand Down
68 changes: 32 additions & 36 deletions src/components/Manager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,12 @@ const Manager = () => {
return errors.length === 0;
};
const savePassword = async () => {

const errors = [];

// Validate Site URL
if (form.site.length <= 3 || !validateURL(form.site)) {
errors.push(
"Error: Invalid site name. Ensure it meets the required format."

);
}

Expand Down Expand Up @@ -133,41 +131,39 @@ const Manager = () => {
return; // Exit if there are validation errors
}

// Proceed to save the password if all validations pass
if (form._id) {
const updatedPasswords = passwordArray.map((item) =>
item._id === form._id ? { ...form } : item
);
setPasswordArray(updatedPasswords);
await fetch(`http://localhost:3000/${form._id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(form),
});
getPasswords();
} else {
const newPassword = { ...form };
setPasswordArray([...passwordArray, newPassword]);
await fetch("http://localhost:3000/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newPassword),
});
getPasswords();
}

setForm({ _id: "", site: "", username: "", password: "" });
toast("Password saved!", {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
theme: "dark",
if (form.id) {
const updatedPasswords = passwordArray.map((item) =>
item._id === form.id ? { ...form } : item
);
setPasswordArray(updatedPasswords);
await fetch(`http://localhost:3000/${form.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(form),
});
} else {
const newPassword = { ...form, id: uuidv4() };
setPasswordArray([...passwordArray, newPassword]);
await fetch("http://localhost:3000/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newPassword),
});
}
}

// Clear form and show success toast
setForm({ id: "", site: "", username: "", password: "" });
toast("Password saved!", {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
theme: "dark",
});
};

const deletePassword = async (id) => {
const confirmDelete = confirm(
Expand Down

0 comments on commit 2b95bcb

Please sign in to comment.