diff --git a/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/internal/-Buffer.kt b/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/internal/-Buffer.kt index ae44cd2..28dca65 100644 --- a/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/internal/-Buffer.kt +++ b/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/internal/-Buffer.kt @@ -179,9 +179,7 @@ internal inline fun Buffer.commonDigestInto( callsInPlace(bufPosSet, InvocationKind.AT_MOST_ONCE) } - dest.commonCheckArgs(destOffset, digestLength, onShortInput = { - ShortBufferException("Not enough room in dest for $digestLength bytes") - }) + dest.commonCheckArgs(destOffset, digestLength, onShortInput = ::ShortBufferException) // Zero out any stale input that may be left in the buffer value.fill(0, bufPos) diff --git a/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/internal/-CommonPlatform.kt b/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/internal/-CommonPlatform.kt index e28437e..4150295 100644 --- a/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/internal/-CommonPlatform.kt +++ b/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/internal/-CommonPlatform.kt @@ -31,15 +31,15 @@ internal inline fun Digest.commonToString(): String { internal inline fun ByteArray.commonCheckArgs( offset: Int, len: Int, - onShortInput: () -> Exception = { IllegalArgumentException("Input too short") }, - onOutOfBounds: (message: String) -> Exception = { message -> IndexOutOfBoundsException(message) }, + onShortInput: (message: String) -> Exception = ::IllegalArgumentException, + onOutOfBounds: (message: String) -> Exception = ::IndexOutOfBoundsException, ) { contract { callsInPlace(onShortInput, InvocationKind.AT_MOST_ONCE) callsInPlace(onOutOfBounds, InvocationKind.AT_MOST_ONCE) } - if (size - offset < len) throw onShortInput() + if (size - offset < len) throw onShortInput("Too Short. size[$size] - offset[$offset] < len[$len]") if (offset < 0) throw onOutOfBounds("offset[$offset] < 0") if (len < 0) throw onOutOfBounds("len[$len] < 0") if (offset > size - len) throw onOutOfBounds("offset[$offset] > size[$size] - len[$len]") diff --git a/library/digest/src/jvmMain/kotlin/org/kotlincrypto/core/digest/Digest.kt b/library/digest/src/jvmMain/kotlin/org/kotlincrypto/core/digest/Digest.kt index 428ae79..70ad0ff 100644 --- a/library/digest/src/jvmMain/kotlin/org/kotlincrypto/core/digest/Digest.kt +++ b/library/digest/src/jvmMain/kotlin/org/kotlincrypto/core/digest/Digest.kt @@ -253,7 +253,7 @@ public actual abstract class Digest: MessageDigest, Algorithm, Cloneable, Copyab @Deprecated("Use digestInto", ReplaceWith("digestInto(buf, offset)")) public final override fun digest(buf: ByteArray?, offset: Int, len: Int): Int { requireNotNull(buf) { "buf cannot be null" } - buf.commonCheckArgs(offset, len, onOutOfBounds = { message -> DigestException(message) }) + buf.commonCheckArgs(offset, len, onOutOfBounds = ::DigestException) @Suppress("DEPRECATION") return engineDigest(buf, offset, len) } diff --git a/library/mac/src/commonMain/kotlin/org/kotlincrypto/core/mac/internal/-CommonPlatform.kt b/library/mac/src/commonMain/kotlin/org/kotlincrypto/core/mac/internal/-CommonPlatform.kt index 62f6240..fbab9a5 100644 --- a/library/mac/src/commonMain/kotlin/org/kotlincrypto/core/mac/internal/-CommonPlatform.kt +++ b/library/mac/src/commonMain/kotlin/org/kotlincrypto/core/mac/internal/-CommonPlatform.kt @@ -40,15 +40,15 @@ internal inline fun Mac.commonToString(): String { internal inline fun ByteArray.commonCheckArgs( offset: Int, len: Int, - onShortInput: () -> Exception = { IllegalArgumentException("Input too short") }, - onOutOfBounds: (message: String) -> Exception = { message -> IndexOutOfBoundsException(message) }, + onShortInput: (message: String) -> Exception = ::IllegalArgumentException, + onOutOfBounds: (message: String) -> Exception = ::IndexOutOfBoundsException, ) { contract { callsInPlace(onShortInput, InvocationKind.AT_MOST_ONCE) callsInPlace(onOutOfBounds, InvocationKind.AT_MOST_ONCE) } - if (size - offset < len) throw onShortInput() + if (size - offset < len) throw onShortInput("Too Short. size[$size] - offset[$offset] < len[$len]") if (offset < 0) throw onOutOfBounds("offset[$offset] < 0") if (len < 0) throw onOutOfBounds("len[$len] < 0") if (offset > size - len) throw onOutOfBounds("offset[$offset] > size[$size] - len[$len]") @@ -87,9 +87,7 @@ internal inline fun Mac.commonDoFinalInto( } val len = macLength() - dest.commonCheckArgs(destOffset, len, onShortInput = { - ShortBufferException("Not enough room in dest for $len bytes") - }) + dest.commonCheckArgs(destOffset, len, onShortInput = ::ShortBufferException) engineDoFinalInto(dest, destOffset) if (engineResetOnDoFinal) engineReset() return len diff --git a/library/mac/src/nonJvmMain/kotlin/org/kotlincrypto/core/mac/Mac.kt b/library/mac/src/nonJvmMain/kotlin/org/kotlincrypto/core/mac/Mac.kt index 4bba3fa..4467d42 100644 --- a/library/mac/src/nonJvmMain/kotlin/org/kotlincrypto/core/mac/Mac.kt +++ b/library/mac/src/nonJvmMain/kotlin/org/kotlincrypto/core/mac/Mac.kt @@ -101,7 +101,7 @@ public actual abstract class Mac: Algorithm, Copyable, Resettable, Updatabl } // See Updatable interface documentation public actual final override fun update(input: ByteArray, offset: Int, len: Int) { - input.commonCheckArgs(offset, len, onOutOfBounds = { message -> IllegalArgumentException(message) }) + input.commonCheckArgs(offset, len, onOutOfBounds = ::IllegalArgumentException) engine.update(input, offset, len) }