def run_test1():
    with CTClient() as c:
        # syncronous calls
        m.refresh()
        print('MsgStore opened as ' + str(m))
        print()
        hdrs = m.get_headers()
        print('MsgStore all headers : ' + str(hdrs))
        print()
        peers = m.get_peers()
        print('MsgStore found ' + str(len(peers)) + ' peers : ')
        for p in peers:
            print('    ' + str(p))
        print()
        for i in range(0,5):
            h = random.choice(hdrs)
            msg = m.get_message(h)
            print('random message retreived as ' + msg.serialize().decode())
            msg_test = m.get_message_by_id(h.Iraw())
            assert msg == msg_test
            #print('random message retreived as ' + msg_test.serialize().decode())
            print()
        Apriv = PrivateKey()
        Apriv.randomize(4)
        Apub = PublicKey.deserialize(Apriv.serialize_pubkey())
        Bpriv = PrivateKey()
        Bpriv.randomize(4)
        Bpub = PublicKey.deserialize(Apriv.serialize_pubkey())
        mtxt = 'the quick brown fox jumped over the lazy dog'
        for ver in ['0200']:
            msg = Message.encode(mtxt, Bpub, Apriv, version=ver)
            r = m.post_message(msg)
            print('message posted, server metadata' + str(r))
        print()
Example #2
0
def run_test1():
    with CTClient() as c:
        # syncronous calls
        m.refresh()
        print('MsgStore opened as ' + str(m))
        print()
        hdrs = m.get_headers()
        print('MsgStore all headers : ' + str(hdrs))
        print()
        peers = m.get_peers()
        print('MsgStore found ' + str(len(peers)) + ' peers : ')
        for p in peers:
            print('    ' + str(p))
        print()
        for i in range(0, 5):
            h = random.choice(hdrs)
            msg = m.get_message(h)
            print('random message retreived as ' + msg.serialize().decode())
            msg_test = m.get_message_by_id(h.Iraw())
            assert msg == msg_test
            #print('random message retreived as ' + msg_test.serialize().decode())
            print()
        Apriv = PrivateKey()
        Apriv.randomize(4)
        Apub = PublicKey.deserialize(Apriv.serialize_pubkey())
        Bpriv = PrivateKey()
        Bpriv.randomize(4)
        Bpub = PublicKey.deserialize(Apriv.serialize_pubkey())
        mtxt = 'the quick brown fox jumped over the lazy dog'
        for ver in ['0200']:
            msg = Message.encode(mtxt, Bpub, Apriv, version=ver)
            r = m.post_message(msg)
            print('message posted, server metadata' + str(r))
        print()
def run_test4():
    with CTClient() as c:
        # syncronous call
        m.refresh()
        print('MsgStore opened as ' + str(m))
        print()
        Apriv = PrivateKey()
        Apriv.randomize(4)
        Apub = PublicKey.deserialize(Apriv.serialize_pubkey())
        Bpriv = PrivateKey()
        Bpriv.randomize(4)
        Bpub = PublicKey.deserialize(Apriv.serialize_pubkey())
        # asynchronous calls
        for ver in ['0200']:
            mtxt = 'the quick brown fox jumped over the lazy dog'
            msgs = []
            print('Encoding messages ')
            print()
            for i in range(0,5):
                msg = Message.encode(mtxt, Bpub, Apriv, version=ver)
                msgs.append(msg)
            print('Posting messages')
            print()
            slist = []
            for msg in msgs:
                h = MessageHeader.deserialize(msg.serialize_header())
                r = yield m.post_message(msg, callback=register_message)
                print('sent async post for ' + h.serialize().decode())
                print()
                mtxt += 'the quick brown fox jumped over the lazy dog'
Example #4
0
def run_test4():
    with CTClient() as c:
        # syncronous call
        m.refresh()
        print('MsgStore opened as ' + str(m))
        print()
        Apriv = PrivateKey()
        Apriv.randomize(4)
        Apub = PublicKey.deserialize(Apriv.serialize_pubkey())
        Bpriv = PrivateKey()
        Bpriv.randomize(4)
        Bpub = PublicKey.deserialize(Apriv.serialize_pubkey())
        # asynchronous calls
        for ver in ['0200']:
            mtxt = 'the quick brown fox jumped over the lazy dog'
            msgs = []
            print('Encoding messages ')
            print()
            for i in range(0, 5):
                msg = Message.encode(mtxt, Bpub, Apriv, version=ver)
                msgs.append(msg)
            print('Posting messages')
            print()
            slist = []
            for msg in msgs:
                h = MessageHeader.deserialize(msg.serialize_header())
                r = yield m.post_message(msg, callback=register_message)
                print('sent async post for ' + h.serialize().decode())
                print()
                mtxt += 'the quick brown fox jumped over the lazy dog'
