Skip to content

Commit 270b949

Browse files
committed
impl: run the signature verification on the IO thread
Signature verification some operations that are cpu bound that are light, like decoding and decompressing signature data, some medium CPU intensive operations like the cryptographic verifications and a couple of blocking IO operations: - reading the cli file - reading the signature file - reading the public key file These last should run on the IO thread to not block the main thread from drawing the screen. The cpu bound operation should run on the default thread.
1 parent fbe68de commit 270b949

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/main/kotlin/com/coder/toolbox/cli/gpg/GPGVerifier.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import com.coder.toolbox.cli.gpg.VerificationResult.Failed
55
import com.coder.toolbox.cli.gpg.VerificationResult.Invalid
66
import com.coder.toolbox.cli.gpg.VerificationResult.SignatureNotFound
77
import com.coder.toolbox.cli.gpg.VerificationResult.Valid
8+
import kotlinx.coroutines.Dispatchers
9+
import kotlinx.coroutines.withContext
810
import org.bouncycastle.bcpg.ArmoredInputStream
911
import org.bouncycastle.openpgp.PGPException
1012
import org.bouncycastle.openpgp.PGPPublicKeyRing
@@ -23,7 +25,7 @@ class GPGVerifier(
2325
private val context: CoderToolboxContext,
2426
) {
2527

26-
fun verifySignature(
28+
suspend fun verifySignature(
2729
cli: Path,
2830
signature: Path,
2931
): VerificationResult {
@@ -33,10 +35,13 @@ class GPGVerifier(
3335
return SignatureNotFound
3436
}
3537

36-
val signatureBytes = Files.readAllBytes(signature)
37-
val cliBytes = Files.readAllBytes(cli)
38+
val (cliBytes, signatureBytes, publicKeyRing) = withContext(Dispatchers.IO) {
39+
val cliBytes = Files.readAllBytes(cli)
40+
val signatureBytes = Files.readAllBytes(signature)
41+
val publicKeyRing = getCoderPublicKeyRing()
3842

39-
val publicKeyRing = getCoderPublicKeyRing()
43+
Triple(cliBytes, signatureBytes, publicKeyRing)
44+
}
4045
return verifyDetachedSignature(
4146
cliBytes = cliBytes,
4247
signatureBytes = signatureBytes,

0 commit comments

Comments
 (0)