major refactoring
- created a package.json - refactor with eslint - use modules
This commit is contained in:
parent
b9a56f38f5
commit
f51ee13176
21
.eslintrc.yml
Normal file
21
.eslintrc.yml
Normal file
@ -0,0 +1,21 @@
|
||||
env:
|
||||
browser: true
|
||||
es2021: true
|
||||
extends: eslint:recommended
|
||||
overrides: []
|
||||
parserOptions:
|
||||
ecmaVersion: latest
|
||||
sourceType: module
|
||||
rules:
|
||||
indent:
|
||||
- error
|
||||
- tab
|
||||
linebreak-style:
|
||||
- error
|
||||
- unix
|
||||
quotes:
|
||||
- error
|
||||
- double
|
||||
semi:
|
||||
- error
|
||||
- always
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
tags
|
||||
node_modules/
|
3
aes.html
3
aes.html
@ -11,7 +11,6 @@
|
||||
<body>
|
||||
<script src="scripts/header-template.js"></script>
|
||||
<h1>AES</h1>
|
||||
<script src="scripts/interface.js"></script>
|
||||
<script src="scripts/aes.js"></script>
|
||||
<script type="module" src="scripts/aes.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
1921
package-lock.json
generated
Normal file
1921
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
35
package.json
Normal file
35
package.json
Normal file
@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "encryptme",
|
||||
"version": "0.4.1",
|
||||
"description": "Simple online cryptography app.",
|
||||
"main": "scripts/interface.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/dogeystamp/encryptme.git"
|
||||
},
|
||||
"keywords": [
|
||||
"website",
|
||||
"cryptography",
|
||||
"encryption",
|
||||
"aes",
|
||||
"webapp",
|
||||
"aes-256",
|
||||
"aes-gcm",
|
||||
"decryption",
|
||||
"encryption-decryption",
|
||||
"cryptography-tools",
|
||||
"cryptography-utilities"
|
||||
],
|
||||
"author": "dogeystamp",
|
||||
"license": "BSD-2-Clause",
|
||||
"bugs": {
|
||||
"url": "https://github.com/dogeystamp/encryptme/issues"
|
||||
},
|
||||
"homepage": "https://github.com/dogeystamp/encryptme#readme",
|
||||
"devDependencies": {
|
||||
"eslint": "^8.33.0"
|
||||
}
|
||||
}
|
@ -12,6 +12,9 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
|
||||
*/
|
||||
|
||||
import { TabList } from "./interface.js";
|
||||
import { bufToB64, b64ToBuf } from "./util.js";
|
||||
|
||||
let tabs = new TabList({});
|
||||
|
||||
let encForm = tabs.createForm({label: "Encryption"});
|
||||
@ -23,7 +26,7 @@ let encMsg = encForm.createTextArea({
|
||||
let encPass = encForm.createPasswordInput({
|
||||
label: "Password",
|
||||
placeholder: "Enter your password",
|
||||
enabledFunc: function() {return !encManualKey.value}
|
||||
enabledFunc: function() {return !encManualKey.value;}
|
||||
});
|
||||
let encPbkdf2Iters = encForm.createNumberInput({
|
||||
label: "PBKDF2 iterations",
|
||||
@ -31,14 +34,14 @@ let encPbkdf2Iters = encForm.createNumberInput({
|
||||
step: 1,
|
||||
value: 300000,
|
||||
advanced: true,
|
||||
enabledFunc: function() {return !encManualKey.value}
|
||||
enabledFunc: function() {return !encManualKey.value;}
|
||||
});
|
||||
let encSalt = encForm.createMediumTextBox({
|
||||
label: "PBKDF2 salt",
|
||||
dataType: "b64",
|
||||
advanced: true,
|
||||
enabled: false,
|
||||
enabledFunc: function() {return encManualSalt.value && !encManualKey.value}
|
||||
enabledFunc: function() {return encManualSalt.value && !encManualKey.value;}
|
||||
});
|
||||
let encManualSalt = encForm.createCheckBox({
|
||||
label: "Use fixed salt instead of random",
|
||||
@ -63,7 +66,7 @@ let encKey = encForm.createMediumTextBox({
|
||||
dataType: "b64",
|
||||
advanced: true,
|
||||
enabled: false,
|
||||
enabledFunc: function() {return encManualKey.value}
|
||||
enabledFunc: function() {return encManualKey.value;}
|
||||
});
|
||||
let encManualKey = encForm.createCheckBox({
|
||||
label: "Use fixed key instead of password",
|
||||
@ -73,25 +76,25 @@ let encIV = encForm.createMediumTextBox({
|
||||
label: "IV",
|
||||
dataType: "b64",
|
||||
advanced: true,
|
||||
enabledFunc: function() {return encManualIV.value},
|
||||
visibleFunc: function() {return ["AES-GCM", "AES-CBC"].includes(encMode.value)}
|
||||
enabledFunc: function() {return encManualIV.value;},
|
||||
visibleFunc: function() {return ["AES-GCM", "AES-CBC"].includes(encMode.value);}
|
||||
});
|
||||
let encManualIV = encForm.createCheckBox({
|
||||
label: "Use fixed IV instead of random",
|
||||
advanced: true,
|
||||
visibleFunc: function() {return ["AES-GCM", "AES-CBC"].includes(encMode.value)}
|
||||
visibleFunc: function() {return ["AES-GCM", "AES-CBC"].includes(encMode.value);}
|
||||
});
|
||||
let encCounter = encForm.createMediumTextBox({
|
||||
label: "Counter",
|
||||
dataType: "b64",
|
||||
advanced: true,
|
||||
enabledFunc: function() {return encManualCounter.value},
|
||||
visibleFunc: function() {return encMode.value === "AES-CTR"}
|
||||
enabledFunc: function() {return encManualCounter.value;},
|
||||
visibleFunc: function() {return encMode.value === "AES-CTR";}
|
||||
});
|
||||
let encManualCounter = encForm.createCheckBox({
|
||||
label: "Use fixed counter instead of random",
|
||||
advanced: true,
|
||||
visibleFunc: function() {return encMode.value === "AES-CTR"}
|
||||
visibleFunc: function() {return encMode.value === "AES-CTR";}
|
||||
});
|
||||
let encMode = encForm.createDropDown({
|
||||
label: "AES mode",
|
||||
@ -132,14 +135,14 @@ let decMsg = decForm.createTextArea({
|
||||
let decPass = decForm.createPasswordInput({
|
||||
label: "Password",
|
||||
placeholder: "Enter your password",
|
||||
enabledFunc: function() {return !decManualKey.value}
|
||||
enabledFunc: function() {return !decManualKey.value;}
|
||||
});
|
||||
let decKey = decForm.createMediumTextBox({
|
||||
label: "Key",
|
||||
dataType: "b64",
|
||||
advanced: true,
|
||||
enabled: false,
|
||||
enabledFunc: function() {return decManualKey.value}
|
||||
enabledFunc: function() {return decManualKey.value;}
|
||||
});
|
||||
let decManualKey = decForm.createCheckBox({
|
||||
label: "Use fixed key instead of password",
|
||||
@ -265,19 +268,18 @@ encButton.handle.addEventListener("click", async function() {
|
||||
|
||||
let ciphertext;
|
||||
switch (encMode.value) {
|
||||
case "AES-GCM":
|
||||
ciphertext = await aesGcmEnc(key, iv, msgEncoded);
|
||||
break;
|
||||
case "AES-CBC":
|
||||
ciphertext = await aesCbcEnc(key, iv, msgEncoded);
|
||||
break;
|
||||
case "AES-CTR":
|
||||
ciphertext = await aesCtrEnc(key, counter, msgEncoded);
|
||||
break;
|
||||
default:
|
||||
let e = Error(`Mode '${encMode.value}' is not implemented.`);
|
||||
encMode.handleError(e);
|
||||
return;
|
||||
case "AES-GCM":
|
||||
ciphertext = await aesGcmEnc(key, iv, msgEncoded);
|
||||
break;
|
||||
case "AES-CBC":
|
||||
ciphertext = await aesCbcEnc(key, iv, msgEncoded);
|
||||
break;
|
||||
case "AES-CTR":
|
||||
ciphertext = await aesCtrEnc(key, counter, msgEncoded);
|
||||
break;
|
||||
default:
|
||||
encMode.handleError(Error(`Mode '${encMode.value}' is not implemented.`));
|
||||
return;
|
||||
}
|
||||
|
||||
encOutRaw.value = ciphertext;
|
||||
@ -290,7 +292,7 @@ encButton.handle.addEventListener("click", async function() {
|
||||
"encMode": encMode.value,
|
||||
"encKeySize": encKeySize.value,
|
||||
"pbkdf2Iters": pbkdf2Iters,
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
async function aesGcmDec(key, iv, ciphertext) {
|
||||
@ -380,18 +382,18 @@ decButton.handle.addEventListener("click", async function() {
|
||||
|
||||
try {
|
||||
switch (encMode) {
|
||||
case "AES-GCM":
|
||||
plaintext = await aesGcmDec(key, iv, ciphertext);
|
||||
break;
|
||||
case "AES-CBC":
|
||||
plaintext = await aesCbcDec(key, iv, ciphertext);
|
||||
break;
|
||||
case "AES-CTR":
|
||||
plaintext = await aesCtrDec(key, counter, ciphertext);
|
||||
break;
|
||||
default:
|
||||
throw Error(`Mode '${encMode.value}' is not implemented.`);
|
||||
}
|
||||
case "AES-GCM":
|
||||
plaintext = await aesGcmDec(key, iv, ciphertext);
|
||||
break;
|
||||
case "AES-CBC":
|
||||
plaintext = await aesCbcDec(key, iv, ciphertext);
|
||||
break;
|
||||
case "AES-CTR":
|
||||
plaintext = await aesCtrDec(key, counter, ciphertext);
|
||||
break;
|
||||
default:
|
||||
throw Error(`Mode '${encMode.value}' is not implemented.`);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e.message !== "" && e.message !== undefined) {
|
||||
decMsg.handleError(e, "Error during decryption.");
|
||||
|
@ -12,6 +12,8 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
|
||||
*/
|
||||
|
||||
import { b64ToBuf, bufToB64 } from "./util.js";
|
||||
|
||||
class InterfaceElement {
|
||||
rootNodes = [];
|
||||
|
||||
@ -105,7 +107,7 @@ class Form extends InterfaceElement {
|
||||
}
|
||||
|
||||
let advancedToggle = this.createCheckBox({label: "Advanced settings"});
|
||||
advancedToggle.handle.addEventListener('change', function() {
|
||||
advancedToggle.handle.addEventListener("change", function() {
|
||||
this.advanced = advancedToggle.value;
|
||||
}.bind(this));
|
||||
|
||||
@ -179,7 +181,7 @@ class Form extends InterfaceElement {
|
||||
|
||||
createMediumTextBox(params) {
|
||||
params.tag = document.createElement("textarea");
|
||||
params.tag.classList.add("mediumbox")
|
||||
params.tag.classList.add("mediumbox");
|
||||
dataTypeSupports(params, ["plaintext", "b64", "json-b64"]);
|
||||
return this.createElem(params);
|
||||
}
|
||||
@ -281,30 +283,11 @@ class Form extends InterfaceElement {
|
||||
}
|
||||
}
|
||||
|
||||
function b64ToBuf (b64) {
|
||||
let ascii = atob(b64);
|
||||
let buf = new ArrayBuffer(ascii.length);
|
||||
let bytes = new Uint8Array(buf);
|
||||
for (var i = 0; i < ascii.length; i++) {
|
||||
bytes[i] = ascii.charCodeAt(i);
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
function bufToB64 (buf) {
|
||||
let bytes = new Uint8Array(buf);
|
||||
let ascii = '';
|
||||
for (var i = 0; i < bytes.byteLength; i++) {
|
||||
ascii += String.fromCharCode(bytes[i]);
|
||||
}
|
||||
return btoa(ascii);
|
||||
}
|
||||
|
||||
class FormElement extends InterfaceElement {
|
||||
constructor({tag, fragment, advanced=false, form,
|
||||
value, dataType, placeholder,
|
||||
labelTag, label="",
|
||||
enabled=true, enabledFunc,
|
||||
enabledFunc,
|
||||
visibleFunc
|
||||
}) {
|
||||
let oriVisibleFunc = visibleFunc;
|
||||
@ -315,7 +298,7 @@ class FormElement extends InterfaceElement {
|
||||
visibleFunc: function() {
|
||||
let res;
|
||||
if (oriVisibleFunc) {
|
||||
res = oriVisibleFunc()
|
||||
res = oriVisibleFunc();
|
||||
} else {
|
||||
res = true;
|
||||
}
|
||||
@ -355,61 +338,62 @@ class FormElement extends InterfaceElement {
|
||||
get value() {
|
||||
this.clearAlerts();
|
||||
switch (this.dataType) {
|
||||
case "number":
|
||||
if (this.handle.checkValidity() == false) {
|
||||
this.alertBox("alert-error", this.handle.validationMessage);
|
||||
return undefined;
|
||||
}
|
||||
return Number(this.handle.value);
|
||||
case "plaintext":
|
||||
return this.handle.value;
|
||||
case "b64":
|
||||
try {
|
||||
return b64ToBuf(this.handle.value);
|
||||
} catch (e) {
|
||||
this.alertBox("alert-error", "Invalid base64 value.");
|
||||
return;
|
||||
}
|
||||
case "json-b64":
|
||||
let jsonString;
|
||||
try {
|
||||
jsonString = atob(this.handle.value);
|
||||
} catch (e) {
|
||||
this.alertBox("alert-error", "Invalid base64 value.");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
return JSON.parse(jsonString);
|
||||
} catch (e) {
|
||||
this.alertBox("alert-error", "Invalid JSON encoding.");
|
||||
return;
|
||||
}
|
||||
case "bool":
|
||||
return this.handle.checked;
|
||||
case "category":
|
||||
return this.handle.value;
|
||||
case "none":
|
||||
case "number":
|
||||
if (this.handle.checkValidity() == false) {
|
||||
this.alertBox("alert-error", this.handle.validationMessage);
|
||||
return undefined;
|
||||
}
|
||||
return Number(this.handle.value);
|
||||
case "plaintext":
|
||||
return this.handle.value;
|
||||
case "b64":
|
||||
try {
|
||||
return b64ToBuf(this.handle.value);
|
||||
} catch (e) {
|
||||
this.handleError(Error("Invalid base64 value."));
|
||||
return undefined;
|
||||
}
|
||||
case "json-b64": {
|
||||
let jsonString;
|
||||
try {
|
||||
jsonString = atob(this.handle.value);
|
||||
} catch (e) {
|
||||
this.handleError(Error("Invalid base64 value."));
|
||||
return undefined;
|
||||
}
|
||||
try {
|
||||
return JSON.parse(jsonString);
|
||||
} catch (e) {
|
||||
this.handleError(Error("Invalid JSON encoding."));
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
case "bool":
|
||||
return this.handle.checked;
|
||||
case "category":
|
||||
return this.handle.value;
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
set value(x) {
|
||||
switch (this.dataType) {
|
||||
case "number":
|
||||
case "plaintext":
|
||||
this.handle.value = x;
|
||||
break;
|
||||
case "b64":
|
||||
this.handle.value = bufToB64(x);
|
||||
break;
|
||||
case "json-b64":
|
||||
this.handle.value = btoa(JSON.stringify(x));
|
||||
break;
|
||||
case "bool":
|
||||
this.handle.checked = x;
|
||||
break;
|
||||
case "category":
|
||||
this.handle.value = x;
|
||||
break;
|
||||
case "number":
|
||||
case "plaintext":
|
||||
this.handle.value = x;
|
||||
break;
|
||||
case "b64":
|
||||
this.handle.value = bufToB64(x);
|
||||
break;
|
||||
case "json-b64":
|
||||
this.handle.value = btoa(JSON.stringify(x));
|
||||
break;
|
||||
case "bool":
|
||||
this.handle.checked = x;
|
||||
break;
|
||||
case "category":
|
||||
this.handle.value = x;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -418,24 +402,24 @@ class FormElement extends InterfaceElement {
|
||||
// type is alert-error or alert-info
|
||||
|
||||
if (this.handle === undefined) {
|
||||
throw `can not add alert: still undefined`;
|
||||
throw "can not add alert: still undefined";
|
||||
}
|
||||
|
||||
if (this.hidden === true) {
|
||||
throw `can not add alert: hidden`;
|
||||
throw "can not add alert: hidden";
|
||||
}
|
||||
|
||||
if (title === undefined) {
|
||||
switch (type) {
|
||||
case "alert-info":
|
||||
title = "Info: ";
|
||||
break;
|
||||
case "alert-error":
|
||||
title = "Error: ";
|
||||
break;
|
||||
default:
|
||||
title = "";
|
||||
break;
|
||||
case "alert-info":
|
||||
title = "Info: ";
|
||||
break;
|
||||
case "alert-error":
|
||||
title = "Error: ";
|
||||
break;
|
||||
default:
|
||||
title = "";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -483,7 +467,7 @@ class Tab extends InterfaceElement {
|
||||
|
||||
class TabList extends InterfaceElement {
|
||||
constructor({tag, par=document.body}) {
|
||||
super({})
|
||||
super({});
|
||||
|
||||
this.par = par;
|
||||
|
||||
@ -497,7 +481,7 @@ class TabList extends InterfaceElement {
|
||||
par.appendChild(this.fragment);
|
||||
}
|
||||
|
||||
tabs = []
|
||||
tabs = [];
|
||||
|
||||
#activeForm;
|
||||
set activeForm(x) {
|
||||
@ -527,7 +511,7 @@ class TabList extends InterfaceElement {
|
||||
});
|
||||
this.tabs.push(tab);
|
||||
|
||||
tab.handle.addEventListener('click', function() {
|
||||
tab.handle.addEventListener("click", function() {
|
||||
this.activeForm = form;
|
||||
}.bind(this));
|
||||
|
||||
@ -539,3 +523,5 @@ class TabList extends InterfaceElement {
|
||||
return form;
|
||||
}
|
||||
}
|
||||
|
||||
export { TabList, FormElement, Form };
|
||||
|
34
scripts/util.js
Normal file
34
scripts/util.js
Normal file
@ -0,0 +1,34 @@
|
||||
/*
|
||||
|
||||
Copyright 2023 dogeystamp <dogeystamp@disroot.org>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
function b64ToBuf (b64) {
|
||||
let ascii = atob(b64);
|
||||
let buf = new ArrayBuffer(ascii.length);
|
||||
let bytes = new Uint8Array(buf);
|
||||
for (var i = 0; i < ascii.length; i++) {
|
||||
bytes[i] = ascii.charCodeAt(i);
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
function bufToB64 (buf) {
|
||||
let bytes = new Uint8Array(buf);
|
||||
let ascii = "";
|
||||
for (var i = 0; i < bytes.byteLength; i++) {
|
||||
ascii += String.fromCharCode(bytes[i]);
|
||||
}
|
||||
return btoa(ascii);
|
||||
}
|
||||
|
||||
export { b64ToBuf, bufToB64 };
|
Loading…
x
Reference in New Issue
Block a user