From 46b311c5ad4b87dc486e217b230a0a47044faaf0 Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 6 Feb 2021 12:54:55 +0000 Subject: [PATCH] Add XteaKey Jackson serializer Signed-off-by: Graham --- crypto/build.gradle.kts | 1 + .../org/openrs2/crypto/CryptoJacksonModule.kt | 11 +++++++++++ .../kotlin/org/openrs2/crypto/CryptoModule.kt | 5 +++++ .../org/openrs2/crypto/XteaKeySerializer.kt | 16 ++++++++++++++++ 4 files changed, 33 insertions(+) create mode 100644 crypto/src/main/kotlin/org/openrs2/crypto/CryptoJacksonModule.kt create mode 100644 crypto/src/main/kotlin/org/openrs2/crypto/XteaKeySerializer.kt diff --git a/crypto/build.gradle.kts b/crypto/build.gradle.kts index 48035e8083..a91f061ab6 100644 --- a/crypto/build.gradle.kts +++ b/crypto/build.gradle.kts @@ -4,6 +4,7 @@ plugins { } dependencies { + api("com.fasterxml.jackson.core:jackson-databind:${Versions.jackson}") api("com.google.inject:guice:${Versions.guice}") api("io.netty:netty-buffer:${Versions.netty}") api("org.bouncycastle:bcpkix-jdk15on:${Versions.bouncyCastlePkix}") diff --git a/crypto/src/main/kotlin/org/openrs2/crypto/CryptoJacksonModule.kt b/crypto/src/main/kotlin/org/openrs2/crypto/CryptoJacksonModule.kt new file mode 100644 index 0000000000..35c866d5d2 --- /dev/null +++ b/crypto/src/main/kotlin/org/openrs2/crypto/CryptoJacksonModule.kt @@ -0,0 +1,11 @@ +package org.openrs2.crypto + +import com.fasterxml.jackson.databind.module.SimpleModule +import javax.inject.Singleton + +@Singleton +public class CryptoJacksonModule : SimpleModule() { + init { + addSerializer(XteaKey::class.java, XteaKeySerializer) + } +} diff --git a/crypto/src/main/kotlin/org/openrs2/crypto/CryptoModule.kt b/crypto/src/main/kotlin/org/openrs2/crypto/CryptoModule.kt index e317916b14..f0f748ae97 100644 --- a/crypto/src/main/kotlin/org/openrs2/crypto/CryptoModule.kt +++ b/crypto/src/main/kotlin/org/openrs2/crypto/CryptoModule.kt @@ -1,7 +1,9 @@ package org.openrs2.crypto +import com.fasterxml.jackson.databind.Module import com.google.inject.AbstractModule import com.google.inject.Scopes +import com.google.inject.multibindings.Multibinder import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters public object CryptoModule : AbstractModule() { @@ -9,5 +11,8 @@ public object CryptoModule : AbstractModule() { bind(RSAPrivateCrtKeyParameters::class.java) .toProvider(RsaKeyProvider::class.java) .`in`(Scopes.SINGLETON) + + Multibinder.newSetBinder(binder(), Module::class.java) + .addBinding().to(CryptoJacksonModule::class.java) } } diff --git a/crypto/src/main/kotlin/org/openrs2/crypto/XteaKeySerializer.kt b/crypto/src/main/kotlin/org/openrs2/crypto/XteaKeySerializer.kt new file mode 100644 index 0000000000..971a070d92 --- /dev/null +++ b/crypto/src/main/kotlin/org/openrs2/crypto/XteaKeySerializer.kt @@ -0,0 +1,16 @@ +package org.openrs2.crypto + +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.databind.SerializerProvider +import com.fasterxml.jackson.databind.ser.std.StdSerializer + +public object XteaKeySerializer : StdSerializer(XteaKey::class.java) { + override fun serialize(value: XteaKey, gen: JsonGenerator, provider: SerializerProvider) { + gen.writeStartArray() + gen.writeNumber(value.k0) + gen.writeNumber(value.k1) + gen.writeNumber(value.k2) + gen.writeNumber(value.k3) + gen.writeEndArray() + } +}