Open-source multiplayer game server compatible with the RuneScape client
https://www.openrs2.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.9 KiB
70 lines
1.9 KiB
9 months ago
|
package org.openrs2.crypto
|
||
|
|
||
|
import java.security.SecureRandom
|
||
|
|
||
9 months ago
|
public data class SymmetricKey(
|
||
9 months ago
|
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
|
||
9 months ago
|
public val ZERO: SymmetricKey = SymmetricKey(0, 0, 0, 0)
|
||
9 months ago
|
|
||
|
@JvmStatic
|
||
|
@JvmOverloads
|
||
9 months ago
|
public fun generate(r: SecureRandom = secureRandom): SymmetricKey {
|
||
|
return SymmetricKey(r.nextInt(), r.nextInt(), r.nextInt(), r.nextInt())
|
||
9 months ago
|
}
|
||
|
|
||
|
@JvmStatic
|
||
9 months ago
|
public fun fromIntArray(a: IntArray): SymmetricKey {
|
||
9 months ago
|
require(a.size == 4)
|
||
|
|
||
9 months ago
|
return SymmetricKey(a[0], a[1], a[2], a[3])
|
||
9 months ago
|
}
|
||
|
|
||
|
@JvmStatic
|
||
9 months ago
|
public fun fromHex(s: String): SymmetricKey {
|
||
9 months ago
|
return fromHexOrNull(s) ?: throw IllegalArgumentException()
|
||
|
}
|
||
|
|
||
|
@JvmStatic
|
||
9 months ago
|
public fun fromHexOrNull(s: String): SymmetricKey? {
|
||
9 months ago
|
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)
|
||
|
|
||
9 months ago
|
SymmetricKey(k0, k1, k2, k3)
|
||
9 months ago
|
} catch (ex: NumberFormatException) {
|
||
|
null
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|