Skip to content

Commit 0610fb3

Browse files
committed
Extract hash func into utils
Going to use this in tests as well.
1 parent fe3b9bf commit 0610fb3

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

src/main/kotlin/com/coder/gateway/sdk/CoderCLIManager.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.coder.gateway.util.OS
77
import com.coder.gateway.util.getArch
88
import com.coder.gateway.util.getOS
99
import com.coder.gateway.util.safeHost
10+
import com.coder.gateway.util.sha1
1011
import com.coder.gateway.util.toURL
1112
import com.coder.gateway.util.withPath
1213
import com.coder.gateway.views.steps.CoderWorkspacesStepView
@@ -15,7 +16,6 @@ import com.google.gson.JsonSyntaxException
1516
import com.intellij.openapi.diagnostic.Logger
1617
import com.intellij.openapi.progress.ProgressIndicator
1718
import org.zeroturnaround.exec.ProcessExecutor
18-
import java.io.BufferedInputStream
1919
import java.io.FileInputStream
2020
import java.io.FileNotFoundException
2121
import java.net.ConnectException
@@ -25,11 +25,8 @@ import java.nio.file.Files
2525
import java.nio.file.Path
2626
import java.nio.file.Paths
2727
import java.nio.file.StandardCopyOption
28-
import java.security.DigestInputStream
29-
import java.security.MessageDigest
3028
import java.util.zip.GZIPInputStream
3129
import javax.net.ssl.HttpsURLConnection
32-
import javax.xml.bind.annotation.adapters.HexBinaryAdapter
3330

3431

3532
/**
@@ -160,17 +157,9 @@ class CoderCLIManager @JvmOverloads constructor(
160157
/**
161158
* Return the entity tag for the binary on disk, if any.
162159
*/
163-
@Suppress("ControlFlowWithEmptyBody")
164160
private fun getBinaryETag(): String? {
165161
return try {
166-
val md = MessageDigest.getInstance("SHA-1")
167-
val fis = FileInputStream(localBinaryPath.toFile())
168-
val dis = DigestInputStream(BufferedInputStream(fis), md)
169-
fis.use {
170-
while (dis.read() != -1) {
171-
}
172-
}
173-
HexBinaryAdapter().marshal(md.digest()).lowercase()
162+
sha1(FileInputStream(localBinaryPath.toFile()))
174163
} catch (e: FileNotFoundException) {
175164
null
176165
} catch (e: Exception) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.coder.gateway.util
2+
3+
import java.io.BufferedInputStream
4+
import java.io.InputStream
5+
import java.security.DigestInputStream
6+
import java.security.MessageDigest
7+
8+
fun ByteArray.toHex() = joinToString(separator = "") { byte -> "%02x".format(byte) }
9+
10+
/**
11+
* Return the SHA-1 for the provided stream.
12+
*/
13+
@Suppress("ControlFlowWithEmptyBody")
14+
fun sha1(stream: InputStream): String {
15+
val md = MessageDigest.getInstance("SHA-1")
16+
val dis = DigestInputStream(BufferedInputStream(stream), md)
17+
stream.use {
18+
while (dis.read() != -1) {
19+
}
20+
}
21+
return md.digest().toHex()
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.coder.gateway.util
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
6+
internal class HashTest {
7+
@Test
8+
fun testToHex() {
9+
val tests = mapOf(
10+
"foobar" to "8843d7f92416211de9ebb963ff4ce28125932878",
11+
"test" to "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3",
12+
)
13+
tests.forEach {
14+
assertEquals(it.value, sha1(it.key.byteInputStream()))
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)