Example #5
0
 def get_message_by_id(self, msgid, callback=None, nak=None, onions=None):
     if isinstance(msgid, bytes):
         msgid = msgid.decode()
     if callback is None:
         r = self.get(_download_message + msgid, nak=None, onions=None)
         if r is None:
             return None
         return Message.deserialize(r)
     else:
         # print('submitting NestedRequest for ' + self._baseurl() + _download_message + hdr.Iraw().decode() + ' with callback ' + str(callback))
         return NestedRequest().get(self, _download_message + msgid, callback=self._cb_get_message, callback_next=callback, nak=nak, onions=onions)
Example #6
0
 def get_message(self, hdr, callback=None, nak=None, onions=None):
     self._sync_headers()
     if hdr not in self.headers:
         return None
     if callback is None:
         r = self.get(_download_message + hdr.Iraw().decode(), nak=nak, onions=onions)
         if r is None:
             return None
         return Message.deserialize(r)
     else:
         # print('submitting NestedRequest for ' + self._baseurl() + _download_message + hdr.Iraw().decode() + ' with callback ' + str(callback))
         return NestedRequest().get(self, _download_message + hdr.Iraw().decode(), callback=self._cb_get_message, callback_next=callback, nak=nak, onions=onions)
Example #7
0
 def _cb_get_async(self, s, r):
     qen = [qe for qe in self._get_queue if qe[0] == r.url]
     cb = qen[0][1]
     self._gq_lock.acquire()
     self._get_queue.remove(qen[0])
     self._gq_lock.release()
     if cb is None:
         return None
     if r.status_code != 200:
         print('Async Reply Error ' + str(r.status_code) + ' ' + r.url)
         return cb(None)
     msg = Message.deserialize(r.text.encode())
     return cb(msg)
Example #8
0
 def _cb_get_async(self, s, r):
     qen = [qe for qe in self._get_queue if qe[0] == r.url]
     cb = qen[0][1]
     self._gq_lock.acquire()
     self._get_queue.remove(qen[0])
     self._gq_lock.release()
     if cb is None:
         return None
     if r.status_code != 200:
         print('Async Reply Error ' + str(r.status_code) + ' ' + r.url)
         return cb(None)
     msg = Message.deserialize(r.text.encode())
     return cb(msg)
Example #9
0
def run_test10():
    with CTClient() as c:
        # synchronous onion post
        m.refresh()
        m2.refresh()
        peers = m.get_peers()
        hdrs = m.get_headers()
        onions = []
        for p in peers:
            onion = MsgStore(p['host'], p['port'])
            onion.refresh()
            if onion.Pkey is not None:
                onions.append(onion)
        nonion = len(onions)
        print('found ' + str(nonion) + ' onion hosts')
        for o in onions:
            print('    ' + str(o))
            onion._sync_headers()
        Apriv = PrivateKey()
        Apriv.randomize(4)
        Apub = PublicKey.deserialize(Apriv.serialize_pubkey())
        Bpriv = PrivateKey()
        Bpriv.randomize(4)
        Bpub = PublicKey.deserialize(Apriv.serialize_pubkey())
        # asynchronous calls
        for ver in ['0200']:
            mtxt = 'the quick brown fox jumped over the lazy dog'
            msgs = []
            print('Encoding messages ')
            print()
            for i in range(0, 5):
                msg = Message.encode(mtxt, Bpub, Apriv, version=ver)
                msgs.append(msg)
            for msg in msgs:
                h = MessageHeader.deserialize(msg.serialize_header())
                orand = random.sample(onions, min(nonion - 1, 3))
                print('posting via onions')
                for o in orand:
                    print('    ' + str(o))
                print()
                r = yield orand[0].post_message(msg,
                                                nak=nak,
                                                onions=orand[1:],
                                                callback=register_message)
                # conf = m.post_message(msg, nak=nak, onions=[m, m2])
                # conf = m2.post_message(msg, nak=nak, onions=[m2])
                print('sent post for header ' + h.serialize().decode())
                print()  # msg = m.get_message(h, nak=nak, onions=[m, m2])
 def get_message_by_id(self, msgid, callback=None, nak=None, onions=None):
     if isinstance(msgid, bytes):
         msgid = msgid.decode()
     if callback is None:
         r = self.get(_download_message + msgid, nak=None, onions=None)
         if r is None:
             return None
         return Message.deserialize(r)
     else:
         # print('submitting NestedRequest for ' + self._baseurl() + _download_message + hdr.Iraw().decode() + ' with callback ' + str(callback))
         return NestedRequest().get(self,
                                    _download_message + msgid,
                                    callback=self._cb_get_message,
                                    callback_next=callback,
                                    nak=nak,
                                    onions=onions)
