Skip to content

Commit

Permalink
Some mor bugfixes with wrong decryption and wrong topics.
Browse files Browse the repository at this point in the history
  • Loading branch information
RaHoni committed Apr 21, 2021
1 parent 4e830ed commit 2528aaa
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/PrivateHome/Devices/MQTT/mqttClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ object mqttClient {

class subscribeTopic(topic: String) extends topic(topic)

object stat extends subscribeTopic(home + "stat/")
object stat extends subscribeTopic(home + "status/")

object setupRequest extends subscribeTopic(home + "setupRequest/")

Expand Down
14 changes: 10 additions & 4 deletions src/main/scala/PrivateHome/Devices/MQTT/mqttController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,27 @@ class mqttController(val masterID: String, _key: Array[Byte], val name: String =

def receiveStatuschange(message: Array[Byte]): Unit = {
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(message.slice(0, 16)))
val messageString = new String(cipher.doFinal(message.slice(16, message.length)), "ASCII")
val messageString = decrypt(message)
logger.debug("Trying to parse {}", messageString)
val messageJson = parse(messageString)
logger.debug("got parsed to {}",messageJson)
val pin = (messageJson \ "pin").extract[Int]
val value: Float = (messageJson \ "value").extract[String].toFloat / 1024
logger.debug("Setting pin {} to {}", pin, value)
pins(pin).status = value
}

def checkSetup(message: Array[Byte]): Boolean = {
def decrypt(message: Array[Byte]): String = {
val iv = message.slice(0, 16)
println(message.map("%02x" format _).mkString(","))
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv))
var messageDecrypted = cipher.doFinal(message.slice(16, message.length))
println(messageDecrypted.map("%02X" format _).mkString("Message: ",",",""))
messageDecrypted = messageDecrypted.slice(0,messageDecrypted.length-messageDecrypted.last)
val messageString = new String(messageDecrypted, "ASCII")
new String(messageDecrypted, "ASCII")
}

def checkSetup(message: Array[Byte]): Boolean = {
val messageString = decrypt(message)
val valid = messageString.equals(masterID)
if (!valid) logger.info("Setup requested for {} with message {}", masterID, messageString)
valid
Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "0.4.1"
version in ThisBuild := "0.4.2"

0 comments on commit 2528aaa

Please sign in to comment.