From 210a4471584c76bbe9b995421daed6ebf9cf5eee Mon Sep 17 00:00:00 2001 From: Marcus Date: Thu, 27 Feb 2020 00:17:07 -0500 Subject: [PATCH] encrypt/decrypt work on POST :dab: --- api/decrypt.js | 14 ++++++++++++++ api/encrypt.js | 14 ++++++++++++++ api/identity.js | 2 +- api/index.js | 2 ++ index.js | 4 ++-- package.json | 1 + src/identity.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- yarn.lock | 2 +- 8 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 api/decrypt.js create mode 100644 api/encrypt.js diff --git a/api/decrypt.js b/api/decrypt.js new file mode 100644 index 0000000..99617ae --- /dev/null +++ b/api/decrypt.js @@ -0,0 +1,14 @@ +const express = require('express'); +const { Router } = express; +const router = new Router() +const identity = require('../src/identity.js') + +router.post('/:uid', async (req, res) => { + const data = req.body.data || ""; + const uid = req.params.uid; + res.json({ + data: await identity.decrypt.with.public(uid, data) + }); +}); + +module.exports = router; \ No newline at end of file diff --git a/api/encrypt.js b/api/encrypt.js new file mode 100644 index 0000000..b71558d --- /dev/null +++ b/api/encrypt.js @@ -0,0 +1,14 @@ +const express = require('express'); +const { Router } = express; +const router = new Router() +const identity = require('../src/identity.js') + +router.post('/:uid', async (req, res) => { + const data = req.body.data || ""; + const uid = req.params.uid; + res.json({ + data: await identity.encrypt.with.private(uid, data) + }); +}); + +module.exports = router; \ No newline at end of file diff --git a/api/identity.js b/api/identity.js index adecc0a..17922e2 100644 --- a/api/identity.js +++ b/api/identity.js @@ -15,4 +15,4 @@ router.get('/:uid', async (req, res) => { res.json(_return); }) -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/api/index.js b/api/index.js index fcb914a..c49342a 100644 --- a/api/index.js +++ b/api/index.js @@ -11,5 +11,7 @@ api.get('/', (req, res) => { }) api.use('/identity', require('./identity.js')) +api.use('/encrypt', require('./encrypt.js')) +api.use('/decrypt', require('./decrypt.js')) module.exports = api; \ No newline at end of file diff --git a/index.js b/index.js index 30bbb52..c08040b 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,8 @@ const express = require('express'); const app = express(); const api = require('./api'); +const bodyParser = require('body-parser'); +app.use(bodyParser.json()); app.use('/api', api); app.listen(6565); - - diff --git a/package.json b/package.json index 32989aa..daa02bf 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "homepage": "https://github.com/marcus13345/socurity#readme", "dependencies": { + "body-parser": "^1.19.0", "express": "^4.17.1", "nedb": "^1.8.0", "node-rsa": "^1.0.7" diff --git a/src/identity.js b/src/identity.js index 74ecc84..3737047 100644 --- a/src/identity.js +++ b/src/identity.js @@ -1,12 +1,12 @@ const NodeRSA = require('node-rsa'); const nedb = require('nedb'); -const rsa = new NodeRSA(); const identities = new nedb({ filename: 'identities.nedb', autoload: true }); async function createIdentity(name) { + const rsa = new NodeRSA(); const pair = rsa.generateKeyPair() await new Promise(res => { @@ -43,11 +43,55 @@ async function getAllIdentities() { }); } +async function getPrivateKey(uid) { + return await new Promise((res, rej) => { + identities.findOne({ + _id: uid + }, (err, doc) => { + if(err || !doc) return rej(err); + res(doc.private); + }) + }); +} + +async function getPublicKey(uid) { + return await new Promise((res, rej) => { + identities.findOne({ + _id: uid + }, (err, doc) => { + if(err || !doc) return rej(err); + res(doc.public); + }) + }); +} + +async function encryptWithPrivate(uid, data) { + const cipher = new NodeRSA(); + cipher.importKey(await getPrivateKey(uid)); + return cipher.encryptPrivate(data, 'base64'); +} + +async function decryptWithPublic(uid, data) { + const cipher = new NodeRSA(); + cipher.importKey(await getPublicKey(uid)); + return cipher.decryptPublic(data, 'ascii'); +} + module.exports = { create: createIdentity, get: Object.assign(getIdentity, { all: getAllIdentities - }) + }), + encrypt: { + with: { + private: encryptWithPrivate + } + }, + decrypt: { + with: { + public: decryptWithPublic + } + } }; (async function() { diff --git a/yarn.lock b/yarn.lock index 0c1f895..320c75a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -29,7 +29,7 @@ binary-search-tree@0.2.5: dependencies: underscore "~1.4.4" -body-parser@1.19.0: +body-parser@1.19.0, body-parser@^1.19.0: version "1.19.0" resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a" dependencies: