示例#1
0
 def __init__(self,contents,hashkeyID=None):
     #assumes contents are objects not jsonDUMPs
     if hashkeyID is None:
         self.hashkeyID = hash_util.hash_str(str(contents))
     else:
        self.hashkeyID = hashkeyID
     self.contents = contents
示例#2
0
    def handle_message(self, msg):
        #print "got", msg, msg.recipient
        #print to, to.hashid, self.myinfo.hashid
        if msg.type == "CPOLL":
            self.handle_CPOLL(msg)
            return True
        if msg.type == "CRESP":
            cname = self.get_channel_from_hashid(msg.get_content("CHANNEL"))
            if len(msg.message) > 0:
                for m in msg.message:
                    #print m
                    ckey = ChatMessage.passwrd_to_3DES(cname)
                    chash = hash_util.hash_str(cname)
                    m.DESKEY = ChatMessage.passwrd_to_3DES(cname)
                    #print len(m.message)
                    m = copy.deepcopy(m)
                    m.decrypt()
                    m_owner = UserInfo.from_secret(m.sender)
                    #cname = self.get_channel_from_hashid(chash)
                    if str(m_owner.hashid) != str(self.myinfo.hashid):
                        print "<"+cname+">","["+m_owner.handle+"]", m.message
                subscribed_channels[cname] = msg.get_content("pollTime")
            return True
        if msg.type == "CPOST":
            self.handle_CPOST(msg)
            return True
        to = UserInfo.from_secret(msg.recipient)
        if not hash_util.hash_equal(to.hashid,self.myinfo.hashid):
            #print("got somebody else's message")
            return True
        if msg.type == "CHAT":
            origin = UserInfo.from_secret(msg.sender)
            local_origin = self.get_friend_from_hash(origin.hashid)
            if local_origin is None:
                print("You got a message from a user outside your friend list")
                return
            msg.desecure(self.myinfo)
            msg.decrypt()
            print( "{"+local_origin.handle+"} "+msg.message)
            return True
        if msg.type == "PING":
            msg.desecure(self.myinfo)
            msg.decrypt()
            to = UserInfo.from_secret(msg.sender)
            pid = msg.message
            if pid in self.open_pings.keys():
                now = time.time()
                delta = now - self.open_pings[pid]
                print self.get_friend_from_hash(to.hashid).handle, "is online", delta
            else:
                print to.handle, "is online and has pinged you"
                newmsg = ChatMessage(self.owner, to.hashid, self.owner, self.myinfo.gen_secret(False), to.gen_secret(False), pid, to.sign(pid) )
                newmsg.type = "PING"
                newmsg.encrypt()
                newmsg.secure(to)
                self.send_message(newmsg,None)
            return True

        return True
示例#3
0
 def __init__(self, myservice, filename):
     self.myservice = myservice
     self.filename = filename
     self.hashloc = hash_util.hash_str(filename)
     self.contents = ""
     self.waiting = True
     request = Database_Message(myservice.owner, self.hashloc)
     myservice.send_message(request,None)
示例#4
0
 def __init__(self,filename):
     self.filename = filename
     self.filehash_name = hash_util.hash_str(filename)
     self.done = False
     self.has_key = False
     self.input_stream = Queue.Queue()
     self.chunklist = []
     self.chunks = {}
示例#5
0
 def __init__(self, myservice, filename):
     self.myservice = myservice
     self.filename = filename
     self.hashloc = hash_util.hash_str(filename)
     self.contents = ""
     self.waiting = True
     request = Database_Message(myservice.owner, self.hashloc)
     myservice.send_message(request, None)
