Skip to content

Commit 9277b45

Browse files
committed
Encryption for Insert and Truncate complete
1 parent 81cb189 commit 9277b45

File tree

6 files changed

+103
-21
lines changed

6 files changed

+103
-21
lines changed

REPL.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ function run(command) {
2222
console.table(util.inspect(result[0], false, null, true));
2323
} else {
2424
console.log(result[0].info);
25-
printTable(result[0].data);
25+
if (result[0].data.length !== 0) {
26+
printTable(result[0].data);
27+
} else {
28+
console.log("Table Empty");
29+
}
2630
}
2731
}
2832
console.log("\n");

classes.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ class BodytoDetailsResolver {
133133
details.values = this.body[0];
134134
details.table = this.body[2];
135135
details.columns = this.body[4];
136+
if (this.body[this.body.length - 2] === "p") {
137+
details.password = this.body[this.body.length - 1];
138+
} else {
139+
details.password = false;
140+
}
136141
} else if (this.action === "delete") {
137142
if (this.body[0] === "database") {
138143
details.object = "database";
@@ -145,6 +150,11 @@ class BodytoDetailsResolver {
145150
}
146151
} else if (this.action === "truncate") {
147152
details.table = this.body[0];
153+
if (this.body[this.body.length - 2] === "p") {
154+
details.password = this.body[this.body.length - 1];
155+
} else {
156+
details.password = false;
157+
}
148158
} else if (this.action === "use") {
149159
details.database = this.body[0];
150160
} else if (this.action === "default") {

parser/actions/insert/insert.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class hashPassword {
88
this.index = index;
99
}
1010
}
11+
const crypto = require("crypto");
12+
13+
const ALGORITHM = "aes-256-ctr";
14+
const SALT_ROUNDS = 10;
1115

1216
function valueChecker(columns, values) {
1317
/*console.log('in valueChecker');
@@ -58,14 +62,23 @@ function actionInsert(code, preRunData) {
5862
`${preRunData[1]}`,
5963
`${code.details.table}.json`
6064
);
61-
let table = require(storagePath);
65+
let Decrypter = new classes.Decrypter(preRunData[1], code.details.table);
66+
//console.log(Decrypter.check());
67+
console.log(code);
68+
let table = null;
69+
if (!Decrypter.check()) {
70+
table = require(storagePath);
71+
} else {
72+
table = JSON.parse(Decrypter.decrypt(code.details.password));
73+
}
6274
if (code.details.columns === "*") {
6375
code.details.columns = "all";
6476
}
6577
let fullColumnData = readColumns(
6678
new classes.ActionDetails("select", {
6779
object: "columns",
6880
table: code.details.table,
81+
password: code.details.password,
6982
}),
7083
preRunData
7184
).data;
@@ -105,7 +118,22 @@ function actionInsert(code, preRunData) {
105118
if (USING_DATABASE === "") {
106119
return "KeyError: Not using any database currently";
107120
}
108-
fs.writeFileSync(storagePath, JSON.stringify(table, null, 4));
121+
if (!Decrypter.check()) {
122+
fs.writeFileSync(storagePath, JSON.stringify(table, null, 4));
123+
} else {
124+
const passwordHash = bcrypt.hashSync(
125+
code.details.password,
126+
SALT_ROUNDS
127+
);
128+
const cipher = crypto.createCipher(ALGORITHM, code.details.password);
129+
let encryptedData = cipher.update(JSON.stringify(table), "utf8", "hex");
130+
encryptedData += cipher.final("hex");
131+
const encryptedObject = {
132+
passwordHash,
133+
data: encryptedData,
134+
};
135+
fs.writeFileSync(storagePath, JSON.stringify(encryptedObject), "utf-8");
136+
}
109137
if (preRunData[0])
110138
return new classes.Success(
111139
`Successfully inserted the values into the table ${table.name}`

parser/actions/select/table.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function selectTable(code, preRunData) {
1515
code.details.columns = "all";
1616
}
1717
let Decrypter = new classes.Decrypter(preRunData[1], code.details.name);
18-
console.log(Decrypter.check());
19-
console.log(code);
18+
//console.log(Decrypter.check());
19+
//console.log(code);
2020
let table = null;
2121
if (!Decrypter.check()) {
2222
table = require(storagePath);

parser/actions/truncate/truncate.js

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,59 @@
1-
const path = require('path');
2-
const fs = require('fs');
3-
const classes = require(path.join(__dirname, '../../../', 'classes.js'));
1+
const path = require("path");
2+
const fs = require("fs");
3+
const classes = require(path.join(__dirname, "../../../", "classes.js"));
4+
const crypto = require("crypto");
5+
const bcrypt = require("bcrypt");
46

5-
function actionTruncate(code, preRunData){
6-
let storagePath = path.join(__dirname, '../../../', 'storage', `${preRunData[1]}`, `${code.details.table}.json`);
7-
try{
8-
let table = require(storagePath);
9-
table.values = [];
10-
let USING_DATABASE = preRunData[1];
11-
if(USING_DATABASE===''){return "KeyError: Not using any database currently"};
12-
fs.writeFileSync(storagePath, JSON.stringify(table, null, 4));
13-
if(preRunData[0]) return new classes.Success(`Successfully truncated the table ${table.name}`);
14-
} catch(e){
15-
return new classes.Error("StorageError", `Cannot find table ${code.details.table}`);
7+
const ALGORITHM = "aes-256-ctr";
8+
const SALT_ROUNDS = 10;
9+
10+
function actionTruncate(code, preRunData) {
11+
let storagePath = path.join(
12+
__dirname,
13+
"../../../",
14+
"storage",
15+
`${preRunData[1]}`,
16+
`${code.details.table}.json`
17+
);
18+
try {
19+
let Decrypter = new classes.Decrypter(preRunData[1], code.details.table);
20+
//console.log(Decrypter.check());
21+
console.log(code);
22+
let table = null;
23+
if (!Decrypter.check()) {
24+
table = require(storagePath);
25+
} else {
26+
table = JSON.parse(Decrypter.decrypt(code.details.password));
27+
}
28+
table.values = [];
29+
let USING_DATABASE = preRunData[1];
30+
if (USING_DATABASE === "") {
31+
return "KeyError: Not using any database currently";
32+
}
33+
if (!Decrypter.check()) {
34+
fs.writeFileSync(storagePath, JSON.stringify(table, null, 4));
35+
} else {
36+
const passwordHash = bcrypt.hashSync(code.details.password, SALT_ROUNDS);
37+
const cipher = crypto.createCipher(ALGORITHM, code.details.password);
38+
let encryptedData = cipher.update(JSON.stringify(table), "utf8", "hex");
39+
encryptedData += cipher.final("hex");
40+
const encryptedObject = {
41+
passwordHash,
42+
data: encryptedData,
43+
};
44+
fs.writeFileSync(storagePath, JSON.stringify(encryptedObject), "utf-8");
1645
}
46+
if (preRunData[0])
47+
return new classes.Success(
48+
`Successfully truncated the table ${table.name}`
49+
);
50+
} catch (e) {
51+
console.log(e);
52+
return new classes.Error(
53+
"StorageError",
54+
`Cannot find table ${code.details.table}`
55+
);
56+
}
1757
}
1858

19-
module.exports = actionTruncate;
59+
module.exports = actionTruncate;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"passwordHash":"$2b$10$dtd4wRRGdQfUFgaP9LZCuOpp9MqfWpwdM.4.8YoPzqeoaYXsU0YNi","data":"7a5a1afadb051a68d942afaee6226bd05b0020d1d17dc180cba4d600996e948f0dec8d25abd0a540f867f7a4252cfd5702c8a7a929b368dd089cf273de8426fa1c9f9b5f29292258222ad73fb22604fc48ce05396b9c17648b44ec539fabc0c4803d853c87f3b730b11e0198d4d1492b02ca6e9134afa06d52725cf16e810ab340d85085072dfc279005df2103ca6c5275a9503fe650253f5be2d63bec8c379cf87cdb142d77cd9fc44967bcf08205743ac10d4d073e7d8eb642fd08e620fc2b2e3265da027273e6629d7b581d3413b022bae350501b07a637bead9a121479205564243ac108b0308dddd993dbec42721466b0aa853ff6fd1b572c5d4e520ff8246195be8a5307413481c704c96c4a60e45bb99dcd0feb67a863cd1755688ce8a7c0889198b610f64e81d8d6c221bf9b7b4b1a800246381ed1b11882e7a17c840d0c48e9b83a469bcba81f997d88659c67ec445a"}
1+
{"passwordHash":"$2b$10$kNBnyrvL25H7goSOgPq.Tu3Ui7J4nJ7ORH.5lLhEoPq3T6GgjYkOe","data":"7a7254bb940e593f9e14f0fdb03f60c41d4b6294803081ff86ea8741cc599ac00afb8d25a9e5da0dba22a5ff5f7eb41a439afee025ae689a04c5be68d28b26aa06cd9a4236213e466b3282669a7551be02d64a4769861537843ca51dd8a99db98e7192308ea4e863b1572eb18c8f1e252aba3cd076b3e706003312f146ea58f216991ec44b7ca567ad0fce3e5298230024800e0db907361959909f78faeb78c58503b0596c3bcf9f8a10208297f7720d6dbc014f430011cfe30ea95ea77db97e711a7f9c433e20a33d"}

0 commit comments

Comments
 (0)