From 4ac91e4f2962b9ed8fdd7d3c256ea50940269e5c Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 29 Aug 2023 17:35:52 +0100 Subject: [PATCH] Move XteaKey to its own file Signed-off-by: Graham --- .../main/kotlin/org/openrs2/crypto/Xtea.kt | 67 ------------------ .../main/kotlin/org/openrs2/crypto/XteaKey.kt | 69 +++++++++++++++++++ 2 files changed, 69 insertions(+), 67 deletions(-) create mode 100644 crypto/src/main/kotlin/org/openrs2/crypto/XteaKey.kt diff --git a/crypto/src/main/kotlin/org/openrs2/crypto/Xtea.kt b/crypto/src/main/kotlin/org/openrs2/crypto/Xtea.kt index 8e46f34db9..5463d807ae 100644 --- a/crypto/src/main/kotlin/org/openrs2/crypto/Xtea.kt +++ b/crypto/src/main/kotlin/org/openrs2/crypto/Xtea.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() diff --git a/crypto/src/main/kotlin/org/openrs2/crypto/XteaKey.kt b/crypto/src/main/kotlin/org/openrs2/crypto/XteaKey.kt new file mode 100644 index 0000000000..bd73cc9996 --- /dev/null +++ b/crypto/src/main/kotlin/org/openrs2/crypto/XteaKey.kt @@ -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 + } + } + } +}