git ssb

0+

Rômulo Alves / ipfs-encrypted-add



Commit c3d2e73c834dc83851232b62bff2cd51b85e0e3f

Initial commit

romuloalves committed on 3/1/2019, 9:21:03 PM

Files changed

.gitignoreadded
index.jsadded
open.jsadded
package-lock.jsonadded
package.jsonadded
.gitignoreView
@@ -1,0 +1,2 @@
1 +node_modules/
2 +temp/
index.jsView
@@ -1,0 +1,42 @@
1 +const crypto = require('crypto');
2 +const { join } = require('path');
3 +
4 +const IPFS = require('ipfs');
5 +const uuidv4 = require('uuid/v4');
6 +
7 +function createMetadataFileContent(name, description) {
8 + return `{"name":"${name}","description":"${description}","revision":1,"createDate":${new Date().getTime()}}`;
9 +}
10 +
11 +const password = uuidv4();
12 +console.log(`\n> Generated password: ${password}`);
13 +
14 +const key = crypto.scryptSync(password, 'salt', 24);
15 +const iv = Buffer.alloc(16, 0);
16 +const cipher = crypto.createCipheriv('aes-192-gcm', key, iv);
17 +console.log('\n> Generated cipher.');
18 +
19 +const fileContent = createMetadataFileContent('TR0001', 'Example of description');
20 +console.log('\n> Generated file content.');
21 +
22 +let encryptedText = cipher.update(fileContent, 'utf8', 'hex');
23 +encryptedText += cipher.final('hex');
24 +
25 +const ipfs = new IPFS({
26 + repo: join(__dirname, 'temp', 'ipfs-repo')
27 +});
28 +
29 +ipfs.on('ready', function() {
30 + console.log('\n> Node ready.');
31 +
32 + ipfs.add({
33 + path: '/metadata.json',
34 + content: Buffer.from(encryptedText)
35 + }, {}, function(err, response) {
36 + if (err) {
37 + return console.error('\n> Error adding file.', err);
38 + }
39 +
40 + return console.log('\n\n> File added.', response);
41 + });
42 +});
open.jsView
@@ -1,0 +1,34 @@
1 +const crypto = require('crypto');
2 +const { join } = require('path');
3 +
4 +const IPFS = require('ipfs');
5 +
6 +const IPFS_KEY = 'QmbMaiVRiDHUAg52sEi9yRUTWqiHaKPmEdqsSrXLXhp9Sq';
7 +const PASSWORD = 'a2d7b8be-9277-4f50-a1ef-c4c5083b5d99';
8 +
9 +const ipfs = new IPFS({
10 + repo: join(__dirname, 'temp', 'ipfs-repo')
11 +});
12 +
13 +ipfs.on('ready', function() {
14 + console.log('\n> Node ready.');
15 +
16 + ipfs.cat(`/ipfs/${IPFS_KEY}`, function(err, file) {
17 + if (err) {
18 + return console.error('\n> Error reading file.', err);
19 + }
20 +
21 + const fileContent = file.toString('utf8');
22 + console.log(`\n> Content read from IPFS: ${fileContent}.`);
23 +
24 + const key = crypto.scryptSync(PASSWORD, 'salt', 24);
25 + const iv = Buffer.alloc(16, 0);
26 + const decipher = crypto.createDecipheriv('aes-192-gcm', key, iv);
27 + console.log('\n> Generated decipher.');
28 +
29 + let decrypted = decipher.update(fileContent, 'hex', 'utf8');
30 + console.log('\n> Content decrypted.');
31 +
32 + return console.log(`\n\n> File content: ${decrypted}`);
33 + })
34 +});
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 0 bytes
New file size: 248008 bytes
package.jsonView
@@ -1,0 +1,19 @@
1 +{
2 + "name": "ipfs-encrypted-add",
3 + "version": "1.0.0",
4 + "description": "Experiment about add and read encrypted files over IPFS",
5 + "main": "index.js",
6 + "scripts": {
7 + "add-file": "node index.js",
8 + "read-file": "node open.js"
9 + },
10 + "keywords": [
11 + "ipfs",
12 + "pentest"
13 + ],
14 + "author": "romuloalves",
15 + "dependencies": {
16 + "ipfs": "^0.34.4",
17 + "uuid": "^3.3.2"
18 + }
19 +}

Built with git-ssb-web