Move XteaKey to its own file

Signed-off-by: Graham <gpe@openrs2.org>
Graham 1 year ago
parent 1771f86d96
commit 4ac91e4f29
  1. 67
      crypto/src/main/kotlin/org/openrs2/crypto/Xtea.kt
  2. 69
      crypto/src/main/kotlin/org/openrs2/crypto/XteaKey.kt

@ -1,79 +1,12 @@
package org.openrs2.crypto
import io.netty.buffer.ByteBuf
import java.security.SecureRandom
private const val GOLDEN_RATIO = 0x9E3779B9.toInt()
private const val ROUNDS = 32
public const val XTEA_BLOCK_SIZE: Int = 8
private const val BLOCK_SIZE_MASK = XTEA_BLOCK_SIZE - 1
public data class XteaKey(
public val k0: Int,
public val k1: Int,
public val k2: Int,
public val k3: Int
) {
public val isZero: Boolean
get() = k0 == 0 && k1 == 0 && k2 == 0 && k3 == 0
public fun toIntArray(): IntArray {
return intArrayOf(k0, k1, k2, k3)
}
public fun toHex(): String {
return Integer.toUnsignedString(k0, 16).padStart(8, '0') +
Integer.toUnsignedString(k1, 16).padStart(8, '0') +
Integer.toUnsignedString(k2, 16).padStart(8, '0') +
Integer.toUnsignedString(k3, 16).padStart(8, '0')
}
override fun toString(): String {
return toHex()
}
public companion object {
@JvmStatic
public val ZERO: XteaKey = XteaKey(0, 0, 0, 0)
@JvmStatic
@JvmOverloads
public fun generate(r: SecureRandom = secureRandom): XteaKey {
return XteaKey(r.nextInt(), r.nextInt(), r.nextInt(), r.nextInt())
}
@JvmStatic
public fun fromIntArray(a: IntArray): XteaKey {
require(a.size == 4)
return XteaKey(a[0], a[1], a[2], a[3])
}
@JvmStatic
public fun fromHex(s: String): XteaKey {
return fromHexOrNull(s) ?: throw IllegalArgumentException()
}
@JvmStatic
public fun fromHexOrNull(s: String): XteaKey? {
if (s.length != 32) {
return null
}
return try {
val k0 = Integer.parseUnsignedInt(s, 0, 8, 16)
val k1 = Integer.parseUnsignedInt(s, 8, 16, 16)
val k2 = Integer.parseUnsignedInt(s, 16, 24, 16)
val k3 = Integer.parseUnsignedInt(s, 24, 32, 16)
XteaKey(k0, k1, k2, k3)
} catch (ex: NumberFormatException) {
null
}
}
}
}
public fun ByteBuf.xteaEncrypt(index: Int, length: Int, key: XteaKey) {
val k = key.toIntArray()

@ -0,0 +1,69 @@
package org.openrs2.crypto
import java.security.SecureRandom
public data class XteaKey(
public val k0: Int,
public val k1: Int,
public val k2: Int,
public val k3: Int
) {
public val isZero: Boolean
get() = k0 == 0 && k1 == 0 && k2 == 0 && k3 == 0
public fun toIntArray(): IntArray {
return intArrayOf(k0, k1, k2, k3)
}
public fun toHex(): String {
return Integer.toUnsignedString(k0, 16).padStart(8, '0') +
Integer.toUnsignedString(k1, 16).padStart(8, '0') +
Integer.toUnsignedString(k2, 16).padStart(8, '0') +
Integer.toUnsignedString(k3, 16).padStart(8, '0')
}
override fun toString(): String {
return toHex()
}
public companion object {
@JvmStatic
public val ZERO: XteaKey = XteaKey(0, 0, 0, 0)
@JvmStatic
@JvmOverloads
public fun generate(r: SecureRandom = secureRandom): XteaKey {
return XteaKey(r.nextInt(), r.nextInt(), r.nextInt(), r.nextInt())
}
@JvmStatic
public fun fromIntArray(a: IntArray): XteaKey {
require(a.size == 4)
return XteaKey(a[0], a[1], a[2], a[3])
}
@JvmStatic
public fun fromHex(s: String): XteaKey {
return fromHexOrNull(s) ?: throw IllegalArgumentException()
}
@JvmStatic
public fun fromHexOrNull(s: String): XteaKey? {
if (s.length != 32) {
return null
}
return try {
val k0 = Integer.parseUnsignedInt(s, 0, 8, 16)
val k1 = Integer.parseUnsignedInt(s, 8, 16, 16)
val k2 = Integer.parseUnsignedInt(s, 16, 24, 16)
val k3 = Integer.parseUnsignedInt(s, 24, 32, 16)
XteaKey(k0, k1, k2, k3)
} catch (ex: NumberFormatException) {
null
}
}
}
}
Loading…
Cancel
Save