File tree Expand file tree Collapse file tree 3 files changed +52
-6
lines changed
nym-vpn-android/core/src/main/java/net/nymtech/vpn/util Expand file tree Collapse file tree 3 files changed +52
-6
lines changed Original file line number Diff line number Diff line change 1+ package net.nymtech.vpn.util
2+
3+ import java.math.BigInteger
4+
5+ object Base58 {
6+ private const val ALPHABET = " 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
7+ private val INDEXES = IntArray (128 ) { - 1 }.also {
8+ for (i in ALPHABET .indices) {
9+ it[ALPHABET [i].code] = i
10+ }
11+ }
12+
13+ fun isValidBase58 (input : String , expectedByteLength : Int = 32): Boolean {
14+ try {
15+ if (input.isEmpty() || input.any { it.code >= 128 || INDEXES [it.code] == - 1 }) {
16+ return false
17+ }
18+
19+ val bytes = decode(input)
20+
21+ return bytes.size == expectedByteLength
22+ } catch (e: Exception ) {
23+ return false
24+ }
25+ }
26+
27+ private fun decode (input : String ): ByteArray {
28+ if (input.isEmpty()) return ByteArray (0 )
29+
30+ val bigInt = input.fold(BigInteger .ZERO ) { acc, char ->
31+ val index = INDEXES [char.code]
32+ if (index == - 1 ) throw IllegalArgumentException (" Invalid Base58 character: $char " )
33+ acc.multiply(BigInteger .valueOf(58 )).add(BigInteger .valueOf(index.toLong()))
34+ }
35+
36+ val bytes = bigInt.toByteArray()
37+
38+ val leadingZeros = input.takeWhile { it == ' 1' }.length
39+ return if (bytes[0 ].toInt() == 0 && bytes.size > 1 ) {
40+ ByteArray (leadingZeros) + bytes.drop(1 ).toByteArray()
41+ } else {
42+ ByteArray (leadingZeros) + bytes
43+ }
44+ }
45+ }
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ internal class NotificationManager private constructor(val context: Context) {
2020 private const val VPN_CHANNEL_ID = " VpnForegroundChannel"
2121 }
2222
23- fun createNotificationChannel () {
23+ private fun createNotificationChannel () {
2424 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
2525 // Create the NotificationChannel.
2626 val name = context.getString(R .string.channel_name)
Original file line number Diff line number Diff line change 11package net.nymtech.vpn.util.extensions
22
33import net.nymtech.vpn.backend.Tunnel
4+ import net.nymtech.vpn.util.Base58
45import nym_vpn_lib.EntryPoint
56import nym_vpn_lib.ExitPoint
67import nym_vpn_lib.TunnelEvent
@@ -39,16 +40,16 @@ fun String.asEntryPoint(): EntryPoint {
3940 this == " random" -> EntryPoint .Random
4041 this == " randomlowlatency" -> EntryPoint .RandomLowLatency
4142 length == 2 -> EntryPoint .Location (this .uppercase())
42- length == 44 || length == 43 -> EntryPoint .Gateway (this )
43+ Base58 .isValidBase58( this , 32 ) -> EntryPoint .Gateway (this )
4344 else -> EntryPoint .Random
4445 }
4546}
4647
4748fun String.asExitPoint (): ExitPoint {
48- return when ( this .length) {
49- 2 -> ExitPoint .Location (this .uppercase())
50- 134 -> ExitPoint .Address (this )
51- 44 , 43 -> ExitPoint .Gateway (this )
49+ return when {
50+ length == 2 -> ExitPoint .Location (this .uppercase())
51+ length == 134 -> ExitPoint .Address (this )
52+ Base58 .isValidBase58( this , 32 ) -> ExitPoint .Gateway (this )
5253 else -> throw IllegalArgumentException (" Invalid exit id" )
5354 }
5455}
You can’t perform that action at this time.
0 commit comments