| const fs = require("fs"); |
| const path = require("path"); |
| const crypto = require("crypto"); |
|
|
| |
| |
| |
| |
| |
| function isValidDirectoryName(dirName) { |
| const regex = /^[a-zA-Z0-9_-]+$/; |
| return regex.test(dirName); |
| } |
|
|
| |
| |
| |
| |
| |
| function hashPassword(password) { |
| return crypto.createHash("sha256").update(password).digest("hex"); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function verifyPassword(password, hash) { |
| return hashPassword(password) === hash; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function createUserDirectory(baseDir, dirName, password) { |
| const dirPath = path.join(baseDir, dirName); |
|
|
| if (fs.existsSync(dirPath)) { |
| throw new Error("Directory already exists"); |
| } |
|
|
| |
| fs.mkdirSync(dirPath, { recursive: true }); |
|
|
| |
| const hashedPassword = hashPassword(password); |
| fs.writeFileSync(path.join(dirPath, "password.txt"), hashedPassword, "utf8"); |
|
|
| console.log(`Directory ${dirName} created successfully`); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function deleteUserDirectory(baseDir, dirName, password) { |
| const dirPath = path.join(baseDir, dirName); |
|
|
| if (!fs.existsSync(dirPath)) { |
| throw new Error("Directory does not exist"); |
| } |
|
|
| |
| const passwordFile = path.join(dirPath, "password.txt"); |
| if (!fs.existsSync(passwordFile)) { |
| throw new Error("Password file is missing"); |
| } |
|
|
| const storedHash = fs.readFileSync(passwordFile, "utf8"); |
| if (!verifyPassword(password, storedHash)) { |
| throw new Error("Invalid password"); |
| } |
|
|
| |
| fs.rmSync(dirPath, { recursive: true, force: true }); |
| console.log(`Directory ${dirName} deleted successfully`); |
| } |
|
|
| |
| |
| |
| |
| function logMessage(message) { |
| const timestamp = new Date().toISOString(); |
| console.log(`[${timestamp}] ${message}`); |
| } |
|
|
| module.exports = { |
| isValidDirectoryName, |
| hashPassword, |
| verifyPassword, |
| createUserDirectory, |
| deleteUserDirectory, |
| logMessage, |
| }; |