Uspjesno kodirane i dokodirane poruke preko servera

This commit is contained in:
Hamza Iseric
2018-12-13 01:30:15 +01:00
parent 5dc6c76750
commit ff673b0a9f
3 changed files with 189 additions and 149 deletions

139
server.js
View File

@@ -9,18 +9,20 @@ var libsignal = require('libsignal');
const chatkit = new Chatkit.default({
instanceLocator: 'v1:us1:366480cc-91f1-4cbd-8c04-68dc96e8ccdf',
key:
'982eaea8-66f5-4bdb-9a2d-14de0fa8b92b:WSgfMJzA8cvZ/fwEqjupJhOpnwQ7qbAEOVze44y+XK0='
key: '982eaea8-66f5-4bdb-9a2d-14de0fa8b92b:WSgfMJzA8cvZ/fwEqjupJhOpnwQ7qbAEOVze44y+XK0='
})
const app = express()
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(bodyParser.json())
app.use(cors())
//--------
var KeyHelper = libsignal.keyhelper;
var sessionProm = null;
var sessionPromise = null;
function SignalProtocolStore() {
this.store = {};
}
@@ -104,7 +106,10 @@ SignalProtocolStore.prototype = {
loadPreKey: function (keyId) {
var res = this.get('25519KeypreKey' + keyId);
if (res !== undefined) {
res = { pubKey: res.pubKey, privKey: res.privKey };
res = {
pubKey: res.pubKey,
privKey: res.privKey
};
}
return Promise.resolve(res);
},
@@ -115,11 +120,14 @@ SignalProtocolStore.prototype = {
return Promise.resolve(this.remove('25519KeypreKey' + keyId));
},
/* Returns a signed keypair object or undefined */
/* Returns aa signed keypair object or undefined */
loadSignedPreKey: function (keyId) {
var res = this.get('25519KeysignedKey' + keyId);
if (res !== undefined) {
res = { pubKey: res.pubKey, privKey: res.privKey };
res = {
pubKey: res.pubKey,
privKey: res.privKey
};
}
return Promise.resolve(res);
},
@@ -148,6 +156,11 @@ SignalProtocolStore.prototype = {
return Promise.resolve();
}
};
var senderStore = new SignalProtocolStore();
var receiverStore = new SignalProtocolStore();
//---------------
function generateIdentity(store) {
return Promise.all([
@@ -199,6 +212,7 @@ function generatePreKeyBundle(store, preKeyId, signedPreKeyId) {
//---------
app.post("/cryptMessage", async (req, res) => {
console.log("UĐEEEEEEEEEEEEE");
//sessionPromise=req.body.data.sessionPromise;
message = req.body.data.message;
//senderStore = req.body.data.senderStore;
@@ -206,17 +220,17 @@ app.post("/cryptMessage", async (req, res) => {
//----------------
var senderAdress = req.body.data.senderAdress;//"xxxxxxxxx"
var receiverAdress = req.body.data.receiverAdress;//""yyyyyyyyyyyyy""
var receiverPreKeyId = req.body.data.receiverPreKeyId;// 1337;
var receiverSignedKeyId = req.body.data.receiverSignedKeyId;// 1;
var senderAdress = req.body.data.senderAdress; //"xxxxxxxxx"
var receiverAdress = req.body.data.receiverAdress; //""yyyyyyyyyyyyy""
var receiverPreKeyId = req.body.data.receiverPreKeyId; // 1337;
var receiverSignedKeyId = req.body.data.receiverSignedKeyId; // 1;
var SENDER_ADDRESS = new libsignal.ProtocolAddress(senderAdress, 1);
var RECEIVER_ADDRESS = new libsignal.ProtocolAddress(receiverAdress, 1);
var senderStore = new SignalProtocolStore();
//var senderStore = new SignalProtocolStore();
var receiverStore = new SignalProtocolStore();
//var receiverStore = new SignalProtocolStore();
var receiverSignedKeyId = 1;
@@ -229,7 +243,8 @@ app.post("/cryptMessage", async (req, res) => {
return generatePreKeyBundle(receiverStore, receiverPreKeyId, receiverSignedKeyId);
}).then(function (preKeyBundle) {
var builder = new libsignal.SessionBuilder(senderStore, RECEIVER_ADDRESS);
sessionPromise = builder.initOutgoing(preKeyBundle).then(function () {
sessionPromise = builder.initOutgoing(preKeyBundle);
sessionPromise.then(function () {
var originalMessage = Buffer.from(message, 'utf8');
var senderSessionCipher = new libsignal.SessionCipher(senderStore, RECEIVER_ADDRESS);
@@ -237,79 +252,48 @@ app.post("/cryptMessage", async (req, res) => {
//var receiverSessionCipher = new libsignal.SessionCipher(receiverStore, SENDER_ADDRESS);
senderSessionCipher.encrypt(originalMessage).then(function (ciphertext) {
console.log("KRI, RECSTORE:", receiverStore);
console.log("KRI, RECadress:", SENDER_ADDRESS);
res.json({ data: { receiverStore: receiverStore, senderAdress: senderAdress, message: ciphertext.body } })
res.json({
data: {
receiverStore: receiverStore,
SENDER_ADDRESS: SENDER_ADDRESS,
message: ciphertext.body
}
})
});
});
});
});
app.post("/decryptMessage2", async (req, res) => {
//sessionPromise=req.body.data.sessionPromise;
message = req.body.data.message;
//senderStore = req.body.data.senderStore;
//RECEIVER_ADDRESS = new libsignal.ProtocolAddress(req.body.data.receiverAdress.id, req.body.data.receiverAdress.deviceId);
//----------------
var senderAdress = req.body.data.senderAdress;//"xxxxxxxxx"
var receiverAdress = req.body.data.receiverAdress;//""yyyyyyyyyyyyy""
var receiverPreKeyId = req.body.data.receiverPreKeyId;// 1337;
var receiverSignedKeyId = req.body.data.receiverSignedKeyId;// 1;
var SENDER_ADDRESS = new libsignal.ProtocolAddress(senderAdress, 1);
var RECEIVER_ADDRESS = new libsignal.ProtocolAddress(receiverAdress, 1);
var senderStore = new SignalProtocolStore();
var receiverStore = new SignalProtocolStore();
var receiverSignedKeyId = 1;
var Curve = libsignal.Curve;
await Promise.all([
generateIdentity(senderStore),
generateIdentity(receiverStore),
]).then(function () {
return generatePreKeyBundle(receiverStore, receiverPreKeyId, receiverSignedKeyId);
}).then(function (preKeyBundle) {
var builder = new libsignal.SessionBuilder(senderStore, RECEIVER_ADDRESS);
sessionPromise = builder.initOutgoing(preKeyBundle).then(function () {
const buf5 = Buffer.from(message.text.split(','));
console.log(message)
var receiverSessionCipher = new libsignal.SessionCipher(receiverStore, SENDER_ADDRESS);//ubaceno
receiverSessionCipher.decryptPreKeyWhisperMessage(buf5, 'binary').then(function (ciphertext) {
console.log("DEKODIRANO:", ciphertext)
res.json({ data: { message: ciphertext } })
});
});
});
});
app.post('/deCryptMessage', (req, res) => {
var receiverStore = req.body.data.receiverStore;//"xxxxxxxxx"
var senderAdress = req.body.data.senderAdress;//""yyyyyyyyyyyyy""
var message = req.body.data.message;//""yyyyyyyyyyyyy""
const buf5 = Buffer.from(message.text.split(','));
message = req.body.data.message;
const buf5 = Buffer.from(message.split(','));
//receiverStore = req.body.data.receiverStore;
//SENDER_ADDRESS = req.body.data.SENDER_ADDRESS;
senderAdress = req.body.data.senderAdress;
var SENDER_ADDRESS = new libsignal.ProtocolAddress(senderAdress, 1);
console.log("DEKRI, RECSTORE:", receiverStore.store.identityKey.pubKey);
console.log("DEKRI, RECSTORE:", SENDER_ADDRESS);
//var receiverSessionCipher = new libsignal.SessionCipher(receiverStore, SENDER_ADDRESS);
var receiverSessionCipher = new libsignal.SessionCipher(receiverStore, SENDER_ADDRESS);//ubaceno
//odavde
var receiverSessionCipher = new libsignal.SessionCipher(receiverStore, SENDER_ADDRESS); //ubaceno
receiverSessionCipher.decryptPreKeyWhisperMessage(buf5, 'binary').then(function (ciphertext) {
console.log("DEKODIRANO:", ciphertext)
res.json({ data: { message: ciphertext } })
poruka = ciphertext.toString();
res.json({
data: {
message: poruka
}
})
});
@@ -320,8 +304,13 @@ app.post('/deCryptMessage', (req, res) => {
app.post('/users', (req, res) => {
const { username } = req.body
const user = { name: username, id: username }
const {
username
} = req.body
const user = {
name: username,
id: username
}
chatkit
.createUser(user)
.then(() => {