make Sha256 own Class
This commit is contained in:
parent
7f8277415e
commit
73fd94e314
|
@ -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()
|
||||
}
|
||||
}
|
|
@ -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<Int, Int>,
|
||||
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()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue