TextureSync/client/src/main/kotlin/org/hso/texturesyncclient/model/DataModel.kt

71 lines
1.7 KiB
Kotlin
Raw Normal View History

2019-05-16 15:26:14 +02:00
package org.hso.texturesyncclient.model
2019-06-03 15:33:41 +02:00
import java.lang.IllegalArgumentException
2019-06-02 22:01:32 +02:00
import java.util.*
2019-06-03 15:33:41 +02:00
import java.security.MessageDigest
2019-06-02 22:01:32 +02:00
enum class TextureFormat {
PNG, JPEG,
}
@Suppress("ArrayInDataClass")
data class Texture(
val id : UUID,
val name : String,
val tags : Array<String>,
val format : TextureFormat,
val resolution : Pair<Int, Int>,
2019-06-02 22:32:19 +02:00
val addedOn : Calendar,
2019-06-03 15:33:41 +02:00
val textureHash : Sha256
2019-06-02 22:01:32 +02:00
)
2019-06-03 15:33:41 +02:00
@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) {
2019-06-03 18:28:53 +02:00
s.append(String.format("%02X", byte))
2019-06-03 15:33:41 +02:00
}
return s.toString()
}
2019-06-03 16:31:04 +02:00
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Sha256
if (!hashBytes.contentEquals(other.hashBytes)) return false
return true
}
override fun hashCode(): Int {
return hashBytes.contentHashCode()
}
2019-06-03 15:33:41 +02:00
}