This document introduces a common Message Module for decentralized instant messaging.
Copyright © 2018-2019 Albert Moky
/* example */
{
sender : "moki@4WDfe3zZ4T7opFSi3iDAKiuTnUHjxmXekk",
receiver : "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj",
time : 1545405083
}
/* example */
{
type : 0x01, // message type
sn : 412968873, // serial number (message ID in conversation)
text : "Hey guy!"
}
typedef NS_ENUM(UInt8, DKDContentType) {
DKDContentType_Unknown = 0x00,
DKDContentType_Text = 0x01, // 0000 0001
DKDContentType_File = 0x10, // 0001 0000
DKDContentType_Image = 0x12, // 0001 0010
DKDContentType_Audio = 0x14, // 0001 0100
DKDContentType_Video = 0x16, // 0001 0110
DKDContentType_Page = 0x20, // 0010 0000
// quote a message before and reply it with text
DKDContentType_Quote = 0x37, // 0011 0111
DKDContentType_Money = 0x40, // 0100 0000
// DKDContentType_LuckyMoney = 0x41, // 0100 0001
// DKDContentType_Transfer = 0x42, // 0100 0010
DKDContentType_Command = 0x88, // 1000 1000
DKDContentType_History = 0x89, // 1000 1001 (Entity history command)
// top-secret message forward by proxy (Service Provider)
DKDContentType_Forward = 0xFF // 1111 1111
};
When the user want to send out a message, the client needs TWO steps before sending it:
- Encrypt the Instant Message to Secure Message;
- Sign the Secure Message to Reliable Message.
Accordingly, when the client received a message, it needs TWO steps to extract the content:
- Verify the Reliable Message to Secure Message;
- Decrypt the Secure Message to Instant Message.
Message Transforming
~~~~~~~~~~~~~~~~~~~~
Instant Message <--> Secure Message <--> Reliable Message
+-------------+ +------------+ +--------------+
| sender | | sender | | sender |
| receiver | | receiver | | receiver |
| time | | time | | time |
| | | | | |
| content | | data | | data |
+-------------+ | key/keys | | key/keys |
+------------+ | signature |
+--------------+
Algorithm:
data = password.encrypt(content)
key = receiver.public_key.encrypt(password)
signature = sender.private_key.sign(data)
/* example */
{
//-------- head (envelope) --------
sender : "moki@4WDfe3zZ4T7opFSi3iDAKiuTnUHjxmXekk",
receiver : "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj",
time : 1545405083,
//-------- body (content) ---------
content : {
type : 0x01, // message type
sn : 412968873, // serial number (ID)
text : "Hey guy!"
}
}
content -> JsON string: {"sn":412968873,"text":"Hey guy!","type":1}
/**
* Algorithm:
* string = json(content);
* PW = random();
* data = encrpyt(string, PW); // Symmetric
* key = encrypt(PW, receiver.PK); // Asymmetric
*/
{
//-------- head (envelope) --------
sender : "moki@4WDfe3zZ4T7opFSi3iDAKiuTnUHjxmXekk",
receiver : "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj",
time : 1545405083,
//-------- body (content) ---------
data : "9cjCKG99ULCCxbL2mkc/MgF1saeRqJaCc+S12+HCqmsuF7TWK61EwTQWZSKskUeF",
key : "WH/wAcu+HfpaLq+vRblNnYufkyjTm4FgYyzW3wBDeRtXs1TeDmRxKVu7nQI/sdIALGLXrY+O5mlRfhU8f8TuIBilZUlX/eIUpL4uSDYKVLaRG9pOcrCHKevjUpId9x/8KBEiMIL5LB0Vo7sKrvrqosCnIgNfHbXMKvMzwcqZEU8="
}
/**
* Algorithm:
* signature = sign(data, sender.SK);
*/
{
//-------- head (envelope) --------
sender : "moki@4WDfe3zZ4T7opFSi3iDAKiuTnUHjxmXekk",
receiver : "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj",
time : 1545405083,
//-------- body (content) ---------
data : "9cjCKG99ULCCxbL2mkc/MgF1saeRqJaCc+S12+HCqmsuF7TWK61EwTQWZSKskUeF",
key : "WH/wAcu+HfpaLq+vRblNnYufkyjTm4FgYyzW3wBDeRtXs1TeDmRxKVu7nQI/sdIALGLXrY+O5mlRfhU8f8TuIBilZUlX/eIUpL4uSDYKVLaRG9pOcrCHKevjUpId9x/8KBEiMIL5LB0Vo7sKrvrqosCnIgNfHbXMKvMzwcqZEU8=",
signature : "Yo+hchWsQlWHtc8iMGS7jpn/i9pOLNq0E3dTNsx80QdBboTLeKoJYAg/lI+kZL+g7oWJYpD4qKemOwzI+9pxdMuZmPycG+0/VM3HVSMcguEOqOH9SElp/fYVnm4aSjAJk2vBpARzMT0aRNp/jTFLawmMDuIlgWhBfXvH7bT7rDI="
}
(All data encode with BASE64 algorithm as default)