some updates

This commit is contained in:
jusax23 2021-10-22 19:51:31 +02:00
parent 2f4fcffbaf
commit 1f7a7c1c99
6 changed files with 145 additions and 12 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules

View file

@ -1,7 +1,8 @@
var node = false;
var crypto;
if (typeof require != "undefined") {
crypto = require('crypto');
if (typeof process != "undefined") {
crypto = await import('crypto');
if(typeof crypto.webcrypto != "undefined"){
crypto = crypto.webcrypto;
console.log("subtle");
@ -9,15 +10,16 @@ if (typeof require != "undefined") {
node = true;
console.log("node crypto");
}
}/*else{
}else{
crypto = window.crypto;
}*/
}
var asciiList = [];
for (var i = 0; i < 256; i++) {
asciiList[i]=String.fromCharCode(i);
}
const C = {
sha256: async (msgBuffer) => {
return node ? new Uint8Array(crypto.createHash("sha256").update(msgBuffer).digest()) :
@ -31,6 +33,7 @@ const C = {
return C.decode(await C.random(length));
},
encode : (data)=>{
console.log(data);
if(node){
return new Uint8Array(Buffer.from(data,"binary"));
}else{
@ -277,7 +280,7 @@ const C = {
d: await C.AES.encrypt(message,randKey),
k: await C.RSA.encrypt(randKey,publicKey)
};
return JSON.stringify(send);
return {send:JSON.stringify(send),key:randKey};
},
decrypt: async (data,privateKey)=>{
try {
@ -287,7 +290,7 @@ const C = {
if(randKey == null) throw "invalid async Key";
var out = await C.AES.decrypt(d,randKey);
if(out == null) throw "invalid sync Key";
return out;
return {data:out,key:randKey};
} catch (e) {
return null;
}
@ -320,4 +323,31 @@ const C = {
}
};
var a = C.sha256;
var b = C.random;
var c = C.randomKey;
var d = C.encode;
var e = C.decode;
var f = C.atob;
var g = C.btoa;
var h = C.pad;
var i = C.unpad;
var j = C.AES;
var k = C.RSA;
var l = C.communicate;
export {
a as sha256,
b as random,
c as randomKey,
d as encode,
e as decode,
f as atob,
g as btoa,
h as pad,
i as unpad,
j as AES,
k as RSA,
l as communicate
};
export default C;

View file

@ -1,7 +1,8 @@
var node = false;
var crypto;
if (typeof require != "undefined") {
crypto = require('crypto');
if (typeof process != "undefined") {
crypto = await import('crypto');
if(typeof crypto.webcrypto != "undefined"){
crypto = crypto.webcrypto;
console.log("subtle");
@ -9,9 +10,9 @@ if (typeof require != "undefined") {
node = true;
console.log("node crypto");
}
}/*else{
}else{
crypto = window.crypto;
}*/
}
var asciiList = [];
for (var i = 0; i < 256; i++) {
@ -31,6 +32,7 @@ const C = {
return C.decode(await C.random(length));
},
encode : (data)=>{
console.log(data);
if(node){
return new Uint8Array(Buffer.from(data,"binary"));
}else{
@ -277,7 +279,7 @@ const C = {
d: await C.AES.encrypt(message,randKey),
k: await C.RSA.encrypt(randKey,publicKey)
};
return JSON.stringify(send);
return {send:JSON.stringify(send),key:randKey};
},
decrypt: async (data,privateKey)=>{
try {
@ -287,7 +289,7 @@ const C = {
if(randKey == null) throw "invalid async Key";
var out = await C.AES.decrypt(d,randKey);
if(out == null) throw "invalid sync Key";
return out;
return {data:out,key:randKey};
} catch (e) {
return null;
}

48
main.js Normal file
View file

@ -0,0 +1,48 @@
import * as C from "./crypto.min.js";
(async ()=>{
const data = "abcdefg123456789";
const AES_key_1 = await C.randomKey(32);
const AES_key_2 = "some_random_text";
//some string operations
var hash = await C.sha256(data);
console.log("sha256: ", await C.decode(hash));
var btoa = C.btoa(data);
var atob = C.atob(data);
console.log("btoa: ", btoa, " atob: ", atob);
//AES
var encryptedAES = await C.AES.encrypt(data,AES_key_1);
var decryptedAES = await C.AES.decrypt(encryptedAES,AES_key_1);
console.log("AES | Encrypted: ", encryptedAES," Decrypted: ", decryptedAES);
//RSA
const encrKey = await C.RSA.generateEncryptionKey();
const signKey = await C.RSA.generateSigningKey();
//RSA encrypt
var encryptedRSA = await C.RSA.encrypt(data,encrKey.publicKey);
var decryptedRSA = await C.RSA.decrypt(encryptedRSA,encrKey.privateKey);
console.log("RSA | Encrypted: ", encryptedRSA," Decrypted: ", decryptedRSA);
//RSA signing
var certificate = await C.RSA.sign(data,encrKey.privateKey);
var isvaild = await C.RSA.verify(data,encryptedRSA,encrKey.publicKey);
console.log("RSA Signing | Cerificate: ", certificate," Is Valid?: ", isvaild ? "valid" : "invalid");
//communicate (RSA and AES for large Data)
///Only the Server has a certificate
var commEncrypted = await C.communicate.encrypt(data,encrKey.publicKey);
//randKey for AES: commEncrypted.key
var commDecrypted = await C.communicate.decrypt(commEncrypted.send,encrKey.privateKey);
//randKey for AES: commDecrypted.key
//If an error occures -> commDecrypted == null
console.log("Communicate | Encrypted: ",commEncrypted.send, " Decrypted: ",commDecrypted.data);
})();

29
package-lock.json generated Normal file
View file

@ -0,0 +1,29 @@
{
"name": "ilmk",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "ilmk",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"crypto": "^1.0.1"
}
},
"node_modules/crypto": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz",
"integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==",
"deprecated": "This package is no longer supported. It's now a built-in Node module. If you've depended on crypto, you should switch to the one that's built-in."
}
},
"dependencies": {
"crypto": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz",
"integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig=="
}
}
}

23
package.json Normal file
View file

@ -0,0 +1,23 @@
{
"name": "ilmk",
"version": "1.0.0",
"description": "I Lost My Keys.\r Crypto Module for WebJS and NodeJS.",
"main": "main.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://gitlab.com/jusax23/ilmk.git"
},
"author": "jusax23",
"license": "ISC",
"bugs": {
"url": "https://gitlab.com/jusax23/ilmk/issues"
},
"homepage": "https://gitlab.com/jusax23/ilmk#readme",
"dependencies": {
"crypto": "^1.0.1"
}
}