Ejemplo n.º 1
0
def mktemp(suffix=''):
    """Gives a decent random string, suitable for a filename."""
    r = random.Random()
    m = crypt.md5(suffix.encode('utf8'))
    r.seed(time.time())
    s = str(r.getstate())
    period = random.random()
    now = start = time.time()
    while start + period < now:
        time.sleep() # Induce a context switch, if possible.
        now = time.time()
        m.update(str(random.random()))
        m.update(s)
        m.update(str(now))
        s = m.hexdigest()
    return crypt.sha((s + str(time.time())).encode('utf8')).hexdigest()+suffix
Ejemplo n.º 2
0
def mktemp(suffix=''):
    """Gives a decent random string, suitable for a filename."""
    r = random.Random()
    m = crypt.md5(suffix)
    r.seed(time.time())
    s = str(r.getstate())
    period = random.random()
    now = start = time.time()
    while start + period < now:
        time.sleep()  # Induce a context switch, if possible.
        now = time.time()
        m.update(str(random.random()))
        m.update(s)
        m.update(str(now))
        s = m.hexdigest()
    return crypt.sha(s + str(time.time())).hexdigest() + suffix
def handleServerReply(cmd, clientSock):
    cmd = cmd.split()
    reply = clientSock.recv(constants.BUFF).decode()

    if (reply.split()[0] == 'Error:'):
        print(reply)
        return

    if (cmd[0].lower() == 'create_account' or cmd[0].lower() == 'login'):
        print(reply)
        global isLoggedIn
        isLoggedIn = True

        clientSock.send(str(PORT).encode(constants.FORMAT))

        global myroll
        if (cmd[0].lower() == 'login'):
            myroll = int(cmd[1])
        else:
            myroll = int(cmd[2])

        global myKey
        myKeyHex = crypt.sha(
            str(random.randint(0, constants.MAX_RANDOM) +
                int(myroll)).encode())
        myKey = int(myKeyHex, 16)
        return

    if (cmd[0].lower() == 'create' or cmd[0].lower() == 'join'):
        print(reply)
        clientSock.send(str(myroll).encode(constants.FORMAT))

        myKeyy = random.randint(0, constants.MAX_RANDOM)
        senderSendKey = crypt.diffie(constants.DIFFIE_GENERATOR, myKeyy,
                                     constants.DIFFIE_PRIME)
        clientSock.send(str(senderSendKey).encode(constants.FORMAT))

        receiverSentKey = int(
            clientSock.recv(constants.BUFF).decode(constants.FORMAT))
        sharedKey = crypt.diffie(receiverSentKey, int(myKeyy),
                                 constants.DIFFIE_PRIME)

        cipher = clientSock.recv(constants.BUFF)
        decryptedMsg = crypt.desDecrypt(cipher, str(sharedKey)).decode(
            constants.FORMAT)

        global groupNonce
        groupNonce[cmd[1]] = decryptedMsg
        return

    if (cmd[0].lower() == 'list'):
        reply = reply.split("$$")
        for i in reply:
            print(i)
        return

    if (cmd[0].lower() == 'send'):
        thread = threading.Thread(target=sendToPeer,
                                  args=(int(reply), ' '.join(cmd)))
        thread.start()
        return

    if (cmd[0].lower() == 'sendgroup'):
        print(reply)
        clientSock.send('acknowledged'.encode(constants.FORMAT))
        for i in cmd[1:-1]:
            portsList = clientSock.recv(constants.BUFF).decode().split("$$")
            clientSock.send('acknowledged'.encode(constants.FORMAT))
            for port in portsList:
                if (port != str(PORT) and len(port) > 3):
                    thread = threading.Thread(target=sendToPeer,
                                              args=(int(port), cmd[0] + ' ' +
                                                    i + ' ' + cmd[-1]))
                    thread.start()