def encrypt_file(file_loc, passcode="*"):
    with open(file_loc, 'r') as inp_file:
        data = inp_file.read()
    encrypted_text = cipher.encrypt(data, passcode)
    file_loc = file_loc + ".encrypted"
    print(file_loc)
    with open(file_loc, 'wb') as out_file:
        out_file.write(encrypted_text)
    return encrypted_text
    def sendMessage(self):
        global storeName

        # while True:
        message = f"{storeName}: {self.msg}"
        message = cipher.encrypt(message)
        msg_length = len(message)
        send_length = str(msg_length).encode(FORMAT)
        send_length += b' ' * (HEADER - len(send_length))
        client.send(send_length)
        client.send(message)
예제 #3
0
def create_chat_room(chatroom_id='', chatroom_key=''):
    length = 1

    encrypted_chatroom_key = cipher.encrypt(chatroom_key)

    all_rooms = firebaseDB.child('chat_rooms').get()
    if all_rooms is not None:
        for room in all_rooms.each():
            if "details" in room.val():
                length = length + 1
                if chatroom_id == room.val()['details']['chatroomID']:
                    raise Exception('CHATROOM_EXISTS')

    data = {
        "chatroomID": chatroom_id,
        "encrypted_chatroom_key": encrypted_chatroom_key.hex(),
        "created_userID": user_auth.userID,
        "created_name": user_auth.currentUser['displayName']
    }

    user_auth.CHATROOM_NAME = f'chatroom_{length}'

    try:
        firebaseDB.child('chat_rooms').child(
            user_auth.CHATROOM_NAME).child("details").set(data)

        firebaseDB.child('chat_rooms').child(
            user_auth.CHATROOM_NAME).child('chats').child('temp').set({
                'msg':
                "",
                'user':
                "******",
            })

        firebaseDB.child('chat_rooms').child(
            user_auth.CHATROOM_NAME).child('attendees').child(
                user_auth.userID).set({
                    'name':
                    user_auth.currentUser['displayName'],
                    'email':
                    user_auth.currentUser['email'],
                    'joined_at':
                    datetime.now().timestamp(),
                    'status':
                    'ONLINE'
                })

        return data

    except Exception:
        raise Exception('ERROR_UPLOAD_DATA')
    def sendMessage(self, chats_box, msg_entry, cont):
        self.doNotUse()
        msg = msg_entry.get("1.0", 'end-1c')
        msg = msg.rstrip()

        if msg == 'q' or msg == 'Q':
            msg_entry.delete('1.0', tk.END)
            cont.on_closing(del_temp=False)
        elif msg == '':
            print('message is empty')
        elif Variables.current_status == 'offline':
            messagebox.showwarning(
                "Cannot send message", "Please first set status to ONLINE\n\n"
                "Under [user -> Change status]")
        else:
            msg_entry.delete('1.0', tk.END)
            msg = msg.rstrip()
            _time = f"{datetime.now().time().strftime('%H:%M')}"

            try:
                chats_box.configure(state=tk.NORMAL)
                chats_box.insert(tk.END, f"You:  {msg}\n")
                chats_box.configure(state=tk.DISABLED)
                self.total_msgs = self.total_msgs + 1

                # TODO encrypt the message
                encrypt_msg = cipher.encrypt(msg).hex()
                upload_msg(encrypt_msg)
            except Exception as e:
                e = str(e)

                length = len(f"You:  {msg}\n") + 1
                chats_box.configure(state=tk.NORMAL)
                chats_box.delete(f'end-{length}c', tk.END)
                chats_box.configure(state=tk.DISABLED)
                self.total_msgs = self.total_msgs - 1

                if e.find('ERROR_SEND_MSG') != -1:
                    messagebox.showerror(
                        'Message',
                        "Error sending message.\nCheck your internet.  ")