Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Apr 3, 2022. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
Latest commit
executable file
·128 lines (109 loc) · 3.74 KB
/
Copy pathcli.js
File metadata and controls
executable file
·128 lines (109 loc) · 3.74 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Imports
constCryptoJS=require('crypto-js');
constio=require('socket.io-client');
constreadline=require('readline');
constdotenv=require('dotenv').config();
consteff=require('./eff.js');
constcrypto=require('crypto');
// Files
// Code
constlineReader=readline.createInterface({input: process.stdin,output: process.stdout});
varusername;
varpassword;
varroomName;
// connect to socketio endpoint
letsocket;
constinitiateSocket=(room)=>{
socket=io(process.env.API);
socket.emit('join',room)
}
getUsername();
functiongetUsername(){// console.log()
lineReader.question('Username: ',(answer)=>{
username=answer;
getPassword();
});
}
functiongetPassword(){
lineReader.question('\n[i] Use "/random" to generate a key.\nRoom Key: ',(answer)=>{
if(answer==='/random'){
password=getRandomWords();
console.log(`\n[CLI] Room Key: ${password}`)
}else{
password=answer;
}
roomName=CryptoJS.SHA512(password).toString();
initiateSocket(roomName)
console.log(`\n[CLI] Connected to CryptoChat.\n`)
socket.emit('chat event',{
"roomName": roomName,
"user_name": crypt.encryptMessage(username,password),
"message": crypt.encryptMessage('has joined the room.',password)
});
socket.on('my response',incomingHandler);
setTimeout(()=>chat(),1500);
});
}
functionchat(){
lineReader.question(`--- [${username}]: `,(answer)=>{
if(answer=='/exit'){
leaveAndReload();
return;
}
if(answer.length>0){
sendMessage(answer);
}
chat();
});
}
functiongetRandomWords(){
varwordList=newArray;
for(varx=0;x<6;x++){
varnumList=newArray;
for(vary=0;y<5;y++){
varnum=crypto.randomInt(1,6);
numList.push(num);
}
varjoinedNum=Number(numList.join(''));
wordList.push(eff.wordList[joinedNum].charAt(0).toUpperCase()+eff.wordList[joinedNum].slice(1));
}
returnwordList.join('')
}
letcrypt={
encryptMessage: function(messageToEncrypt='',secretkey=''){
varencryptedMessage=CryptoJS.AES.encrypt(messageToEncrypt,secretkey);
returnencryptedMessage.toString();
},
decryptMessage: function(encryptedMessage='',secretkey=''){
vardecryptedBytes=CryptoJS.AES.decrypt(encryptedMessage,secretkey);
returndecryptedBytes.toString(CryptoJS.enc.Utf8);
}
};
functionsendMessage(message){
socket.emit('chat event',{// encrypt and send the user's name and message
"roomName": roomName,
"user_name": crypt.encryptMessage(username,password),
"message": crypt.encryptMessage(message,password)
});
}
functionincomingHandler(msg){
readline.clearLine(process.stdout,0)
readline.cursorTo(process.stdout,0,null)
vardecryptedUsername=crypt.decryptMessage(msg.user_name,password);
vardecryptedMessage=crypt.decryptMessage(msg.message,password);
if(decryptedUsername!==''||decryptedMessage!==''){// if the username and message are empty values, stop
console.log(`[${decryptedUsername}]: ${decryptedMessage}`);
chat();
}else{
vartime=newDate();
console.log(`[${time}] Could not decrypt: ${msg}`);
}
}
functionleaveAndReload(){
socket.emit('chat event',JSON.parse(JSON.stringify({
"roomName": roomName,
"user_name": crypt.encryptMessage(username,password),
"message": crypt.encryptMessage('has left the room.',password)
})));
setTimeout(()=>process.exit(0),1500)
}