-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
281 lines (270 loc) · 9.67 KB
/
index.html
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
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
</head>
<script>
var ciphertext = decodeURI("%ciphertext%");
var data = [];
var JsonFormatter = {
// this block copypastas from https://code.google.com/p/crypto-js/#The_Cipher_Output
stringify: function (cipherParams) {
// create json object with ciphertext
var jsonObj = {
ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64)
};
// optionally add iv and salt
if (cipherParams.iv) {
jsonObj.iv = cipherParams.iv.toString();
}
if (cipherParams.salt) {
jsonObj.s = cipherParams.salt.toString();
}
// stringify json object
return JSON.stringify(jsonObj);
},
parse: function (jsonStr) {
// parse json string
var jsonObj = JSON.parse(jsonStr);
// extract ciphertext from json object, and create cipher params object
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct)
});
// optionally extract iv and salt
if (jsonObj.iv) {
cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv)
}
if (jsonObj.s) {
cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s)
}
return cipherParams;
}
};
function decrypt() {
var password = $("#password-div input[type=password]")[0].value;
var decrypted = CryptoJS.AES.decrypt(ciphertext,password,{format:JsonFormatter});
var sites = JSON.parse(decrypted.toString(CryptoJS.enc.Utf8));
console.log(sites);
$("#password-div")[0].style.display = "none";
UI.init({"sites":sites});
}
function save() {
var newCleartext = JSON.stringify(UI.getSites());
var password = null;
if(ciphertext == "") {
password = $("#new-pass input[type=text]")[0].value;
} else {
password = $("#password-div input[type=password]")[0].value;
}
var newCiphertext = encodeURI(CryptoJS.AES.encrypt(newCleartext,password,{format:JsonFormatter}).toString());
$.ajax({
url: window.location.href,
dataType: "json",
type: "PUT",
data: newCiphertext,
success: function(data,status,req) {
console.log(JSON.parse(req.responseText));
console.log("success\t" + req);
},
error: function(req,status,err) {
console.log("error\t" + status);
}
});
}
var UI = {
addQuestion: function() {
var node = $("#save-div table:hidden tbody.site-body-input tr:last");
var targetNode = $("#save-div table:visible tbody.site-body-input tr:last");
var copy = node.clone(true);
targetNode.parent().append(copy);
UI.update();
},
addSite: function() {
UI.collectUpdate();
var node = $("#save-div table:hidden tbody.site-body-input");
var copy = node.clone(true);
var targetNode = $("#save-div table:visible tbody:last");
if(targetNode.length == 0) {
targetNode = $("#save-div table:visible thead:last");
targetNode.after(copy);
} else {
targetNode.after(copy);
}
UI.update();
},
update: function() {
var focusFun = function(ev){
if(ev.target.hasOwnProperty("touched")){
return;
}
ev.target["touched"] = true;
if(ev.target.tagName.match(/input/i)) {
ev.target.value="";
}
if(ev.target.tagName.match(/textarea/i)) {
ev.target.innerHTML="";
}
};
$("#save-div table:visible input[type=text]").focus(focusFun);
$("#save-div table:visible textarea").focus(focusFun);
},
init: function(options) {
$("#save-div")[0].style.display = "block";
var table = $("#save-div table");
var newTable = table.clone(true);
newTable[0].style.display = "block";
table.parent().append(newTable);
UI.update();
$("#save-div table:visible tbody:first").remove();
if(options.hasOwnProperty("new-pass") && options["new-pass"] == true) {
$("#new-pass")[0].style.display = "block";
}
if(options.hasOwnProperty("sites") && options["sites"].length > 0) {
$("#save-div table:visible tbody").remove();
var sites = options["sites"];
var targetNode = $("#save-div table:visible thead")
for(var i = 0; i < sites.length; i++) {
var site = sites[i];
var node = $("#save-div table:hidden tbody:first").clone(true);
node.find("tr:not(:first)").remove();
node.find("tr:first td:nth-child(1)")[0].innerHTML = site["name"];
node.find("tr:first td:nth-child(2)")[0].innerHTML = site["user"];
node.find("tr:first td:nth-child(3)")[0].innerHTML = site["pass"];
if(site.hasOwnProperty("questions")) {
for(var j = 0; j < site["questions"].length; j++) {
var question = site["questions"][j];
var qNode = $("#save-div table:hidden tbody.site-body tr:nth-child(2)").clone(true);
qNode.find("td:nth-child(2)")[0].innerHTM = question["question"];
qNode.find("td:nth-child(3)")[0].innerHTM = question["answer"];
node.append(qNode);
}
}
targetNode.after(node);
}
}
$("#save-div table:visible tbody.site-body").click(function(ev){
console.log($(ev.target).parents("tbody"));
});
},
collect: function() {
var nodes = $("#save-div table:visible tbody.site-body-input");
var sites = [];
for(var i = 0; i < nodes.length;i++) {
var site = {};
var node = $(nodes[i]);
site["name"] = node.find("tr:first td:nth-child(1) input[type=text]")[0].value;
site["user"] = node.find("tr:first td:nth-child(2) input[type=text]")[0].value;
site["pass"] = node.find("tr:first td:nth-child(3) input[type=text]")[0].value;
var questions = node.find("tr:not(:first)");
if(questions.length > 0) {
site["questions"] = [];
}
for(var j = 0;j< questions.length;j++) {
var question = {};
var qNode = $(questions[j]);
if(qNode.find("td:nth-child(2) textarea")[0].value == "") {
question["question"] = qNode.find("td:nth-child(2) textarea")[0].innerHTML;
} else {
question["question"] = qNode.find("td:nth-child(2) textarea")[0].value;
}
question["answer"] = qNode.find("td:nth-child(3) input[type=text]")[0].value;
site["questions"].push(question);
}
sites.push(site);
}
return sites;
},
collectUpdate: function() {
var sites = UI.collect();
var nodes = $("#save-div table:visible tbody.site-body-input");
nodes.remove();
for(var i = 0; i < sites.length; i++) {
var site = sites[i];
var node = $("#save-div table:hidden tbody.site-body").clone(true);
node.find("tr:first td:nth-child(1)")[0].innerHTML = site["name"];
node.find("tr:first td:nth-child(2)")[0].innerHTML = site["user"];
node.find("tr:first td:nth-child(3)")[0].innerHTML = site["pass"];
node.find("tr:not(tr:first)").remove();
if(site.hasOwnProperty("questions")) {
for(var j = 0; j < site["questions"].length; j++) {
var question = site["questions"][j];
var qNode = $("#save-div table:hidden tbody.site-body tr:nth-child(2)").clone(true);
qNode.find("td:nth-child(2)")[0].innerHTML = question.question;
qNode.find("td:nth-child(3)")[0].innerHTML = question.answer;
node.find("tr:last").after(qNode);
}
}
//if($("#save-div table:visible tbody:last").length == 0) {
$("#save-div table:visible thead:last").after(node);
//} else {
// $("#save-div table:visible tbody:last").after(node);
//}
}
return sites;
},
getSites: function() {
UI.collectUpdate();
var nodes = $("#save-div table:visible tbody.site-body");
var sites = [];
for(var i = 0; i < nodes.length;i++) {
var site = {};
var node = $(nodes[i]);
site["name"] = node.find("tr:first td:nth-child(1)")[0].innerHTML;
site["user"] = node.find("tr:first td:nth-child(2)")[0].innerHTML;
site["pass"] = node.find("tr:first td:nth-child(3)")[0].innerHTML;
var questions = node.find("tr:not(:first)");
if(questions.length > 0) {
site["questions"] = [];
}
for(var j = 0;j< questions.length;j++) {
var question = {};
var qNode = $(questions[j]);
question["question"] = qNode.find("td:nth-child(2)")[0].innerHTML;
question["answer"] = qNode.find("td:nth-child(3)")[0].innerHTML;
site["questions"].push(question);
}
sites.push(site);
}
return sites;
}
};
$(document).ready(function() {
if(ciphertext == "") {
$("#password-div")[0].style.display = "none";
UI.init({"new-pass":true});
}
});
</script>
<style>
#save-div {
display: none;
}
.hover:hover {
background: green;
}
</style>
<body>
<div id="password-div">
<p>Enter password: <input type="password"> <input type="button" value="Decrypt" onclick="decrypt()"></p>
</div>
<div id="save-div">
<div id="new-pass" style="display:none;">New pass: <input type="text"></input></div><input type="button" value="Save" onclick="save()">
<table style="display:none">
<thead>
<tr><th>Site</th><th>Username</th><th>Password</th></tr>
<tr><th></th><th>Secret Question</th><th>Answer</th></tr>
</thead>
<tbody class="site-body hover">
<tr><td>example.com</td><td>user@example.com</td><td>testpass</td></tr>
<tr><td></td><td>Question 1</td><td>Answer 1</td></tr>
</tbody>
<tbody class="site-body-input">
<tr><td><input type="text" value="Site name" class="site-name"></td><td><input type="text" value="Site-specific email" class="site-user"></td><td><input type="text" value="password" class="site-pass"></td></tr>
<tr><td></td><td><textarea class="site-question">Security Question</textarea></td><td><input type="text" value="Answer" class="site-answer"></td></tr>
</tbody>
</table>
<input type="button" value="Add security question" onclick="UI.addQuestion()"><input type="button" value="Add site" onclick="UI.addSite()">
</div>
</body>
</html>