-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
51 lines (45 loc) · 1.08 KB
/
client.ts
File metadata and controls
51 lines (45 loc) · 1.08 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
import { io } from 'socket.io-client'
import * as aes256 from 'aes256'
import * as RandomString from 'randomstring'
import * as crypto from 'crypto'
let socket = io('http://localhost:5000/messages')
function encryptRSA(text, publicKey) {
return crypto.publicEncrypt(
{
key: publicKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: 'sha256',
},
Buffer.from(text, 'ascii'),
)
}
function decryptRSA(text, privateKey) {
return crypto.privateDecrypt(
{
key: privateKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: 'sha256',
},
Buffer.from(text, 'hex'),
)
}
socket.on('connect', () => {
socket.on('PUBLIC_KEY', (publicKey) => {
const secureKey = RandomString.generate(32)
const data = aes256.encrypt(
secureKey,
JSON.stringify({
username: 'Testuser',
keyHash: 'hash',
publicKey: publicKey,
}),
)
socket.emit('connection', {
data: data,
key: encryptRSA(secureKey, publicKey),
})
})
})
socket.emit('sendToServer', {
hallo: 'hallo',
})