-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
165 lines (136 loc) · 5.26 KB
/
main.js
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
$(document).ready(function() {
var passwords = [];
var debug = false;
$("#create-button").click(function() {
debugLog("Create Database button clicked");
$("#buttons-container").hide();
$("#content-container").show();
renderPasswords();
});
$("#load-button").click(function() {
debugLog("Load Database button clicked");
var fileInput = $("<input>").attr({
type: "file",
accept: ".json",
}).css("display", "none");
fileInput.change(function(event) {
var file = event.target.files[0];
var reader = new FileReader();
reader.onload = function(e) {
debugLog("File loaded for import");
var fileContent = e.target.result;
var masterPassword = prompt("Enter your master password:");
debugLog("Master password entered");
if (masterPassword !== null) {
try {
var decryptedContent = decrypt(fileContent, masterPassword);
if (decryptedContent !== null) {
passwords = JSON.parse(decryptedContent);
$("#buttons-container").hide();
$("#content-container").show();
renderPasswords();
} else {
alert("Incorrect master password!");
}
} catch (error) {
alert("Error decrypting the file!");
}
}
};
reader.readAsText(file);
});
fileInput.click();
});
$("#debug-button").click(function() {
debug = !debug;
$("#debug-button").text(`Debug: ${debug ? "On" : "Off"}`);
debugLog(`Debug mode: ${debug ? "On" : "Off"}`);
});
function renderPasswords() {
debugLog("Rendering passwords");
var contentContainer = $("#content-container");
contentContainer.html("");
$.each(passwords, function(index, password) {
var passwordEntry = $("<div>").addClass("password-entry");
var passwordBox = $("<div>").addClass("password-box");
var nameInput = $("<input>").attr({
type: "text",
value: password.name,
readonly: true,
});
var passwordInput = $("<input>").attr({
type: "password",
value: password.password,
readonly: true,
});
var copyButton = $("<button>").text("Copy");
copyButton.click(function() {
copyToClipboard(password.password);
});
passwordBox.append(nameInput, passwordInput, copyButton);
passwordEntry.append(passwordBox);
contentContainer.append(passwordEntry);
});
var addButton = $("<button>").attr("id", "add-button").text("Add");
var exportButton = $("<button>").attr("id", "export-button").text("Export");
addButton.click(function() {
var name = prompt("Enter name:");
var password = prompt("Enter password:");
if (name !== null && password !== null) {
passwords.push({
name: name,
password: password
});
renderPasswords();
}
});
exportButton.click(function() {
var masterPassword = prompt("Enter your master password:");
if (masterPassword !== null) {
var encryptedContent = encrypt(JSON.stringify(passwords), masterPassword);
download(encryptedContent, "passwords.json", "application/json");
}
});
contentContainer.append(addButton, exportButton);
if (passwords.length > 3) {
contentContainer.addClass("more-than-three-passwords");
} else {
contentContainer.removeClass("more-than-three-passwords");
}
}
function copyToClipboard(text) {
var tempInput = $("<input>").val(text).appendTo("body").select();
document.execCommand("copy");
tempInput.remove();
debugLog("Password copied to clipboard");
alert("Password copied to clipboard!");
}
function debugLog(message) {
if (debug) {
console.log(message);
}
}
function encrypt(content, password) {
debugLog("Encrypting content");
var encryptedContent = CryptoJS.AES.encrypt(content, password).toString();
return encryptedContent;
}
function decrypt(content, password) {
debugLog("Decrypting content");
try {
var decryptedContent = CryptoJS.AES.decrypt(content, password).toString(CryptoJS.enc.Utf8);
return decryptedContent;
} catch (error) {
return null;
}
}
function download(content, fileName, contentType) {
debugLog("Downloading file");
var a = $("<a>").attr({
href: "data:" + contentType + ";charset=utf-8," + encodeURIComponent(content),
download: fileName,
}).appendTo("body");
a[0].click();
a.remove();
}
});