encrypt/decrypt work on POST :dab:
parent
9ad2adc510
commit
210a447158
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
4
index.js
4
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);
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Reference in New Issue