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) {
20
20
private const val VPN_CHANNEL_ID = " VpnForegroundChannel"
21
21
}
22
22
23
- fun createNotificationChannel () {
23
+ private fun createNotificationChannel () {
24
24
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
25
25
// Create the NotificationChannel.
26
26
val name = context.getString(R .string.channel_name)
Original file line number Diff line number Diff line change 1
1
package net.nymtech.vpn.util.extensions
2
2
3
3
import net.nymtech.vpn.backend.Tunnel
4
+ import net.nymtech.vpn.util.Base58
4
5
import nym_vpn_lib.EntryPoint
5
6
import nym_vpn_lib.ExitPoint
6
7
import nym_vpn_lib.TunnelEvent
@@ -39,16 +40,16 @@ fun String.asEntryPoint(): EntryPoint {
39
40
this == " random" -> EntryPoint .Random
40
41
this == " randomlowlatency" -> EntryPoint .RandomLowLatency
41
42
length == 2 -> EntryPoint .Location (this .uppercase())
42
- length == 44 || length == 43 -> EntryPoint .Gateway (this )
43
+ Base58 .isValidBase58( this , 32 ) -> EntryPoint .Gateway (this )
43
44
else -> EntryPoint .Random
44
45
}
45
46
}
46
47
47
48
fun 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 )
52
53
else -> throw IllegalArgumentException (" Invalid exit id" )
53
54
}
54
55
}
You can’t perform that action at this time.
0 commit comments