8000 Just need to check the last byte. by robxyy · Pull Request #22 · diglol/id · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Just need to check the last byte. #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions id/src/commonMain/kotlin/diglol/id/Id.kt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ class Id private constructor(private val raw: ByteArray) : Comparable<Id> {
}
val raw = ByteArray(rawSize)
raw[12] = ((decode[srcInts[19]] shl 4) or (decode[srcInts[20]] shr 1)).toByte()
if (encode[(raw[12].toInt() and 0xff shl 1) and 0x1f] != src[20]) {
return empty
}
raw[11] =
((decode[srcInts[17]] shl 6) or (decode[srcInts[18]] shl 1) or (decode[srcInts[19]] shr 4)).toByte()
raw[10] = ((decode[srcInts[16]] shl 3) or (decode[srcInts[17]] shr 2)).toByte()
Expand All @@ -157,16 +160,7 @@ class Id private constructor(private val raw: ByteArray) : Comparable<Id> {
raw[1] =
((decode[srcInts[1]] shl 6) or (decode[srcInts[2]] shl 1) or (decode[srcInts[3]] shr 4)).toByte()
raw[0] = ((decode[srcInts[0]] shl 3) or (decode[srcInts[1]] shr 2)).toByte()

// check
val checkRawInts = raw.copyOfRange(10, 13).map { v -> v.toInt() and 0xff } // big endian
val check = byteArrayOf(
encode[(checkRawInts[1] shr 6) and 0x1f or (checkRawInts[0] shl 2) and 0x1f],
encode[(checkRawInts[1] shr 1) and 0x1f],
encode[(checkRawInts[2] shr 4) and 0x1f or (checkRawInts[1] shl 4) and 0x1f],
encode[(checkRawInts[2] shl 1) and 0x1f]
)
return if (check.contentEquals(src.copyOfRange(17, 21))) Id(raw) else empty
return Id(raw)
}

private fun generateRaw(epochSeconds: Long): ByteArray {
Expand Down
8 changes: 8 additions & 0 deletions id/src/commonTest/kotlin/diglol/id/IdTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertFails
import kotlin.test.assertNotEquals
import kotlin.test.assertTrue

class IdTest {
Expand Down Expand Up @@ -40,6 +41,13 @@ class IdTest {
assertEquals(epochSeconds, id.time)
}

@Test
fun padding() {
val id1 = "016ohoarc3q8dp1884ms1".decodeToId() // invalid
val id2 = "016ohoarc3q8dp1884ms0".decodeToId()
assertNotEquals(id1, id2)
}

@Test
fun toId() {
val bytes = byteArrayOf(
Expand Down
0