示例#6
0
    def handle_command(self, comand_st, arg_str):
        #print "debug", comand_st, arg_str
        global subscribed_channels
        ### one of your commands got typed in
        if comand_st == "/listen":
            chan = arg_str
            if not chan in subscribed_channels.keys():
                subscribed_channels[chan] = 0.0
        if comand_st == "/post":
            chan, msg = arg_str.split(" ",1)
            ckey = ChatMessage.passwrd_to_3DES(chan)
            chash = hash_util.hash_str(chan)
            pmsg = ChatMessage(self.owner, chash, self.owner, self.myinfo.gen_secret(False), "NONE", msg, "NONE" )
            pmsg.DESKEY = ChatMessage.passwrd_to_3DES(chan)
            pmsg.type = "CPOST"         
            pmsg.encrypt()
            pmsg.DESKEY = None
            self.send_message(pmsg, None)

        if comand_st == "/add": 
            self.add_friend(arg_str)
        if comand_st == "/send":
            args = arg_str.split(" ",1)
            to_str = args[0]
            to = self.get_friend(to_str)
            if to is None:
                return
            try:
                msg = args[1]
            except:
                print("you need a message")
                return
            newmsg = ChatMessage(self.owner, to.hashid, self.owner, self.myinfo.gen_secret(False), to.gen_secret(False), msg, to.sign(msg) )
            newmsg.encrypt()
            newmsg.secure(to)
            self.send_message(newmsg, None)
        if comand_st == "/whoami":
            print( self.myinfo.gen_secret(False))
        if comand_st == "/who":
            print("Your friends are:")
            for f in self.friends:
                print(f.handle)
            for f in self.friends:
                self.ping(f.handle)
        if comand_st == "/save":
            mylist = [self.myinfo]+self.friends
            write_preferences("userinfo/data.txt",mylist)
        if comand_st == "/rename":
            args = arg_str.split(" ")
            old = args[0]
            new = args[1]
            f = self.get_friend(old)
            if not f is None:
                f.handle = new
                print(old+" has been renamed "+new)
        if comand_st == "/ping":
            user = arg_str
            self.ping(user)
示例#7
0
 def poll_channels(self):
     global subscribed_channels
     for c in subscribed_channels.keys():
        # print "pollin", c
         lasttime = subscribed_channels[c]
         chash = hash_util.hash_str(c)
         newmsg = ChatMessage(self.owner, chash, self.owner, self.myinfo.gen_secret(False), "NONE", str(lasttime), "NONE" )
         newmsg.type = "CPOLL"         
         self.send_message(newmsg, None)
         
     time.sleep(1)
示例#8
0
 def writeFile(self,key):
     keyfilestr = str(key)
     hashid = hash_util.hash_str(key.name)
     d = Data_Atom(keyfilestr,hashid)
     self.putChunk(d)
     atoms = []
     for k in key.chunks:
         atoms.append(key.chunks[k])
     mystuff = self.polite_distribute(atoms)
     for a in mystuff:
         writeChunk(a)
示例#9
0
 def http_get(self,path):
     path = path[1:].upper()
     possible = path.split(".")
     path = possible[-2]
     key = hash_util.hash_str(path)
     self.open_requests[key] = Open_request(self, path)
     data = self.open_requests[key].get_file(blocking=True)
     try:
         raw = data[:-64]
         sig = data[-64:]
         if self.blockchain.validate(raw,sig,path):
             return raw
         else:
             return "403 Forbidden"
     except ValueError:
         return data
示例#10
0
 def http_get(self, path):
     path = path[1:].upper()
     possible = path.split(".")
     path = possible[-2]
     key = hash_util.hash_str(path)
     self.open_requests[key] = Open_request(self, path)
     data = self.open_requests[key].get_file(blocking=True)
     try:
         raw = data[:-64]
         sig = data[-64:]
         if self.blockchain.validate(raw, sig, path):
             return raw
         else:
             return "403 Forbidden"
     except ValueError:
         return data
示例#11
0
def stage():
    filename = ".\\tests\\shakespeare.txt"
    key = makeKeyFile(filename,smartChunk)
    cfs = getCFSsingleton()
    cfs.writeFile(key)
    
    time.sleep(2)
    
    # generate the id for the keyfile from the filename
    hashid =hash_util.hash_str(filename)
    keyfile_raw = cfs.getChunk(hashid).contents
    keyfile = KeyFile.parse(keyfile_raw)
    atoms = []
    for key in keyfile.chunklist:
        atoms.append(Data_Atom(key,key))
    return atoms