def run_test10():
    with CTClient() as c:
        # synchronous onion post
        m.refresh()
        m2.refresh()
        peers = m.get_peers()
        hdrs = m.get_headers()
        onions = []
        for p in peers:
            onion = MsgStore(p['host'], p['port'])
            onion.refresh()
            if onion.Pkey is not None:
                onions.append(onion)
        nonion = len(onions)
        print('found ' + str(nonion) + ' onion hosts')
        for o in onions:
            print('    ' + str(o))
            onion._sync_headers()
        Apriv = PrivateKey()
        Apriv.randomize(4)
        Apub = PublicKey.deserialize(Apriv.serialize_pubkey())
        Bpriv = PrivateKey()
        Bpriv.randomize(4)
        Bpub = PublicKey.deserialize(Apriv.serialize_pubkey())
        # asynchronous calls
        for ver in ['0200']:
            mtxt = 'the quick brown fox jumped over the lazy dog'
            msgs = []
            print('Encoding messages ')
            print()
            for i in range(0,5):
                msg = Message.encode(mtxt, Bpub, Apriv, version=ver)
                msgs.append(msg)
            for msg in msgs:
                h = MessageHeader.deserialize(msg.serialize_header())
                orand = random.sample(onions, min(nonion-1,3))
                print('posting via onions')
                for o in orand:
                    print('    ' + str(o))
                print()
                r = yield orand[0].post_message(msg, nak=nak, onions=orand[1:], callback=register_message)
                # conf = m.post_message(msg, nak=nak, onions=[m, m2])
                # conf = m2.post_message(msg, nak=nak, onions=[m2])
                print('sent post for header ' + h.serialize().decode())
                print()# msg = m.get_message(h, nak=nak, onions=[m, m2])
Example #12
0
 def get_message(self, hdr, callback=None):
     self._sync_headers()
     if hdr not in self.headers:
         return None
     r = None
     try:
         r = self.session.get(self.baseurl + _download_message + hdr.Iraw().decode(), stream=True)
     except (Timeout, ConnectionError, HTTPError):
         return None
     if r.status_code != 200:
         return None
     raw = b''
     for chunk in r:
         raw += chunk
     msg = Message.deserialize(raw)
     if callback:
         return callback(msg)
     return msg
 def get_message(self, hdr, callback=None, nak=None, onions=None):
     self._sync_headers()
     if hdr not in self.headers:
         return None
     if callback is None:
         r = self.get(_download_message + hdr.Iraw().decode(),
                      nak=nak,
                      onions=onions)
         if r is None:
             return None
         return Message.deserialize(r)
     else:
         # print('submitting NestedRequest for ' + self._baseurl() + _download_message + hdr.Iraw().decode() + ' with callback ' + str(callback))
         return NestedRequest().get(self,
                                    _download_message + hdr.Iraw().decode(),
                                    callback=self._cb_get_message,
                                    callback_next=callback,
                                    nak=nak,
                                    onions=onions)
Example #14
0
 def get_message(self, hdr, callback=None):
     self._sync_headers()
     if hdr not in self.headers:
         return None
     r = None
     try:
         r = self.session.get(self.baseurl + _download_message +
                              hdr.Iraw().decode(),
                              stream=True)
     except (Timeout, ConnectionError, HTTPError):
         return None
     if r.status_code != 200:
         return None
     raw = b''
     for chunk in r:
         raw += chunk
     msg = Message.deserialize(raw)
     if callback:
         return callback(msg)
     return msg
Example #15
0
from ciphrtxt.message import Message
from argparse import ArgumentParser
import sys

parser = ArgumentParser(
    description=
    'read message plaintext from stdin and write encoded message to stdout')
parser.add_argument('recipient',
                    help='recipient public key (hint: starts with P0100)')
parser.add_argument('--sender',
                    default=None,
                    help='sender private key (optional, omit for anonymous)')
clargs = parser.parse_args()

f_key = None

if clargs.sender:
    f_key = PrivateKey.deserialize(clargs.sender)
    if f_key is None:
        print('Error: unable to parse sender key', file=sys.stderr)
        exit()

t_key = PublicKey.deserialize(clargs.recipient)
if t_key is None:
    print('Error: unable to parse recipient key', file=sys.stderr)
    exit()

ptext = sys.stdin.read()
msg = Message.encode(ptext, t_key, f_key)
sys.stdout.write(msg.serialize().decode())
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from ciphrtxt.message import Message
from ciphrtxt.network import MsgStore, CTClient
from argparse import ArgumentParser
import sys

parser = ArgumentParser(description='post encoded message read from stdin to server')
parser.add_argument('--host', default='ciphrtxt.com', help='hostname or IP address of server (default ciphrtxt.com)')
parser.add_argument('--port', default=7754, help='specify server port (default = 7754)')
clargs = parser.parse_args()

with CTClient() as c:
    mtxt = sys.stdin.read()

    msg = Message.deserialize(mtxt)
    if msg is None:
        print('Error: Message format invalid', file=sys.stderr)
        exit()

    ms = MsgStore(str(clargs.host), int(clargs.port))
    reachable = ms.refresh()
    if not reachable:
        print('Error: host unreachable', file=sys.stderr)
        exit()

    metadata = ms.post_message(msg)
    if metadata is None:
        print('Error: upload failed', file=sys.stderr)
        exit()
    
parser.add_argument('--sender', default=None, help='sender public key (optional for signature verification)')
clargs = parser.parse_args()

f_key = None

if clargs.sender:
    f_key = PublicKey.deserialize(clargs.sender)
    if f_key is None:
        print('Error: unable to parse sender key', file=sys.stderr)
        exit()

t_key = PrivateKey.deserialize(clargs.recipient)
if t_key is None:
    print('Error: unable to parse recipient key', file=sys.stderr)
    exit()

ctext = sys.stdin.read()

msg = Message.deserialize(ctext)
if not msg.decode(t_key):
    print('Error: cannot decrypt message', file=sys.stderr)
    exit()

if f_key is not None:
    if not msg.is_from(f_key):
        print('Error: message signature failed', file=sys.stderr)
        exit()

print(msg.ptxt)
    
Example #18
0
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from ciphrtxt.keys import PrivateKey
from ciphrtxt.topic import TopicKey
from ciphrtxt.message import Message

topics = [ "#ciphrtxt", "#crypto", "#privacy", "#privacz", "#security", "#securitz" ]

ptext = 'the quick brown fox jumped over the lazy dog'

pkey = PrivateKey()
pkey.randomize()

for topic in topics:
    k = TopicKey(topic)
    print('topic = ' + topic)
    print('key = ' + str(k))
    assert k is not None
    m = Message.encode(ptext, k)
    assert m is not None
    print('encoded = ' + str(m))
    assert m.decode(k)
    print('decoded = ' + m.ptxt)
    
Example #19
0
 def _cb_get_message(self, resp, callback_next):
     self.reply_log.append((resp, callback_next))
     if resp is None:
         return callback_next(None)
     m = Message.deserialize(resp)
     return callback_next(m)
Example #20
0
parser = ArgumentParser(
    description='post encoded message read from stdin to server')
parser.add_argument(
    '--host',
    default='ciphrtxt.com',
    help='hostname or IP address of server (default ciphrtxt.com)')
parser.add_argument('--port',
                    default=7754,
                    help='specify server port (default = 7754)')
clargs = parser.parse_args()

with CTClient() as c:
    mtxt = sys.stdin.read()

    msg = Message.deserialize(mtxt)
    if msg is None:
        print('Error: Message format invalid', file=sys.stderr)
        exit()

    ms = MsgStore(str(clargs.host), int(clargs.port))
    reachable = ms.refresh()
    if not reachable:
        print('Error: host unreachable', file=sys.stderr)
        exit()

    metadata = ms.post_message(msg)
    if metadata is None:
        print('Error: upload failed', file=sys.stderr)
        exit()
