-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsandesh.py
More file actions
113 lines (84 loc) · 2.91 KB
/
sandesh.py
File metadata and controls
113 lines (84 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# sandesh_client
import base64
import sys, socket, select
from Crypto.Cipher import AES
import os
import hashlib
import signal
os.system("clear")
print """
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
___ ____ ___ __ ___
| / __// __ \|\ ||| _ \| _| / __/||_|| |
_\ \ ||__||||\||||_||| _| _\ \ | _ |
| |___/ || |||| \||___/|__||__ / || || |
|| SECRET-CHAT_ROOM||
| Created BY:Akumar7042 |
| _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _|
"""
def sigint_handler(signum, frame):
print '\n user interrupt ! shutting down'
print "[info] shutting down SANDESH \n\n"
sys.exit()
signal.signal(signal.SIGINT, sigint_handler)
def hasher(key):
hash_object = hashlib.sha512(key)
hexd = hash_object.hexdigest()
hash_object = hashlib.md5(hexd)
hex_dig = hash_object.hexdigest()
return hex_dig
def encrypt(secret,data):
BLOCK_SIZE = 32
PADDING = '{'
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
cipher = AES.new(secret)
encoded = EncodeAES(cipher, data)
return encoded
def decrypt(secret,data):
BLOCK_SIZE = 32
PADDING = '{'
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
cipher = AES.new(secret)
decoded = DecodeAES(cipher, data)
return decoded
def chat_client():
if(len(sys.argv) < 5) :
print 'Usage : python sandesh.py <hostname> <port> <password> <nick_name>'
sys.exit()
host = sys.argv[1]
port = int(sys.argv[2])
key = sys.argv[3]
key = hasher(key)
uname = sys.argv[4]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
try :
s.connect((host, port))
except :
print "\033[91m"+'Unable to connect'+"\033[0m"
sys.exit()
print "Connected to host. You can start sending messages"
sys.stdout.write("\033[34m"+'\n[Me :] '+ "\033[0m"); sys.stdout.flush()
while 1:
socket_list = [sys.stdin, s]
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
for sock in read_sockets:
if sock == s:
data = sock.recv(4096)
if not data :
print "\033[91m"+"\nDisconnected from chat server"+"\033[0m"
sys.exit()
else :
data = decrypt(key,data)
sys.stdout.write(data)
sys.stdout.write("\033[34m"+'\n[Me :] '+ "\033[0m"); sys.stdout.flush()
else :
msg = sys.stdin.readline()
msg = '[ '+ uname +': ] '+msg
msg = encrypt(key,msg)
s.send(msg)
sys.stdout.write("\033[34m"+'\n[Me :] '+ "\033[0m"); sys.stdout.flush()
if __name__ == "__main__":
sys.exit(chat_client())