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

59 lines
1.3 KiB
Kotlin

package org.hso.texturesyncclient.model
import java.lang.IllegalArgumentException
import java.util.*
import java.security.MessageDigest
class DataModel
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>,
val addedOn : Calendar,
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()
}
}