-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
98 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 10 additions & 15 deletions
25
core/common/src/main/java/com/looker/core/common/extension/File.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,21 @@ | ||
package com.looker.core.common.extension | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ensureActive | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.coroutines.yield | ||
import kotlinx.coroutines.* | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.io.InputStream | ||
|
||
val File.size: Long? | ||
get() = if (exists()) length().takeIf { it > 0L } else null | ||
|
||
suspend fun File.readFrom(input: InputStream) = withContext(Dispatchers.IO) { | ||
suspend infix fun InputStream.writeTo(file: File) = withContext(Dispatchers.IO) { | ||
val buffer = ByteArray(DEFAULT_BUFFER_SIZE) | ||
var bytesRead = input.read(buffer) | ||
val output = FileOutputStream(this@readFrom) | ||
while (bytesRead != -1) { | ||
ensureActive() | ||
output.write(buffer, 0, bytesRead) | ||
yield() | ||
bytesRead = input.read(buffer) | ||
var bytesRead = read(buffer) | ||
file.outputStream().use { output -> | ||
while (bytesRead != -1) { | ||
ensureActive() | ||
output.write(buffer, 0, bytesRead) | ||
bytesRead = read(buffer) | ||
} | ||
output.flush() | ||
} | ||
output.flush() | ||
output.close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
core/common/src/main/java/com/looker/core/common/signature/HashChecker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.looker.core.common.signature | ||
|
||
import com.looker.core.common.extension.exceptCancellation | ||
import com.looker.core.common.hex | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ensureActive | ||
import kotlinx.coroutines.withContext | ||
import java.io.File | ||
import java.security.MessageDigest | ||
|
||
suspend fun File.verifyHash(hash: Hash): Boolean = withContext(Dispatchers.IO) { | ||
try { | ||
val digest = MessageDigest | ||
.getInstance(hash.name) | ||
if (length() < Int.MAX_VALUE) { | ||
return@withContext digest | ||
.digest(readBytes()) | ||
.hex() | ||
.equals(hash.hash, ignoreCase = true) | ||
} | ||
val buffer = ByteArray(DEFAULT_BUFFER_SIZE) | ||
inputStream().use { input -> | ||
var bytesRead = input.read(buffer) | ||
while (bytesRead >= 0) { | ||
ensureActive() | ||
digest.update(buffer, 0, bytesRead) | ||
bytesRead = input.read(buffer) | ||
} | ||
digest | ||
.digest() | ||
.hex() | ||
.equals(hash.hash, ignoreCase = true) | ||
} | ||
} catch (e: Exception) { | ||
e.exceptCancellation() | ||
false | ||
} | ||
} | ||
|
||
sealed class Hash(val name: String, val hash: String) | ||
|
||
data class SHA256(val value: String) : Hash(name = "sha256", hash = value) { | ||
init { | ||
require(value.length == 64) | ||
} | ||
} | ||
|
||
data class MD5(val value: String) : Hash(name = "md5", hash = value) { | ||
init { | ||
require(value.length == 32) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters