示例#1
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)
示例#2
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)
示例#3
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)
示例#4
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)
 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)
示例#6
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)
示例#8
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
# 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)
    
示例#11
0
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))
print(' header ' + str(msg2a.serialize_header()))
print(' hash ' + str(sha256(msg2a.serialize_header()).hexdigest()))
if msg2a.decode(bob):
    print('decoded:', msg2a.ptxt)
示例#12
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()
 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)
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))
示例#15
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)
示例#16
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)