-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecretnotes.html
543 lines (491 loc) · 23.9 KB
/
secretnotes.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<!doctype html>
<!--
A local based secret notes manager by Maurice Snoeren (macsnoeren(at)gmail.com)
2022-11-21: Initial version.
2025-02-01: Updated libraries!
When you do not want to rely on servers, services or third parties. This
straigthforward password manager encrypts all your passwords and stores
it locally at your browsers local storages. Everything is encrypted in
that store. When you type in the password, the encrypted passwords will
be visualized in the browser. No server communication, just on your local
computer or browser. You can check the source code!
Use it at your own risk!
GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007
-->
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha512-jnSuA4Ss2PkkikSOLtYs8BlYIeeIK1h99ty4YfvRPAlzr377vr3CXDb7sb7eEEBYjDtcYj+AjBH3FLv5uSJuXg==" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/js/bootstrap.bundle.min.js" integrity="sha512-7Pi/otdlbbCR+LnW+F7PwFcSDJOuUJB3OxtEHbg4vSMvzvJjde4Po1v4BR9Gdc9aXNUNFVUY+SK51wWT8WF0Gg==" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/crypto-js.min.js" integrity="sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==" crossorigin="anonymous"></script>
<style>
/* The switch - the box around the slider */
.switch {
position: relative;
display: inline-block;
width: 40px;
height: 20px;
}
/* Hide default HTML checkbox */
.switch input {
opacity: 0;
width: 0;
height: 0;
}
/* The slider */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 13px;
width: 13px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(20px);
-ms-transform: translateX(20px);
transform: translateX(20px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
.dark-mode {
background-color: black;
color: white;
--input-bg-color: gray;
--input-tx-color: white;
}
#vault, #password3, #vault-label, #password3-label, #source, #username, #newpassword {
background-color: var(--input-bg-color);
color: var(--input-tx-color);
}
</style>
<script>
// Check if the browser implement localStorage
console.log("A local based password manager started...");
if (typeof(Storage) == "undefined") {
alert("Sorry, your browser does not support this application! Update your browser to support localStorage!");
console.log("Sorry, your browser does not support this application! Update your browser to support localStorage!");
}
// Holds the vaultfile
var vaultfile = null;
var vaultOpened = false;
// Function that checks if the application has been initialized.
// When it is initialized, the localStorage contains a vault and an information key.
// If this is not yet the case, the user gets the form to start the application.
function applicationStarted() {
return retrieve("vault") !== null && retrieve("information") !== null;
}
// Function that stores the value to a belonging key in the local storage.
function store(key, value) {
localStorage.setItem(key, value);
}
// Function that retrieves the value to a belonging key from the local storage.
function retrieve(key) {
return localStorage.getItem(key);
}
// Function that shows the information on the web page. <div id="information"></div>
function showInformation(data) {
$("#information").html(data);
}
// Function that shows the vault information on the web page. <div id="vault"></div>
// The creation and update dates are shown in the information part of the web page.
function showVault(data) {
showInformation("<i>Vault created: " + data.created + " and last updated: " + data.updated + "</i>");
$("#vault").val(data.vault);
}
// Function that shows the error on the web page. <div id="error"></div>
function showError(error) {
alertMessage(error, 'danger');
}
// Function that shows the message on the web page. <div id="message"></div>
function showMessage(message) {
alertMessage(message, 'success');
}
// Empty the password inputs
function resetPasswordInputs () {
$("#password1").val(""); $("#password2").val(""); $("#password3").val("");
}
// Reset the user interface and start over.
function resetUserInterface () {
resetPasswordInputs();
$("#information").val(""); $("#vault").val(""); $("#error").val(""); $("#password").hide();
$("#application_start").show(); $("#application_active").hide();
}
// Show the setup user interface screen
function showSetupScreen () {
$("#application_start").show();
$("#application_active").hide();
$("#password").hide();
}
// Show the application user interface screen
function showApplicationScreen () {
$("#application_start").hide();
$("#application_active").show();
$("#password").show();
}
// Function that clears the vault and resets the application. Note that everything will be erased!
function clearVault() {
localStorage.clear("vault");
localStorage.clear("information");
resetUserInterface();
showMessage("Vault is successfully cleared!")
vaultOpened = false;
vaultfile = null;
}
// Create a vault based on the two passwords that have been provided.
// Passwords need to match and all the information is stored in the local storage.
function createVault() {
password1 = $("#password1").val();
password2 = $("#password2").val();
if ( password1 != "" && password1 === password2 ) {
vault = JSON.stringify({ created: new Date(), updated: new Date(), vault: "This is your vault..." });
vault_enc = CryptoJS.AES.encrypt(vault, password1);
vault = ""; // Remove it again...
key_size = vault_enc.key.toString().length*8;
iv_size = vault_enc.iv.toString().length*8
salt_size = vault_enc.salt.toString().length*8
if ( key_size > 255 && iv_size > 100 && salt_size > 100 ) {
store("vault", vault_enc);
information = JSON.stringify( { created: new Date(), key_size: key_size, iv_size: iv_size, salt_size: salt_size } );
information_enc = CryptoJS.AES.encrypt(vault, password1);
store("information", information_enc);
showApplicationScreen();
showMessage("Vault is successfully created!")
} else {
showError("Cipher keys are not correctly generated, please try it again!");
}
} else {
showError("Password cannot be empty or passwords do not match!");
}
resetPasswordInputs();
}
// Function that reads all the information from the local storage. Opening the vault,
// while everything needs to be decrypted based on the password provided.
function openVault() {
try {
password = $("#password3").val();
vault_enc = retrieve("vault");
vault = CryptoJS.AES.decrypt(vault_enc, password).toString(CryptoJS.enc.Utf8);
vault = JSON.parse(vault);
showMessage("Vault successfully opened.");
showVault(vault);
vaultOpened = true;
}
catch(err) {
showError("Could not open the vault, is your password correct?");
}
$("#password3").val("");
}
// Function that saves the secret information to the vault. Everything is encrypt
// by the provided password. Before saving, the vault is first retrieved to update
// the creation date information. When the password is not correct, it will not
// save the vault information.
function saveVault() {
try {
password = $("#password3").val();
// Get the saved vault first
vault_enc = retrieve("vault");
vault = CryptoJS.AES.decrypt(vault_enc, password).toString(CryptoJS.enc.Utf8);
vault = JSON.parse(vault);
// Update the vault with the existing values
vault.vault = $("#vault").val();
vault.updated = new Date();
// Save the vault again
vault = JSON.stringify(vault);
vault_enc = CryptoJS.AES.encrypt(vault, password);
vault = ""; // Remove it again...
key_size = vault_enc.key.toString().length*8;
iv_size = vault_enc.iv.toString().length*8
salt_size = vault_enc.salt.toString().length*8
if ( key_size > 255 && iv_size > 100 && salt_size > 100 ) {
store("vault", vault_enc)
showMessage("Vault successfully saved.")
} else {
showError("Cipher keys are not correctly generated, please try it again!");
}
}
catch(err) {
showError("Could not save the vault, is your password correct?");
console.log(err)
}
$("#password3").val("");
}
// Function returns the encrypted vault as it is stored in the local storage.
function getRawVault() {
rawVault = {
vault: retrieve("vault"),
information: retrieve("information")
}
return JSON.stringify(rawVault);
}
// Function that reads the local file, that has been selected by the user, and store it in the local storage.
function loadRawVault() {
if (!vaultfile) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
try {
var contents = e.target.result;
vault = JSON.parse(contents);
if ( "vault" in vault && "information" in vault ) {
store("vault", vault.vault);
store("information", vault.information);
vaultfile = null;
showApplicationScreen();
resetPasswordInputs();
showInformation("");
showMessage("Successfully loaded the vault file.");
}
}
catch(err) {
showError("Could not read the vault file.");
}
};
reader.readAsText(vaultfile);
}
// Bootstrap functions
function alertMessage(message, type) {
$("#liveAlertPlaceholder").append(
'<div class="alert alert-' + type + ' alert-dismissible fade show" style="position: fixed; width: 100%" role="alert">' +
' <div>' + message + '</div>' +
' <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>' +
'</div>' +
'<script>window.setTimeout(function() { $(".alert").fadeTo(500, 0).slideUp(500, function() { $(this).remove(); }); }, 4000);<\/script>'
);
}
// Function the implements the download function by javascript.
function download(filename, text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
if (document.createEvent) {
var event = document.createEvent('MouseEvents');
event.initEvent('click', true, true);
pom.dispatchEvent(event);
}
else {
pom.click();
}
}
// This function toggles the dark and light mode. It stores the result into the localstorage.
function toggleMode (value) {
document.body.classList.toggle("dark-mode");
if ( retrieve("darkmode") == "true" ) {
store("darkmode", false);
} else {
store("darkmode", true);
}
}
// Adds the new generated password to the password vault textarea, function belongs to the model box
function addPassword2Vault () {
var newPassword = "PASSWORD " + $("#source").val() + ": " + $("#username").val() + " / " + $("#newpassword").val() + "\n";
$("#vault").val($("#vault").val() + newPassword);
resetNewPasswordModal();
}
// Reset the model box, so all values are empty
function resetNewPasswordModal() {
$("#source").val("");
$("#username").val("");
$("#newpassword").val("");
}
// Generate a strong password of 25 characters
function generateNewPassword () {
var chars = "0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var passwordLength = 25;
var password = "";
for (var i = 0; i <= passwordLength; i++) {
var randomNumber = Math.floor(Math.random() * chars.length);
password += chars.substring(randomNumber, randomNumber +1);
}
$("#newpassword").val(password);
}
// Check if enter has been pressed and execute the open and save vault automatically.
function processActions () {
if (event.keyCode == 13) {
if ( vaultOpened ) {
saveVault();
} else {
openVault();
}
}
}
</script>
</head>
<body>
<!-- Bootstrap Modal Clear Vault -->
<div class="modal fade" id="clearVaultModal" tabindex="-1" aria-labelledby="clearVaultModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div style="background-color: red;" class="modal-header">
<h1 class="modal-title fs-5" id="clearVaultModalLabel">Are you sure to clear your vault?</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div style="background-color: var(--input-bg-color);" class="modal-body">
By clearing your vault all data will be lost and cannot be recovered! This is a destructive function!
</div>
<div style="background-color: var(--input-bg-color);" class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-danger" onclick="clearVault();" data-bs-dismiss="modal">Clear vault</button>
</div>
</div>
</div>
</div>
<!-- Bootstrap Modal Add Password-->
<div class="modal fade" id="newPasswordModal" tabindex="-1" aria-labelledby="newPasswordModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div style="background-color: var(--input-bg-color);" class="modal-header">
<h1 class="modal-title fs-5" id="newPasswordModalLabel">Add password</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div style="background-color: var(--input-bg-color);" class="modal-body">
<label for="source" class="form-label">Source/URL</label>
<input id="source" type="text" class="form-control">
<label for="username" class="form-label">Username</label>
<input id="username" type="text" class="form-control">
<label for="newpassword" class="form-label">Password</label>
<input id="newpassword" type="text" class="form-control">
</div>
<div style="background-color: var(--input-bg-color);" class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" onclick="resetNewPasswordModal()">Close</button>
<button type="button" class="btn btn-success" onclick="generateNewPassword();">Generate</button>
<button type="button" class="btn btn-primary" onclick="addPassword2Vault();" data-bs-dismiss="modal">Add</button>
</div>
</div>
</div>
</div>
<!-- The web application part -->
<div id="liveAlertPlaceholder"></div>
<div class="container">
<div id="application_start">
<h1>Your local secret notes manager!</h1>
<p>Client based, no cookies and no communication with the Internet. It do stores the vault AES encrypted in your local browser storage (<5MB).<br/>
Please initialize your vault to store your secrets by setting your ROOT password. Note, when you forget this password, the information is lost.</p>
<form class="row">
<div class="col border p-3">
<div class="col-mb-12">
<h4>Create your vault:</h4>
</div>
<div class="col-mb-12">
<label for="formGroupPassword1" class="form-label">Password</label>
<input id="password1" type="password" class="form-control">
</div>
<div class="col-mb-12">
<label for="formGroupPassword2" class="form-label">Verify password</label>
<input id="password2" type="password" class="form-control">
</div>
<div class="col-mb-12">
<br/>
<button type="button" class="btn btn-primary btn-sm" onclick="createVault();">Create!</button>
</div>
</div>
<div class="col border p-3">
<div class="col-mb-12">
<h4>Load your vault:</h4>
</div>
<div class="col-mb-12">
<input type="file" class="form-control" id="vaultfile">
</div>
<div class="col-mb-12">
<br/>
<button type="button" class="btn btn-primary btn-sm" onclick="loadRawVault();">Load!</button>
</div>
</div>
</form>
</div>
<div id="application_active">
<div id="application_header">
<div style="float: left;">
<h1>Your local secret notes manager!
<button type="button" class="btn btn-danger btn-sm" data-bs-toggle="modal" data-bs-target="#clearVaultModal">Clear vault</button>
</h1>
<p>Client based, no cookies and no communication with the Internet. It do stores the vault in your local browser storage (<5MB).<br/>
Great, you already have initialized a vault. Type your password and open your secrets!</p>
</div>
<div style="margin-top: 10px; float: right;">
<button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#newPasswordModal">Add new password</button>
<div style="text-align: right;">dark mode </div>
<div style="text-align: right;">
<label class="switch">
<input id="darkmode" type="checkbox" onchange="toggleMode();" value="1">
<span class="slider round"></span>
</label>
</div>
</div>
<div style="clear: both;"></div>
</div>
<div class="input-group">
<span id="password3-label" class="input-group-text">Your password</span>
<input id="password3" type="password" class="form-control" style="width: 250px" onkeydown="processActions()"/>
<button type="button" class="btn btn-primary btn-sm" onclick="openVault();">Open</button>
<button type="button" class="btn btn-success btn-sm" onclick="saveVault(); $('#password3').val('');">Save</button>
</div>
<div style="padding-top: 10px;">
<div id="information"></div>
<div class="input-group">
<span id="vault-label" class="input-group-text">Vault</span>
<textarea id="vault" rows="20" class="form-control" aria-label="Vault" placeholder="Your vault has not been loaded yet, please open your vault..."></textarea>
</div>
</div>
<p style="padding-top: 10px;">
<button type="button" class="btn btn-info btn-sm" onclick="download('vault.txt', getRawVault());">Download vault</button>
</p>
</div>
</div>
<script>
resetUserInterface();
// Initialize the dark mode
if ( retrieve("darkmode") === null ) {
store("darkmode", false);
} else {
if ( retrieve("darkmode") == "true" ) {
$("#darkmode").prop("checked", true);
document.body.classList.toggle("dark-mode");
}
}
// When the page is loaded, start the application and show the appropiate parts of the application.
if ( applicationStarted() ) {
showApplicationScreen();
} else {
showSetupScreen();
}
// Prevent the annoying ctr+s for people that do want to save
document.addEventListener('keydown', e => {
if (e.ctrlKey && e.key === 's') {
e.preventDefault();
}
});
// When a file is selected to be loaded as vault, the file is stored, so the file can be read.
$("#vaultfile").change( function(e) {
vaultfile = e.target.files[0];
});
</script>
</body>
</html>