-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
88 lines (83 loc) · 2.15 KB
/
main.js
File metadata and controls
88 lines (83 loc) · 2.15 KB
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
const fs = require("fs");
const path = require('path');
const WebCrypto = require("node-webcrypto-ossl");
const axios = require("axios");
const FormData = require("form-data");
let form = new FormData();
const webcrypto = new WebCrypto();
const host = "https://cryptsend.io";
const upload = (filePath) => {
axios
.get(`${host}/mkdir`)
.then(res => {
let fullPath = `${host}${res.request.path}`;
encryptFile(filePath)
.then(data => {
form.append("f", data.buffer, path.basename(filePath));
axios
.post(fullPath, form, {
headers: form.getHeaders()
})
.then(res => {
console.log(`Here's the link to your encrypted file, only share it with people you trust!
${fullPath}#${data.hash}`);
})
.catch(error => {
console.error(error);
});
})
.catch(error => {
console.error(error);
});
})
.catch(error => {
console.error(error);
});
}
const encryptFile = async (filePath) => {
const key = await webcrypto.subtle.generateKey(
{
name: "AES-GCM",
length: 256
},
true,
["encrypt", "decrypt"]
);
const keydata = await webcrypto.subtle.exportKey("jwk", key);
return new Promise((resolve, reject) => {
try {
fs.readFile(filePath, (error, data) => {
if (error) {
throw error;
} else {
const iv = webcrypto.getRandomValues(new Uint8Array(12));
webcrypto.subtle
.encrypt(
{
name: "AES-GCM",
iv: iv
},
key,
data
)
.then(encryptedFile => {
let buffer = Buffer.concat([
Buffer.from(iv.buffer),
Buffer.from(encryptedFile)
]);
resolve({
buffer,
hash: keydata.k
});
})
.catch(error => {
console.error(error);
});
}
});
} catch (error) {
console.error(error);
}
});
};
exports.upload = upload;