Skip to content

Commit

Permalink
Make HMAC key non-nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
ileasile committed Oct 20, 2024
1 parent 860d874 commit 4bca1c3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ package org.jetbrains.kotlinx.jupyter.protocol
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec

class HMAC(algorithm: String, key: String?) {
private val mac = if (key?.isNotBlank() == true) Mac.getInstance(algorithm) else null

init {
mac?.init(SecretKeySpec(key!!.toByteArray(), algorithm))
}
class HMAC(algorithm: String, key: String) {
private val mac: Mac =
Mac.getInstance(algorithm).apply {
init(SecretKeySpec(key.toByteArray(), algorithm))
}

@Synchronized
operator fun invoke(data: Iterable<ByteArray>): String? =
mac?.let { mac ->
data.forEach { mac.update(it) }
mac.doFinal().toHexString()
}
operator fun invoke(data: Iterable<ByteArray>): String {
data.forEach { mac.update(it) }
return mac.doFinal().toHexString()
}

operator fun invoke(vararg data: ByteArray): String? = invoke(data.asIterable())
operator fun invoke(vararg data: ByteArray): String = invoke(data.asIterable())
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class SocketWrapper(
prop ->
prop.get(msg)?.let { MessageFormat.encodeToString(it) }?.toByteArray() ?: emptyJsonObjectStringBytes
}
sendMore(hmac(signableMsg) ?: "")
sendMore(hmac(signableMsg))
for (i in 0 until (signableMsg.size - 1)) {
sendMore(signableMsg[i])
}
Expand Down Expand Up @@ -121,7 +121,7 @@ class SocketWrapper(
val content = recv()
val calculatedSig = hmac(header, parentHeader, metadata, content)

if (calculatedSig != null && sig != calculatedSig) {
if (sig != calculatedSig) {
throw SignatureException("Invalid signature: expected $calculatedSig, received $sig - $ids")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract class KernelServerTestsBase(protected val runServerInSeparateProcess: B
protected val kernelConfig =
createKotlinKernelConfig(
ports = createRandomKernelPorts(),
signatureKey = "",
signatureKey = "abc",
scriptClasspath = classpath,
homeDir = File(""),
)
Expand Down

0 comments on commit 4bca1c3

Please sign in to comment.