From e5fb60ebd9e5114ed07596b3e4bc125c4654c8f7 Mon Sep 17 00:00:00 2001 From: Guillermo Orellana <172084+wiyarmir@users.noreply.github.com> Date: Fri, 8 Sep 2023 13:09:55 +0200 Subject: [PATCH 1/2] make sha256 thread safe moving the working arrays inside the function call prevents threading issues and doesn't require synchronisation --- .../kotlin/org/komputing/khash/sha256/Sha256.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/Sha256.kt b/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/Sha256.kt index 15a9561..47f71b2 100644 --- a/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/Sha256.kt +++ b/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/Sha256.kt @@ -28,11 +28,6 @@ public object Sha256 { 0x510e527f, -0x64fa9774, 0x1f83d9ab, 0x5be0cd19 ) - // Working arrays - private val W = IntArray(64) - private val H = IntArray(8) - private val TEMP = IntArray(8) - /** * Hashes the given message with SHA-256 and returns the digest. * @@ -40,6 +35,11 @@ public object Sha256 { * @return The digest's bytes. */ public fun digest(message: ByteArray): ByteArray { + // Working arrays + private val W = IntArray(64) + private val H = IntArray(8) + private val TEMP = IntArray(8) + // Let H = H0 H0.copy(0, H, 0, H0.size) From 20c3afc786245537c451e65d956d30ded166b52f Mon Sep 17 00:00:00 2001 From: Guillermo Orellana <172084+wiyarmir@users.noreply.github.com> Date: Fri, 8 Sep 2023 13:30:24 +0200 Subject: [PATCH 2/2] Update Sha256.kt --- .../commonMain/kotlin/org/komputing/khash/sha256/Sha256.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/Sha256.kt b/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/Sha256.kt index 47f71b2..295b21b 100644 --- a/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/Sha256.kt +++ b/sha256/src/commonMain/kotlin/org/komputing/khash/sha256/Sha256.kt @@ -36,9 +36,9 @@ public object Sha256 { */ public fun digest(message: ByteArray): ByteArray { // Working arrays - private val W = IntArray(64) - private val H = IntArray(8) - private val TEMP = IntArray(8) + val W = IntArray(64) + val H = IntArray(8) + val TEMP = IntArray(8) // Let H = H0 H0.copy(0,