From 73fd94e314543e69eaac801d657365d49cf9fab1 Mon Sep 17 00:00:00 2001 From: CodeSteak Date: Mon, 3 Jun 2019 15:33:41 +0200 Subject: [PATCH] make Sha256 own Class --- .../controller/net/InternalJsonModel.kt | 19 ++------- .../hso/texturesyncclient/model/DataModel.kt | 41 ++++++++++++++++++- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/InternalJsonModel.kt b/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/InternalJsonModel.kt index f48b84f..324b430 100644 --- a/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/InternalJsonModel.kt +++ b/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/InternalJsonModel.kt @@ -2,6 +2,7 @@ package org.hso.texturesyncclient.controller.net // These types will be converted to the model.DataModel +import org.hso.texturesyncclient.model.Sha256 import org.hso.texturesyncclient.model.Texture import org.hso.texturesyncclient.model.TextureFormat import java.lang.Exception @@ -32,7 +33,7 @@ internal data class InternalTexture( tex.addedOn.get(Calendar.MONTH), // tex.addedOn.get(Calendar.DAY_OF_MONTH) ), - texture_hash = bytes2hexString(tex.textureHash) + texture_hash = tex.textureHash.toString() ) @Throws(ConnectionException::class) @@ -50,24 +51,10 @@ internal data class InternalTexture( }, resolution = Pair(resolution[0], resolution[1]), addedOn = GregorianCalendar(added_on[0], added_on[1], added_on[2]), - textureHash = hexString2Bytes(texture_hash) + textureHash = Sha256(texture_hash) ) } catch (e: Exception) { // i Know, but no time :[] throw ConnectionInvalidJsonException() } } -} - -private fun bytes2hexString(bytes: ByteArray): String { - val s = StringBuilder(64) - for (byte in bytes) { - s.append(String.format("%02X"), byte) - } - return s.toString() -} - -private fun hexString2Bytes(bytes: String): ByteArray { - return ByteArray(bytes.length / 2) { i -> - bytes.substring(i * 2, i * 2 + 2).toInt(16).toByte() - } } \ No newline at end of file diff --git a/client/src/main/kotlin/org/hso/texturesyncclient/model/DataModel.kt b/client/src/main/kotlin/org/hso/texturesyncclient/model/DataModel.kt index e473889..a8036dc 100644 --- a/client/src/main/kotlin/org/hso/texturesyncclient/model/DataModel.kt +++ b/client/src/main/kotlin/org/hso/texturesyncclient/model/DataModel.kt @@ -1,6 +1,10 @@ package org.hso.texturesyncclient.model +import java.lang.IllegalArgumentException import java.util.* +import java.security.MessageDigest + + class DataModel @@ -16,5 +20,40 @@ data class Texture( val format : TextureFormat, val resolution : Pair, val addedOn : Calendar, - val textureHash : ByteArray + val textureHash : Sha256 ) + +@Suppress("ArrayInDataClass") +class Sha256 { + + private val hashBytes : ByteArray + + @Throws(IllegalArgumentException::class) + constructor(hex : String) { + + if(hex.length != 64) { + throw IllegalArgumentException("Sha256 has wrong length.") + } + + try { + hashBytes = ByteArray(hex.length / 2) { i -> + hex.substring(i * 2, i * 2 + 2).toInt(16).toByte() + } + }catch(n : NumberFormatException) { + throw IllegalArgumentException("Hash does not only Contain '0-9a-zA-Z'.") + } + } + + constructor(data : ByteArray ) { + val digest = MessageDigest.getInstance("SHA-256") + hashBytes = digest.digest(data) + } + + override fun toString(): String { + val s = StringBuilder(64) + for (byte in hashBytes) { + s.append(String.format("%02X"), byte) + } + return s.toString() + } +} \ No newline at end of file