forked from readium/kotlin-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
2786fb4
commit 975e8af
Showing
13 changed files
with
128 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
29 changes: 29 additions & 0 deletions
29
readium/streamer/src/main/java/org/readium/r2/streamer/extensions/StringExt.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,29 @@ | ||
package org.readium.r2.streamer.extensions | ||
|
||
import com.mcxiaoke.koi.HASH | ||
import com.mcxiaoke.koi.ext.toHexBytes | ||
import org.readium.r2.shared.extensions.tryOrNull | ||
|
||
/** | ||
* Computes the SHA-1 hash of a string. | ||
*/ | ||
internal fun String.sha1(): String = | ||
HASH.sha1(this) | ||
|
||
/** | ||
* Converts an hexadecimal string (e.g. 8ad5078e) to a byte array. | ||
*/ | ||
internal fun String.toHexByteArray(): ByteArray? { | ||
// Only even-length strings can be converted to an Hex byte array, otherwise it crashes | ||
// with StringIndexOutOfBoundsException. | ||
if (isEmpty() || !hasEvenLength() || !isHexadecimal()) { | ||
return null | ||
} | ||
return tryOrNull { toHexBytes() } | ||
} | ||
|
||
private fun String.hasEvenLength(): Boolean = | ||
length % 2 == 0 | ||
|
||
private fun String.isHexadecimal(): Boolean = | ||
all { it in '0'..'9' || it in 'a'..'f' || it in 'A'..'F' } |
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
26 changes: 26 additions & 0 deletions
26
readium/streamer/src/test/java/org/readium/r2/streamer/extensions/StringExtTest.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,26 @@ | ||
package org.readium.r2.streamer.extensions | ||
|
||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertNull | ||
import org.junit.Test | ||
|
||
class StringExtTest { | ||
|
||
@Test | ||
fun `convert an hexadecimal string to a byte array`() { | ||
assertNull("".toHexByteArray()) | ||
// Forbids odd-length strings. | ||
assertNull("8".toHexByteArray()) | ||
assertNull("8ad".toHexByteArray()) | ||
// Forbids character outside 0-f range. | ||
assertNull("8y".toHexByteArray()) | ||
|
||
assertContentEquals(byteArrayOf(0x8a), "8a".toHexByteArray()) | ||
assertContentEquals(byteArrayOf(0x8a), "8A".toHexByteArray()) | ||
assertContentEquals(byteArrayOf(0x8a, 0xd5, 0x07, 0x8e), "8ad5078e".toHexByteArray()) | ||
} | ||
|
||
private fun byteArrayOf(vararg bytes: Int): ByteArray { | ||
return bytes.map { it.toByte() }.toByteArray() | ||
} | ||
} |
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