From 5934cb31ce9e7ecce0e2725d2510c11a6d6ed54c Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 17 Dec 2014 02:36:39 +0200 Subject: [PATCH] Reduce CryptoData size by using smaller length fields. --- PROTOCOL.md | 6 ++---- .../ensichat/protocol/messages/CryptoData.scala | 12 ++++++------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/PROTOCOL.md b/PROTOCOL.md index e4d8de7..e1d8bdb 100644 --- a/PROTOCOL.md +++ b/PROTOCOL.md @@ -106,16 +106,14 @@ message. 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Signature Length | + | Signature Length | Key Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / \ Signature (variable length) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Encryption Key Length | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / - \ Encryption Key (variable length) \ + \ Key (variable length) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ diff --git a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/CryptoData.scala b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/CryptoData.scala index b809570..71788eb 100644 --- a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/CryptoData.scala +++ b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/CryptoData.scala @@ -3,6 +3,7 @@ package com.nutomic.ensichat.protocol.messages import java.nio.ByteBuffer import java.util.Arrays +import android.util.Log import com.nutomic.ensichat.protocol.BufferUtils object CryptoData { @@ -12,11 +13,11 @@ object CryptoData { */ def read(array: Array[Byte]): (CryptoData, Array[Byte]) = { val b = ByteBuffer.wrap(array) - val signatureLength = BufferUtils.getUnsignedInt(b).toInt + val signatureLength = BufferUtils.getUnsignedShort(b) + val keyLength = BufferUtils.getUnsignedShort(b) val signature = new Array[Byte](signatureLength) b.get(signature, 0, signatureLength) - val keyLength = BufferUtils.getUnsignedInt(b).toInt val key = if (keyLength != 0) { val key = new Array[Byte](keyLength) @@ -46,18 +47,17 @@ case class CryptoData(Signature: Option[Array[Byte]], Key: Option[Array[Byte]]) /** * Writes this object into a new byte array. - * @return */ def write: Array[Byte] = { val b = ByteBuffer.allocate(length) - BufferUtils.putUnsignedInt(b, Signature.get.length) + BufferUtils.putUnsignedShort(b, Signature.get.length) + BufferUtils.putUnsignedShort(b, keyLength) b.put(Signature.get) - BufferUtils.putUnsignedInt(b, keyLength) if (Key.nonEmpty) b.put(Key.get) b.array() } - def length = 8 + Signature.get.length + keyLength + def length = 4 + Signature.get.length + keyLength private def keyLength = if (Key.isDefined) Key.get.length else 0