-
Notifications
You must be signed in to change notification settings - Fork 0
/
V1VoteSender.kt
40 lines (33 loc) · 1.27 KB
/
V1VoteSender.kt
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
package io.dotrun.mcvotifierlib
import java.io.DataOutputStream
import java.net.InetSocketAddress
import java.net.Socket
import java.security.KeyFactory
import java.security.interfaces.RSAPublicKey
import java.security.spec.X509EncodedKeySpec
import java.util.Base64
import javax.crypto.Cipher
data class V1VoteSender(
override val address: InetSocketAddress,
val publicKey: RSAPublicKey
) : VoteSender(address) {
constructor(address: InetSocketAddress, publicKey: ByteArray) : this(
address,
KeyFactory.getInstance("RSA")
.generatePublic(X509EncodedKeySpec(Base64.getDecoder().decode(publicKey))) as RSAPublicKey
)
constructor(address: InetSocketAddress, publicKey: String) : this(
address,
KeyFactory.getInstance("RSA")
.generatePublic(X509EncodedKeySpec(Base64.getDecoder().decode(publicKey))) as RSAPublicKey
)
private val rsa = Cipher.getInstance("RSA").also { it.init(Cipher.ENCRYPT_MODE, publicKey) }
override fun sendVote(vote: Vote) {
Socket(address.address, address.port).use { socket ->
val out = DataOutputStream(socket.getOutputStream())
val block = rsa.doFinal(vote.toString().toByteArray())
out.write(block)
out.close()
}
}
}