Skip to main content

A common message module

Project description

Dao Ke Dao (道可道) -- Message Module (Python)

license Version PRs Welcome Platform

This document introduces a common Message Module for decentralized instant messaging.

Copyright © 2018-2019 Albert Moky

0. Envelope

Message Envelope

/* example */
{
    "sender"   : "moki@4WDfe3zZ4T7opFSi3iDAKiuTnUHjxmXekk",
    "receiver" : "hulk@4YeVEN3aUnvC1DNUufCq1bs9zoBSJTzVEj",
    "time"     : 1545405083
}

1. Content

/* example */
{
    "type"     : 0x01,      // message type
    "sn"       : 412968873, // serial number (message ID in conversation)

    "text"     : "Hey guy!"
}

Message Content Type

class ContentType(IntEnum):

    TEXT = 0x01        # 0000 0001

    FILE = 0x10        # 0001 0000
    IMAGE = 0x12       # 0001 0010
    AUDIO = 0x14       # 0001 0100
    VIDEO = 0x16       # 0001 0110

    PAGE = 0x20        # 0010 0000

    # quote a message before and reply it with text
    QUOTE = 0x37       # 0011 0111

    MONEY = 0x40          # 0100 0000
    # TRANSFER = 0x41     # 0100 0001
    # LUCKY_MONEY = 0x42  # 0100 0010

    COMMAND = 0x88     # 1000 1000
    HISTORY = 0x89     # 1000 1001 (Entity history command)

    # top-secret message forward by proxy (Service Provider)
    FORWARD = 0xFF  # 1111 1111

2. Message

When the user want to send out a message, the client needs TWO steps before sending it:

  1. Encrypt the Instant Message to Secure Message;
  2. Sign the Secure Message to Reliable Message.

Accordingly, when the client received a message, it needs TWO steps to extract the content:

  1. Verify the Reliable Message to Secure Message;
  2. 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)

Instant Message

/* 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}

Secure Message

/**
 *  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="
}

Reliable Message

/**
 *  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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dkd-2.1.0.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

dkd-2.1.0-py3-none-any.whl (26.1 kB view details)

Uploaded Python 3

File details

Details for the file dkd-2.1.0.tar.gz.

File metadata

  • Download URL: dkd-2.1.0.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0b3

File hashes

Hashes for dkd-2.1.0.tar.gz
Algorithm Hash digest
SHA256 def877fd12e89df2a3e78cbb76b75fd2a0878e6afcb29c022e858fc107c07a2c
MD5 0a1596ee39070f5d648aac2acb87d556
BLAKE2b-256 8fb688cd1a8a8d83416bb66b017d88e0856949b5b66b3454f58061b7ef9d328e

See more details on using hashes here.

File details

Details for the file dkd-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: dkd-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 26.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0b3

File hashes

Hashes for dkd-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2bf95624d16edb4cd35235c330e766d57b30cde92c5eeb090441ba54b8125f5e
MD5 5bfad87aaba84796593e4a853e9a859d
BLAKE2b-256 24a31a91461b98b8f50d319c197a23b747a6eacaf096d55dbe7caef1f8c11355

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page