Added npm file and source

Signed-off-by: ENGO150 <business.engo150@gmail.com>
This commit is contained in:
SebestikCZ 2022-03-20 17:12:08 +01:00
parent b911654666
commit a4abf465cc
4 changed files with 199 additions and 0 deletions

4
index.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
decrypter: require("./src/decrypter"),
encrypter: require("./src/encrypter")
}

24
package.json Normal file
View File

@ -0,0 +1,24 @@
{
"name": "why2-encryption-system.js",
"version": "1.0.0",
"description": "Node.js wrapper for encryption system WHY2 made by ENGO150 in C lang.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ENGO150/WHY2-Encryption-System.git#nodejs-wrapper"
},
"keywords": [
"why2",
"decrypter",
"encrypter"
],
"author": "Šebestíček & ENGO150",
"license": "ISC",
"bugs": {
"url": "https://github.com/ENGO150/WHY2-Encryption-System/issues"
},
"homepage": "https://github.com/ENGO150/WHY2-Encryption-System/tree/nodejs-wrapper#readme"
}

83
src/decrypter.js Normal file
View File

@ -0,0 +1,83 @@
module.exports = (text, key) => {
const KEY_LENGTH = 50;
const ENCRYPTION_SEPARATOR = ".";
//CHECK FOR INVALID key
if (key.length < KEY_LENGTH) {
return { exitCode: -2 };
}
//VARIABLES
var returningText;
var numberBuffer;
var textBuffer = "";
var textBuffer1 = "";
numberBuffer = 1;
//GET LENGTH OF returningText AND textKeyChain
for (var i = 0; i < text.length; i++) {
if (text[i] == ENCRYPTION_SEPARATOR) numberBuffer++;
}
//SET LENGTH
returningText = [];
returningText.length = numberBuffer;
var textKeyChain = [];
var encryptedTextKeyChain = [];
textKeyChain.length = encryptedTextKeyChain.length = numberBuffer;
//LOAD textKeyChain
for (var i = 0; i < textKeyChain.length; i++) {
numberBuffer = i;
//CHECK, IF numberBuffer ISN'T GREATER THAN KEY_LENGTH AND CUT UNUSED LENGTH
while (numberBuffer >= key.length) {
numberBuffer -= key.length;
}
if (typeof key === 'string') {
key = key.split("");
key = key.map(element => element.charCodeAt(0));
}
//FILL textKeyChain
if ((numberBuffer + 1) % 3 == 0)
{
textKeyChain[i] = key[numberBuffer] * key[numberBuffer + 1];
} else if ((numberBuffer + 1) % 2 == 0)
{
textKeyChain[i] = key[numberBuffer] - key[numberBuffer + 1];
} else
{
textKeyChain[i] = key[numberBuffer] + key[numberBuffer + 1];
}
}
//LOAD encryptedTextKeyChain
for (var i = 0; i < encryptedTextKeyChain.length; i++) {
numberBuffer = 0;
textBuffer1 = text.split(ENCRYPTION_SEPARATOR);
try {
textBuffer = textBuffer1[i];
encryptedTextKeyChain[i] = parseInt(textBuffer);
} catch (e) {
return { exitCode: -1, error: e };
}
textBuffer = "";
}
//DECRYPT TEXT
for (var i = 0; i < textKeyChain.length; i++) {
textKeyChain[i] -= encryptedTextKeyChain[i];
}
//LOAD returningText
for (var i = 0; i < textKeyChain.length; i++) {
returningText[i] = String.fromCharCode(textKeyChain[i]);
}
returningText = returningText.join("");
return { exitCode: 0, value: returningText };
},

88
src/encrypter.js Normal file
View File

@ -0,0 +1,88 @@
module.exports = async (text, key = undefined) => {
const KEY_LENGTH = 50
const ENCRYPTION_SEPARATOR_STRING = '.';
const GENERATE_RANDOM_KEY = async (numberBuffer) => {
key = [];
//LOAD KEY
for (var i = 0; i < KEY_LENGTH; i++) {
//SET numberBuffer TO RANDOM NUMBER BETWEEN 0 AND 52
numberBuffer = await crypto.randomInt(0, 52 + 1);
//GET CHAR FROM numberBuffer
if (numberBuffer > 26) {
numberBuffer += 70;
} else {
numberBuffer += 64;
}
key[i] = numberBuffer;
}
}
//VARIABLES
var keyRand = KEY_LENGTH;
var returningText = new String();
var textBuffer;
var textKeyChain = [];
textKeyChain.length = text.length;
var numberBuffer;
if (key < KEY_LENGTH) {
return { exitCode: -2 };
}
if (!key) {
await GENERATE_RANDOM_KEY(numberBuffer);
}
//LOAD textKeyChain
for (var i = 0; i < textKeyChain.length; i++) {
numberBuffer = i;
//CHECK, IF numberBuffer ISN'T GREATER THAN KEY_LENGTH AND CUT UNUSED LENGTH
while (numberBuffer >= key.length)
{
numberBuffer -= key.length;
}
if (typeof key === 'string') {
key = key.split("");
key = key.map(element => element.charCodeAt(0));
}
//FILL textKeyChain
if ((numberBuffer + 1) % 3 == 0)
{
textKeyChain[i] = key[numberBuffer] * key[numberBuffer + 1];
} else if ((numberBuffer + 1) % 2 == 0)
{
textKeyChain[i] = key[numberBuffer] - key[numberBuffer + 1];
} else
{
textKeyChain[i] = key[numberBuffer] + key[numberBuffer + 1];
}
}
//ACTUALLY ENCRYPT TEXT
for (var i = 0; i < text.length; i++) {
textKeyChain[i] -= text[i].charCodeAt(0);
}
numberBuffer = 0;
//LOAD returningText
for (var i = 0; i < textKeyChain.length; i++) {
textBuffer = Math.floor(Math.log10(Math.abs(textKeyChain[i])));
textBuffer = textKeyChain[i].toString();
returningText += textBuffer;
if (i != textKeyChain.length - 1)
{
returningText += ENCRYPTION_SEPARATOR_STRING;
}
textBuffer = undefined;
}
key = key.map(element => String.fromCharCode(element)).join("");
return { exitCode: 0, value: returningText, key: key };
}