-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
124 lines (102 loc) · 4.06 KB
/
app.js
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
const ipfsClient = require("ipfs-http-client");
const express = require("express");
const fs = require('fs');
const fileUpload = require("express-fileupload");
const bodyParser = require("body-parser");
const crypto = require("crypto")
const ipfs = new ipfsClient({host:'localhost' , port:'5001',protocol:'http'});
const app = express();
// app.engine('html', require('ejs').renderFile);
let secretPrivateKey="";
let Encryptedcipher="";
let IV = "";
//middleware
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended:true}));
app.use(fileUpload());
//routes
app.get('/',(req,res) => {
res.render('home');
});
app.post('/upload',(req,res) => {
const file = req.files.file;
const fileName = req.body.fileName;
const filePath = 'files/'+fileName;
file.mv(filePath,async (err) => {
if(err){
console.log('Error:failed to download the file');
return res.status(500).send(err);
}
// The `generateKeyPairSync` method accepts two arguments:
// 1. The type of keys we want, which in this case is "rsa"
// 2. An object with the properties of the key
const { publicKey, privateKey } = crypto.generateKeyPairSync("rsa", {
modulusLength: 2048,
})
secretPrivateKey = privateKey;
const encryptedFileData = await addFile(publicKey,fileName,filePath);
fs.unlink(filePath , (err) => {
if(err) console.log(err);
});
Encryptedcipher = encryptedFileData.cipher;
const ipfsURL = 'https://ipfs.io/ipfs/'+encryptedFileData.fileHash;
console.log('IPFS asset url is: ',ipfsURL)
let data = {fileName:fileName,fileHash:encryptedFileData.fileHash};
res.render('upload',data);
})
});
app.post('/getFile',(req,res) => {
const encryptedDataHash = req.body.hash;
const encryptedDataHashBuffer = Buffer.from(Encryptedcipher,'base64');
const decryptedcipher = crypto.privateDecrypt(
{
key: secretPrivateKey,
// In order to decrypt the data, we need to specify the
// same hashing function and padding scheme that we used to
// encrypt the data in the previous step -- Read the docs once!
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},
encryptedDataHashBuffer
)
const decrypt = ((encrypted) => {
let decipher = crypto.createDecipheriv('aes-256-cbc',decryptedcipher,IV);
let decrypted = decipher.update(encrypted, 'base64', 'utf8');
return (decrypted + decipher.final('utf8')).toString();
});
// console.log('ENC_KEY during encryption: ',decryptedcipher)
decryptedFile = decrypt(encryptedDataHash)
console.log(typeof decryptedFile)
console.log("decrypted data: ", decryptedFile.toString())
res.render('viewFile',{data:decryptedFile.toString()})
})
//functions
const addFile = async (publicKey,fileName,filePath) =>{
const file = fs.readFileSync(filePath);
const ENC_KEY = Buffer.from(crypto.randomBytes(32)); // set random encryption key
let iv = new Buffer.from(crypto.randomBytes(16))
IV = iv.toString('hex').slice(0, 16);
const encrypt = ((val) => {
let cipher = crypto.createCipheriv('aes-256-cbc', ENC_KEY, IV);
let encrypted = cipher.update(val, 'utf8', 'base64');
encrypted += cipher.final('base64');
return encrypted;
});
// console.log('ENC_KEY during encryption: ',ENC_KEY)
const encryptedData1 = encrypt(file);
const encryptedData2 = crypto.publicEncrypt(
{
key: publicKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},
Buffer.from(ENC_KEY)
)
const fileAdded = await ipfs.add({path:fileName,content:encryptedData1.toString("base64")});
console.log('Asset file details - IPFS: ',fileAdded)
const fileHash = fileAdded.cid;
return {fileHash:fileHash,cipher:encryptedData2.toString("base64")}
}
app.listen(3000, () => {
console.log('Server is listening on port 3000!')
})