示例#12
0
 def store(self, filename,segment_size=128):
     segment_ids = []
     segments = []
     with open(filename, "rb") as f:
         byte = f.read(segment_size)
         while byte:
             newid = hash_util.generate_random_key().key
             segment_ids.append(newid)
             segments.append(byte)
             byte = f.read(segment_size)
     keyfile = "KEYFILE\n"+filename+"\n"+reduce(lambda x,y: x+"\n"+y,segment_ids)
     keyfile_hash = hash_util.hash_str(filename)
     keyfile_message = shelver.Database_Message(self.owner,keyfile_hash, "FileSystem", "PUT")
     keyfile_message.add_content("file_contents",keyfile)
     self.send_message(keyfile_message,None)
     number_of_chunks = len(segments)
     for i in range(0,number_of_chunks):
         chunkfile_message = shelver.Database_Message(self.owner,hash_util.Key(segment_ids[i]), "FileSystem", "PUT")
         actual_contents = str(keyfile_hash)+"\n"+segments[i]
         chunkfile_message.add_content("file_contents",actual_contents)
         self.send_message(chunkfile_message,None)
示例#13
0
 def sign(self, msg):
     hash_to_sign = hash_util.hash_str(msg).key
     return rsa.encrypt(hash_to_sign, self.publickey)
示例#14
0
#hash of Last Block|blockid|Exchanges|declaration|timestamp|nonce
#64 bit int
#hash of last block = 160 bit hash of the previous block
#Exchanges = <Domain,Newowner>sig(oldowner)
#Declaration = <Domain, newowner>
#timestamp = unix epoc time
#none =

# '|' is the divider between sections
# '+' is the divider between Exchanges
#EXAMPLE
"""
test = hash_util.hash_str("TEST")
print test
test_user = sec_service.SecNodeInfo.generate_new("Brendan")
print test_user.gen_secret(False)
"""
difficulty = 2**140

test = """0x0000000000000000000000000000000000000000|0||<test,Brendan:0x540be95c12e72a28c32388994ff0efb12ac63ca7:0x800b400ed966fcc0d4807adf5e884f1ed6af227aaeb00c463ebdf473d4da1b9bde466a9d155df80dbc565575a5130344691425d0f705c88514d77912f3fd339d?0x10001>|1384540375.4590001|"""

nonce = hash_util.generate_random_key()
newhash = hash_util.hash_str(test + str(nonce))
while newhash.toint() > difficulty:
    print
    nonce = hash_util.generate_random_key()
    newhash = hash_util.hash_str(test + str(nonce))
print ""
print nonce
print newhash
示例#15
0
 def http_get(self,path):
     key = hash_util.hash_str(path)
     self.open_requests[key] = Open_request(self, path)
     return self.open_requests[key].get_file(blocking=True)
示例#16
0
 def get_record(self, name):
     hash_loc = hash_util.hash_str(name)
     newmsg = Database_Message(self.owner, hash_loc, "ECHO", "GET")
     self.send_message(newmsg)
示例#17
0
 def read(self,filename):
     filehash = hash_util.hash_str(filename)
     new_partial = Partial_file(filename)
     self.partial_files[str(filehash)]=new_partial
     get_keyfile_message = shelver.Database_Message(self.owner,filehash, "FileSystem", "GET")
     self.send_message(get_keyfile_message,None)
示例#18
0
    def validate(self, msg, signature):
        unsigned = rsa.decrypt(signature, self.publickey)
        hash_to_sign = hash_util.hash_str(msg).key

        return unsigned == hash_to_sign
示例#19
0
 def get_record(self,name,return_service="DATABASE"):
     hash_loc = hash_util.hash_str(name)
     newmsg = Database_Message(self.owner,hash_loc,SERVICE_ECHO,GET)
     self.send_message(newmsg, None)
示例#20
0
 def get_record(self,node_info, name):
     hash_loc = hash_util.hash_str(name)
     newmsg = Database_Get_Message(node_info,hash_loc,SERVICE_ECHO,GET)
     self.send_message(newmsg)
示例#21
0
 def generate_new(cls, handle):
     (pubkey, privkey) = rsa.newkeys(512)
     pubkey_str = hex(pubkey.n)[:-1] + "?" + hex(pubkey.e)[:-1]
     myhash = hash_util.hash_str(pubkey_str)
     return SecNodeInfo(handle, myhash, pubkey, privkey)
