Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / registration / RegEncoding.kt
RegEncoding.kt68 lines · 2.7 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp.registration |
| 2 | |
| 3 | import android.util.Base64 |
| 4 | import java.nio.ByteBuffer |
| 5 | import java.util.UUID |
| 6 | |
| 7 | /** |
| 8 | * Byte/string encoders that mirror WhatsApp's `C34244EyE` registration request builder exactly, so |
| 9 | * `/v2/ endpoints` params are byte-for-byte what the server expects (avoids `bad_param`). |
| 10 | * |
| 11 | * Verified against the pinned APK: |
| 12 | * - [b64Url] = `DIj.A0w` = `Base64.encodeToString(b, URL_SAFE|NO_WRAP|NO_PADDING)` (flag 11). |
| 13 | * Used by `A03`(expid/access_session_id after UUID→bytes) and `A04`(the E2E key bundle). |
| 14 | * - [percentEncode] = `EPJ.A00` = RFC-3986 percent-encoding keeping unreserved `A-Za-z0-9-._~`. |
| 15 | * Used by `A05`(id, backup_token); the result is stored PRE-ENCODED and must not be |
| 16 | * URL-encoded again at query-build time. |
| 17 | * - [uuidToBytes] = the 16-byte big-endian form `A03` derives from a UUID string. |
| 18 | */ |
| 19 | object RegEncoding { |
| 20 | |
| 21 | private const val UUID_BYTES = 16 |
| 22 | private const val BYTE_MASK = 0xFF |
| 23 | private const val NIBBLE_BITS = 4 |
| 24 | private const val NIBBLE_MASK = 0xF |
| 25 | private val DIGIT_RANGE = 0x30..0x39 |
| 26 | private val UPPER_RANGE = 0x41..0x5A |
| 27 | private val LOWER_RANGE = 0x61..0x7A |
| 28 | private const val HYPHEN = 0x2D |
| 29 | private const val DOT = 0x2E |
| 30 | private const val UNDERSCORE = 0x5F |
| 31 | private const val TILDE = 0x7E |
| 32 | |
| 33 | /** URL-safe Base64, no padding, no wrap (Android Base64 flag 11). */ |
| 34 | fun b64Url(bytes: ByteArray): String = |
| 35 | Base64.encodeToString(bytes, Base64.URL_SAFE or Base64.NO_WRAP or Base64.NO_PADDING) |
| 36 | |
| 37 | /** 16 big-endian bytes of a UUID (matches C34244EyE.A03). */ |
| 38 | fun uuidToBytes(uuid: String): ByteArray { |
| 39 | val u = UUID.fromString(uuid) |
| 40 | return ByteBuffer.allocate(UUID_BYTES) |
| 41 | .putLong(u.mostSignificantBits) |
| 42 | .putLong(u.leastSignificantBits) |
| 43 | .array() |
| 44 | } |
| 45 | |
| 46 | /** RFC-3986 percent-encoding of raw bytes, unreserved set = A-Za-z0-9-._~ (matches EPJ.A00). */ |
| 47 | fun percentEncode(bytes: ByteArray): String { |
| 48 | val sb = StringBuilder(bytes.size * 3) |
| 49 | for (b in bytes) { |
| 50 | val i = b.toInt() and BYTE_MASK |
| 51 | val c = i.toChar() |
| 52 | val unreserved = (i in DIGIT_RANGE) || // 0-9 |
| 53 | (i in UPPER_RANGE) || // A-Z |
| 54 | (i in LOWER_RANGE) || // a-z |
| 55 | i == HYPHEN || i == DOT || i == UNDERSCORE || i == TILDE // - . _ ~ |
| 56 | if (unreserved) { |
| 57 | sb.append(c) |
| 58 | } else { |
| 59 | sb.append('%') |
| 60 | sb.append(HEX[i shr NIBBLE_BITS]) |
| 61 | sb.append(HEX[i and NIBBLE_MASK]) |
| 62 | } |
| 63 | } |
| 64 | return sb.toString() |
| 65 | } |
| 66 | |
| 67 | private val HEX = "0123456789ABCDEF".toCharArray() |
| 68 | } |