Skip to content

Commit

Permalink
creation of network.py. gots cool decorator factories.
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Henderson <tim.tadh@gmail.com>
  • Loading branch information
timtadh committed Jul 30, 2008
1 parent 6856e6f commit 8e23f35
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 4 deletions.
8 changes: 4 additions & 4 deletions chatClient.py
Expand Up @@ -16,14 +16,14 @@ def __init__(self, name='Tim'):
def setGui(self, gui):
self.gui = gui

def setNetwork(self, network):
self.network = network
def setTcpClient(self, tcp_client):
self.tcp_client = tcp_client

def printInfo(self, data):
self.gui.printText.insert(END, data+'\n')

def sendInfo(self, data):
self.network.send(data)
self.tcp_client.send(data)

class Gui:
def __init__(self, root, printer):
Expand Down Expand Up @@ -54,7 +54,7 @@ def exit(self):
class tcpClient:

def __init__(self, printer, host, port=21567, bufsize=1024):
printer.setNetwork(self)
printer.setTcpClient(self)
self.HOST = host
self.PORT = port
self.BUFSIZE = bufsize
Expand Down
101 changes: 101 additions & 0 deletions network.py
@@ -0,0 +1,101 @@
# Generic Network Functions

import authenticator as auth
import qcrypt, os

END_MARK = 'STOP'
END_LEN = 4

def create_existance_check_dec(attr):
def dec(f, *args, **kwargs):
def h(*args, **kwargs):
if len(args) <= 0: raise Exception, 'must be applied to a class method'
attrs = dir(args[0])
if '__dict__' in attrs and args[0].__dict__.has_key(attr):
if args[0].__dict__[attr] != None:
return f(*args, **kwargs)
else:
raise Exception, attr + ' must not be None'
elif '__contains__' in attrs and args[0].__contains__(attr):
if args.__getattribute__(attr) != None:
return f(*args, **kwargs)
else:
raise Exception, attr + ' must not be None'
else:
raise Exception, 'could not detirmine the existance of ' + attr + ' in args[0]'
return h
return dec

def create_value_check_dec(attr, desired_value):
def dec(f, *args, **kwargs):
'''Assumes that an existance check has already been done'''
def h(*args, **kwargs):
attrs = dir(args[0])
if ('__dict__' in attrs):
if (args[0].__dict__[attr] != desired_value): raise Exception, attr + ' must equal ' + desired_value
return f(*args, **kwargs)
else:
if args.__getattribute__(attr) != desired_value: raise Exception, attr + ' must equal ' + desired_value
return f(*args, **kwargs)
return h

auth_msg_check = create_existance_check_dec('auth_msg')
pub_key_check = create_existance_check_dec('pub_key')
aes_key_check = create_existance_check_dec('aes_key')
authenticated_exist_check = create_existance_check_dec('authenticated')
authenticated_true_check = create_value_check_dec('authenticated', True)
key_agreement_true_check = create_value_check_dec('key_agreement', True)

class SocketLink(object):

def __init__(self, sock, secret, salt, partner_secret_hash):
self.sock = sock
self.secret = secret
self.salt = salt
self.partner_secret_hash = partner_secret_hash
self.auth_msg = None
self.pub_key = None
self.authenticated = None
self.aes_key = None
self.key_agreement = False

def read_data(self):
data = ''
while data == '':
try:
while data[(-3-END_LEN):] != '>~>STOP': data += self.sock.recv(1024)
except:
data = ''
continue
data = data[:-1*END_LEN]
return data

def send_dict(self, msg_dict):
self.sock.sendall(nDDB.encode(msg_dict)+END_MARK)

@authenticated_exist_check
@authenticated_true_check
@pub_key_check
def send_new_aes_key(self):
self.aes_key = qcrypt.normalize(qcrypt.create_aes_key())
k = qcrypt.pub_encrypt(self.aes_key, self.pub_key)
signature = auth.sign_msg(self.partner_secret_hash, k)
send_dict({'type':'setaeskey', 'value':k, 'signature':signature})
self.key_agreement = False
return self.aes_key

def begin_auth(self, sock):
pass

def request_auth(self):
auth_msg = os.urandom(64)
a = auth.create_auth(self.partner_secret_hash, auth_msg)
self.send_dict({'type':'sign_auth', 'value':a})
self.auth_msg = auth_msg
return ran_str

def sign_auth(self, auth_msg):
signed_a = auth.sign_auth(self.secret, self.salt, self.partner_secret_hash, auth_msg)
self.send_dict({'type':'verify_auth', 'value':signed_a})

def
7 changes: 7 additions & 0 deletions qcrypt.py
Expand Up @@ -2,6 +2,13 @@

from Crypto.Cipher import AES
from Crypto.Hash import SHA256
import os

def create_aes_key():
sha256 = SHA256.new()
sha256.update(os.urandom(64))
for x in xrange(5000): sha256.update(sha256.digest())
return sha256.digest()

def pub_decrypt(ciphertext, key):
try:
Expand Down

0 comments on commit 8e23f35

Please sign in to comment.