示例#22
0
 def put_record(self, name, data):
     hash_loc = hash_util.hash_str(name)
     newmsg = Database_Message(self.owner, hash_loc, DATABASE, PUT)
     newmsg.add_content("file_contents", data)
     self.send_message(newmsg, None)
示例#23
0
 def get_record(self, name, return_service="DATABASE"):
     hash_loc = hash_util.hash_str(name)
     newmsg = Database_Message(self.owner, hash_loc, SERVICE_ECHO, GET)
     self.send_message(newmsg, None)
示例#24
0
 def get_channel_from_hashid(self, hid):
     for c in subscribed_channels.keys():
         if hash_util.hash_str(c) == hid:
             return c
     return None
示例#25
0
 def get_record(self,name):
     hash_loc = hash_util.hash_str(name)
     newmsg = Database_Message(self.owner,hash_loc,"ECHO","GET")
     self.send_message(newmsg)
示例#26
0
 def getfileinfo(self,path,task):
     fileloc = hash_util.hash_str(path)
     request_message = shelver.Database_Message(self.owner, fileloc, Response_service="FTP", file_type="GET")
     self.send_message(request_message,None)
     self.outstanding_jobs.append(("get",fileloc,task))
示例#27
0
 def put_record(self,node_info, name,data):
     hash_loc = hash_util.hash_str(name)
     newmsg = Database_Put_Message(node_info,hash_loc, data)
     self.send_message(newmsg)
示例#28
0
#Exchanges = <Domain,Newowner>sig(oldowner)
#Declaration = <Domain, newowner>
#timestamp = unix epoc time
#none = 

# '|' is the divider between sections
# '+' is the divider between Exchanges
#EXAMPLE
"""
test = hash_util.hash_str("TEST")
print test
test_user = sec_service.SecNodeInfo.generate_new("Brendan")
print test_user.gen_secret(False)
"""
difficulty = 2**140


test="""0x0000000000000000000000000000000000000000|0||<test,Brendan:0x540be95c12e72a28c32388994ff0efb12ac63ca7:0x800b400ed966fcc0d4807adf5e884f1ed6af227aaeb00c463ebdf473d4da1b9bde466a9d155df80dbc565575a5130344691425d0f705c88514d77912f3fd339d?0x10001>|1384540375.4590001|"""


nonce = hash_util.generate_random_key()
newhash = hash_util.hash_str(test+str(nonce))
while newhash.toint() > difficulty:
	print 
	nonce = hash_util.generate_random_key()
	newhash = hash_util.hash_str(test+str(nonce))
print ""
print nonce
print newhash

示例#29
0
 def put_record(self,name,data):
     hash_loc = hash_util.hash_str(name)
     newmsg = Database_Message(self.owner,hash_loc,DATABASE,PUT)
     newmsg.add_content("file_contents",data)
     self.send_message(newmsg, None)
示例#30
0
 def generate_new(cls, handle):
     (pubkey, privkey) = rsa.newkeys(1024)
     pubkey_str = hex(pubkey.n)[:-1]+"?"+hex(pubkey.e)[:-1]
     myhash = hash_util.hash_str(pubkey_str)
     return UserInfo(handle,myhash,pubkey,privkey)
示例#31
0
 def getfileinfo(self,path,task):
     fileloc = hash_util.hash_str(path)
     request_message = shelver.Database_Message(self.owner, fileloc, Response_service="FTP", file_type="GET")
     self.send_message(request_message,None)
     self.outstanding_jobs.append(("get",fileloc,task))
示例#32
0
    def validate(self, msg, signature):
        unsigned = rsa.decrypt(signature, self.publickey)
        hash_to_sign = hash_util.hash_str(msg).key

        return unsigned == hash_to_sign
示例#33
0
 def sign(self, msg):
     hash_to_sign = hash_util.hash_str(msg).key
     return rsa.encrypt(hash_to_sign, self.publickey)
示例#34
0
 def gethashval(self):
     if self.raw == "":
         self.raw = str(self)
     return hash_util.hash_str(self.raw).key