encrypt/decrypt work on POST :dab:

master
Marcus 2020-02-27 00:17:07 -05:00
parent 9ad2adc510
commit 210a447158
8 changed files with 81 additions and 6 deletions

14
api/decrypt.js 100644
View File

@ -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;

14
api/encrypt.js 100644
View File

@ -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;

View File

@ -15,4 +15,4 @@ router.get('/:uid', async (req, res) => {
res.json(_return);
})
module.exports = router;
module.exports = router;

View File

@ -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;

View File

@ -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);

View File

@ -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"

View File

@ -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() {

View File

@ -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: