This repository was archived by the owner on Mar 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
328 lines (291 loc) · 8.75 KB
/
index.mjs
File metadata and controls
328 lines (291 loc) · 8.75 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import inquirer from "inquirer";
import fs from "fs";
import crypto from "crypto";
import chalk from "chalk";
import figlet from "figlet";
function clear() {
process.stdout.write('\x1Bc');
}
function exitHandler() {
console.log(chalk.yellow("\nProgramdan çıkılıyor... İyi günler!"));
process.exit();
}
process.on('SIGINT', exitHandler);
const PASSWORDS_FILE = "passwords.dat";
const ENCRYPT_KEY_FILE = "encrypt_key.dat";
function generateAndStoreEncryptKey(key) {
const salt = crypto.randomBytes(16);
const encryptKey = crypto.pbkdf2Sync(key, salt, 100000, 32, "sha512");
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv("aes-256-gcm", encryptKey, iv);
let encrypted = cipher.update(key, "utf8");
encrypted = Buffer.concat([encrypted, cipher.final()]);
const authTag = cipher.getAuthTag();
const dataToStore = Buffer.concat([
salt,
iv,
authTag,
Buffer.from([encrypted.length]),
encrypted,
]);
fs.writeFileSync(ENCRYPT_KEY_FILE, dataToStore);
return encryptKey;
}
function getEncryptKey(key) {
const storedData = fs.readFileSync(ENCRYPT_KEY_FILE);
const salt = storedData.slice(0, 16);
const iv = storedData.slice(16, 32);
const authTag = storedData.slice(32, 48);
const encryptedLength = storedData[48];
const encrypted = storedData.slice(49, 49 + encryptedLength);
const encryptKey = crypto.pbkdf2Sync(key, salt, 100000, 32, "sha512");
const decipher = crypto.createDecipheriv("aes-256-gcm", encryptKey, iv);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(encrypted);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return crypto.pbkdf2Sync(
decrypted.toString("utf8"),
salt,
100000,
32,
"sha512"
);
}
let encryptKey;
let passwords = {};
function loadPasswords() {
if (fs.existsSync(PASSWORDS_FILE)) {
const data = fs.readFileSync(PASSWORDS_FILE);
let offset = 0;
while (offset < data.length) {
const nameLength = data.readUInt8(offset);
offset += 1;
const name = data.slice(offset, offset + nameLength).toString('utf8');
offset += nameLength;
const ivLength = data.readUInt8(offset);
offset += 1;
const iv = data.slice(offset, offset + ivLength);
offset += ivLength;
const authTagLength = data.readUInt8(offset);
offset += 1;
const authTag = data.slice(offset, offset + authTagLength);
offset += authTagLength;
const encryptedDataLength = data.readUInt16BE(offset);
offset += 2;
const encryptedData = data.slice(offset, offset + encryptedDataLength).toString('hex');
offset += encryptedDataLength;
passwords[name] = {
iv: iv.toString('hex'),
authTag: authTag.toString('hex'),
encryptedData: encryptedData
};
}
}
}
function savePasswords() {
let data = Buffer.alloc(0);
for (const [name, password] of Object.entries(passwords)) {
const nameBuffer = Buffer.from(name, 'utf8');
const ivBuffer = Buffer.from(password.iv, 'hex');
const authTagBuffer = Buffer.from(password.authTag, 'hex');
const encryptedDataBuffer = Buffer.from(password.encryptedData, 'hex');
data = Buffer.concat([
data,
Buffer.from([nameBuffer.length]),
nameBuffer,
Buffer.from([ivBuffer.length]),
ivBuffer,
Buffer.from([authTagBuffer.length]),
authTagBuffer,
Buffer.alloc(2),
encryptedDataBuffer
]);
data.writeUInt16BE(encryptedDataBuffer.length, data.length - encryptedDataBuffer.length - 2);
}
fs.writeFileSync(PASSWORDS_FILE, data);
}
async function initializeEncryptKey() {
if (!fs.existsSync(ENCRYPT_KEY_FILE)) {
const { key } = await inquirer.prompt({
type: "password",
name: "key",
message: "Lütfen yeni bir şifreleme anahtarı girin:",
mask: "*",
});
const { confirmKey } = await inquirer.prompt({
type: "password",
name: "confirmKey",
message: "Lütfen şifreleme anahtarını tekrar girin:",
mask: "*",
});
if (key !== confirmKey) {
console.log(
chalk.red("Şifreleme anahtarları eşleşmiyor. Lütfen tekrar deneyin.")
);
return initializeEncryptKey();
}
const { confirm } = await inquirer.prompt({
type: "confirm",
name: "confirm",
message: "Şifreleme anahtarını onaylıyor musunuz?",
});
if (!confirm) {
return initializeEncryptKey();
}
encryptKey = generateAndStoreEncryptKey(key);
} else {
const { key } = await inquirer.prompt({
type: "password",
name: "key",
message: "Lütfen mevcut şifreleme anahtarınızı girin:",
mask: "*",
});
try {
encryptKey = getEncryptKey(key);
} catch (error) {
console.log(
chalk.red("Yanlış şifreleme anahtarı. Lütfen tekrar deneyin.")
);
return initializeEncryptKey();
}
}
loadPasswords();
}
function encrypt(text) {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv("aes-256-gcm", encryptKey, iv);
let encrypted = cipher.update(text, "utf8", "hex");
encrypted += cipher.final("hex");
const authTag = cipher.getAuthTag();
return {
iv: iv.toString("hex"),
encryptedData: encrypted,
authTag: authTag.toString("hex"),
};
}
function decrypt(encryptedObj) {
const decipher = crypto.createDecipheriv(
"aes-256-gcm",
encryptKey,
Buffer.from(encryptedObj.iv, "hex")
);
decipher.setAuthTag(Buffer.from(encryptedObj.authTag, "hex"));
let decrypted = decipher.update(encryptedObj.encryptedData, "hex", "utf8");
decrypted += decipher.final("utf8");
return decrypted;
}
function displayBanner() {
clear();
console.log(
chalk.yellow(
figlet.textSync("Şifre Yöneticisi", { horizontalLayout: "full" })
)
);
}
async function main() {
await initializeEncryptKey();
const initialQuestion = {
type: "list",
name: "action",
message: "Ne yapmak istersiniz?",
choices: [
{ name: chalk.green("Şifreleri Görüntüle"), value: "Görüntüle" },
{ name: chalk.blue("Yeni Şifre Oluştur"), value: "Oluştur" },
{ name: chalk.red("Şifre Sil"), value: "Sil" },
{ name: chalk.gray("Çıkış"), value: "Çıkış" },
],
};
while (true) {
displayBanner();
console.log(chalk.cyan("Hoş geldiniz! Lütfen bir işlem seçin.\n"));
const { action } = await inquirer.prompt(initialQuestion);
switch (action) {
case "Görüntüle":
await handleView();
break;
case "Oluştur":
await handleCreate();
break;
case "Sil":
await handleDelete();
break;
case "Çıkış":
exitHandler();
return;
}
console.log(chalk.dim("\nDevam etmek için bir tuşa basın..."));
await inquirer.prompt({ type: "input", name: "continue", message: "" });
}
}
async function handleView() {
clear();
displayBanner();
console.log(chalk.cyan("Kaydedilmiş Şifreler:\n"));
if (Object.keys(passwords).length === 0) {
console.log(chalk.yellow("Henüz kaydedilmiş şifre bulunmamaktadır."));
return;
}
for (let [applicationName, encryptedPassword] of Object.entries(passwords)) {
try {
const decryptedPassword = decrypt(encryptedPassword);
console.log(
chalk.green(`${applicationName}:`),
chalk.white(decryptedPassword)
);
} catch (error) {
console.log(chalk.red(`${applicationName}: Şifre çözülemedi`));
}
}
}
async function handleCreate() {
clear();
displayBanner();
console.log(chalk.cyan("Yeni Şifre Oluştur:\n"));
const questions = [
{
type: "input",
name: "applicationName",
message: "Uygulama adı nedir?",
default: "uygulama-ismi",
},
{
type: "password",
name: "password",
message: "Şifre nedir?",
mask: "*",
},
];
const answers = await inquirer.prompt(questions);
passwords[answers.applicationName] = encrypt(answers.password);
savePasswords();
console.log(
chalk.green(
`\n"${answers.applicationName}" için şifre başarıyla kaydedildi.`
)
);
}
async function handleDelete() {
clear();
displayBanner();
console.log(chalk.cyan("Şifre Sil:\n"));
if (Object.keys(passwords).length === 0) {
console.log(chalk.yellow("Silinecek şifre bulunmamaktadır."));
return;
}
const question = {
type: "list",
name: "projectToDelete",
message: "Hangi projenin şifresini silmek istersiniz?",
choices: Object.keys(passwords),
};
const { projectToDelete } = await inquirer.prompt(question);
delete passwords[projectToDelete];
savePasswords();
console.log(
chalk.green(`\n"${projectToDelete}" için şifre başarıyla silindi.`)
);
}
main().catch((error) => {
console.error(chalk.red('Bir hata oluştu'));
exitHandler();
});