diff options
| author | Linnea Gräf <nea@nea.moe> | 2023-12-10 16:54:57 +0100 |
|---|---|---|
| committer | Linnea Gräf <nea@nea.moe> | 2023-12-10 16:54:57 +0100 |
| commit | 0936f6272c7d1e41b7e9550bb17fe84af5d976c3 (patch) | |
| tree | 0c41488910659eefb31a6231dce0f72f29ca75e3 /plugin/src/main/kotlin/moe/nea/archenemy/util | |
| parent | 17f0faaccb0db837a64b6ce7cecff7f8ab048410 (diff) | |
| download | archenemy-0936f6272c7d1e41b7e9550bb17fe84af5d976c3.tar.gz archenemy-0936f6272c7d1e41b7e9550bb17fe84af5d976c3.tar.bz2 archenemy-0936f6272c7d1e41b7e9550bb17fe84af5d976c3.zip | |
Move utilities around
Diffstat (limited to 'plugin/src/main/kotlin/moe/nea/archenemy/util')
| -rw-r--r-- | plugin/src/main/kotlin/moe/nea/archenemy/util/DigestUtils.kt | 14 | ||||
| -rw-r--r-- | plugin/src/main/kotlin/moe/nea/archenemy/util/DownloadUtils.kt | 52 |
2 files changed, 66 insertions, 0 deletions
diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/util/DigestUtils.kt b/plugin/src/main/kotlin/moe/nea/archenemy/util/DigestUtils.kt new file mode 100644 index 0000000..4f50000 --- /dev/null +++ b/plugin/src/main/kotlin/moe/nea/archenemy/util/DigestUtils.kt @@ -0,0 +1,14 @@ +package moe.nea.archenemy.util + +import java.security.MessageDigest + +fun MessageDigest.updateField(text: String, value: String) { + this.update(text) + this.update(":") + this.update(value) + this.update(";") +} + +fun MessageDigest.update(text: String) { + this.update(text.encodeToByteArray()) +}
\ No newline at end of file diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/util/DownloadUtils.kt b/plugin/src/main/kotlin/moe/nea/archenemy/util/DownloadUtils.kt new file mode 100644 index 0000000..cec2c55 --- /dev/null +++ b/plugin/src/main/kotlin/moe/nea/archenemy/util/DownloadUtils.kt @@ -0,0 +1,52 @@ +package moe.nea.archenemy.util + +import java.io.File +import java.io.IOException +import java.net.URL +import java.security.MessageDigest + +object DownloadUtils { + fun bytesToHex(hash: ByteArray): String { + val hexString = StringBuilder(2 * hash.size) + for (i in hash.indices) { + val hex = Integer.toHexString(0xff and hash[i].toInt()) + if (hex.length == 1) { + hexString.append('0') + } + hexString.append(hex) + } + return hexString.toString() + } + + + fun sha1Hash(file: File): String { + if (!file.exists()) return "" + val messageDigest = MessageDigest.getInstance("SHA-1") + + file.inputStream().use { + val r = ByteArray(4096) + while (true) { + val d = it.read(r) + if (d < 0) break + messageDigest.update(r, 0, d) + } + } + return bytesToHex(messageDigest.digest()) + } + + fun areHashesEqual(a: String, b: String): Boolean { + return b.equals(a, ignoreCase = true) + } + + fun downloadFile(source: URL, sha1: String, targetFile: File) { + targetFile.parentFile.mkdirs() + if (areHashesEqual(sha1Hash(targetFile), sha1)) return + source.openStream().use { inp -> + targetFile.outputStream().use { out -> + inp.copyTo(out) + } + } + if (!areHashesEqual(sha1Hash(targetFile), sha1)) + throw IOException("$targetFile should hash to $sha1, but does not") + } +}
\ No newline at end of file |