test_msgs = 1500

print('creating alice keys')
alice = PrivateKey()
alice.randomize(4)
aliceP = PublicKey.deserialize(alice.serialize_pubkey())
print('creating bob keys')
bob = PrivateKey()
bob.randomize(4)
bobP = PublicKey.deserialize(bob.serialize_pubkey())
print('keys complete')

mtxt = 'the quick brown fox jumped over the lazy dog'
msg1 = Message.encode(mtxt,
                      bobP,
                      alice,
                      progress_callback=progress,
                      version="0100")
print('message1 = ' + str(msg1))
print(' header ' + str(msg1.serialize_header()))
print(' hash ' + str(sha256(msg1.serialize_header()).hexdigest()))
msg1a = Message.deserialize(msg1.serialize())
print('message1a = ' + str(msg1a))
print(' header ' + str(msg1a.serialize_header()))
print(' hash ' + str(sha256(msg1a.serialize_header()).hexdigest()))
if msg1a.decode(bob):
    print('decoded:', msg1a.ptxt)
msg2 = Message.encode_impersonate(mtxt,
                                  aliceP,
                                  bob,
                                  progress_callback=progress,
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from ciphrtxt.keys import PrivateKey
from ciphrtxt.topic import TopicKey
from ciphrtxt.message import Message

topics = [
    "#ciphrtxt", "#crypto", "#privacy", "#privacz", "#security", "#securitz"
]

ptext = 'the quick brown fox jumped over the lazy dog'

pkey = PrivateKey()
pkey.randomize()

for topic in topics:
    k = TopicKey(topic)
    print('topic = ' + topic)
    print('key = ' + str(k))
    assert k is not None
    m = Message.encode(ptext, k)
    assert m is not None
    print('encoded = ' + str(m))
    assert m.decode(k)
    print('decoded = ' + m.ptxt)
Example #23
0
    default=None,
    help='sender public key (optional for signature verification)')
clargs = parser.parse_args()

f_key = None

if clargs.sender:
    f_key = PublicKey.deserialize(clargs.sender)
    if f_key is None:
        print('Error: unable to parse sender key', file=sys.stderr)
        exit()

t_key = PrivateKey.deserialize(clargs.recipient)
if t_key is None:
    print('Error: unable to parse recipient key', file=sys.stderr)
    exit()

ctext = sys.stdin.read()

msg = Message.deserialize(ctext)
if not msg.decode(t_key):
    print('Error: cannot decrypt message', file=sys.stderr)
    exit()

if f_key is not None:
    if not msg.is_from(f_key):
        print('Error: message signature failed', file=sys.stderr)
        exit()

print(msg.ptxt)
 def _cb_get_message(self, resp, callback_next):
     self.reply_log.append((resp, callback_next))
     if resp is None:
         return callback_next(None)
     m = Message.deserialize(resp)
     return callback_next(m)
test_keys = 1500
test_msgs = 1500

print('creating alice keys')
alice = PrivateKey()
alice.randomize(4)
aliceP = PublicKey.deserialize(alice.serialize_pubkey())
print('creating bob keys')
bob = PrivateKey()
bob.randomize(4)
bobP = PublicKey.deserialize(bob.serialize_pubkey())
print('keys complete')

mtxt = 'the quick brown fox jumped over the lazy dog'
msg1 = Message.encode(mtxt, bobP, alice, progress_callback=progress, version="0100")
print('message1 = ' + str(msg1))
print(' header ' + str(msg1.serialize_header()))
print(' hash ' + str(sha256(msg1.serialize_header()).hexdigest()))
msg1a = Message.deserialize(msg1.serialize())
print('message1a = ' + str(msg1a))
print(' header ' + str(msg1a.serialize_header()))
print(' hash ' + str(sha256(msg1a.serialize_header()).hexdigest()))
if msg1a.decode(bob):
    print('decoded:', msg1a.ptxt)
msg2 = Message.encode_impersonate(mtxt, aliceP, bob, progress_callback=progress, version="0100")
print('message2 = ' + str(msg2))
print(' header ' + str(msg2.serialize_header()))
print(' hash ' + str(sha256(msg2.serialize_header()).hexdigest()))
msg2a = Message.deserialize(msg2.serialize())
print('message2a = ' + str(msg2a))