diff --git a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/network/Fingerprint.kt b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/network/Fingerprint.kt index 4e3ae215..0e112b8e 100644 --- a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/network/Fingerprint.kt +++ b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/network/Fingerprint.kt @@ -13,7 +13,27 @@ class Fingerprint(fingerprint: String) : Comparable { } override fun compareTo(other: Fingerprint): Int { - return fingerprint.compareTo(other.fingerprint) + // The strings are equal + if (this == other) return 0 + + // Compare char by char. + // This ordering has the property that Hex-encoded Fingerprints are + // ordered in the same way as in sequoia-wot. + // (As they would be, if they were byte arrays). + // + // For other formats of "Fingerprint", the specific ordering doesn't matter to us. + // + // Following the sequoia-wot ordering means that we get the exact same output for the test suite. + val a = this.fingerprint.toCharArray() + val b = other.fingerprint.toCharArray() + a.zip(b).forEach { (x, y) -> + if (x != y) return x.compareTo(y) + } + + // If prefix is the same, but one string is longer, we consider the longer one "bigger" + if (a.size != b.size) return a.size.compareTo(b.size) + + throw RuntimeException("This should be unreachable") } override fun equals(other: Any?): Boolean {