From bba220aebc9bcd20ed2785edf95c73bf50b70902 Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 29 Jul 2023 10:23:50 +0100 Subject: [PATCH] Add SHA-1 extension method to ByteBuf Similar to the existing Whirlpool extension method. Signed-off-by: Graham --- .../main/kotlin/org/openrs2/crypto/Sha1.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 crypto/src/main/kotlin/org/openrs2/crypto/Sha1.kt diff --git a/crypto/src/main/kotlin/org/openrs2/crypto/Sha1.kt b/crypto/src/main/kotlin/org/openrs2/crypto/Sha1.kt new file mode 100644 index 0000000000..1c6b6e0af1 --- /dev/null +++ b/crypto/src/main/kotlin/org/openrs2/crypto/Sha1.kt @@ -0,0 +1,19 @@ +package org.openrs2.crypto + +import io.netty.buffer.ByteBuf +import io.netty.buffer.ByteBufUtil +import java.security.MessageDigest + +public fun ByteBuf.sha1(): ByteArray { + return sha1(readerIndex(), readableBytes()) +} + +public fun ByteBuf.sha1(index: Int, len: Int): ByteArray { + val digest = MessageDigest.getInstance("SHA-1") + if (hasArray()) { + digest.update(array(), arrayOffset() + index, len) + } else { + digest.update(ByteBufUtil.getBytes(this, index, len, false)) + } + return digest.digest() +}