mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 16:25:22 +00:00
refactor frombinarydata
This commit is contained in:
@@ -10,8 +10,6 @@ import com.bitchat.android.protocol.BitchatPacket
|
|||||||
import com.bitchat.android.protocol.MessageType
|
import com.bitchat.android.protocol.MessageType
|
||||||
import com.bitchat.android.util.toHexString
|
import com.bitchat.android.util.toHexString
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import org.json.JSONObject
|
|
||||||
import java.security.MessageDigest
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
|
|
||||||
@@ -121,7 +119,7 @@ class MessageHandler(private val myPeerID: String) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Parse the identity announcement
|
// Parse the identity announcement
|
||||||
val announcement = parseNoiseIdentityAnnouncement(packet.payload)
|
val announcement = NoiseIdentityAnnouncement.fromBinaryData(packet.payload)
|
||||||
if (announcement == null) {
|
if (announcement == null) {
|
||||||
Log.w(TAG, "Failed to parse Noise identity announcement from $peerID")
|
Log.w(TAG, "Failed to parse Noise identity announcement from $peerID")
|
||||||
return
|
return
|
||||||
@@ -406,45 +404,6 @@ class MessageHandler(private val myPeerID: String) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse Noise identity announcement from payload
|
|
||||||
*/
|
|
||||||
private fun parseNoiseIdentityAnnouncement(payload: ByteArray): NoiseIdentityAnnouncement? {
|
|
||||||
return try {
|
|
||||||
val jsonString = String(payload, Charsets.UTF_8)
|
|
||||||
val json = JSONObject(jsonString)
|
|
||||||
|
|
||||||
val peerID = json.getString("peerID")
|
|
||||||
val nickname = json.getString("nickname")
|
|
||||||
val publicKeyBase64 = json.getString("publicKey")
|
|
||||||
val timestampMs = json.getLong("timestamp")
|
|
||||||
val signatureBase64 = json.getString("signature")
|
|
||||||
val previousPeerID = if (json.has("previousPeerID")) json.getString("previousPeerID") else null
|
|
||||||
|
|
||||||
// Decode base64 fields
|
|
||||||
val publicKey = android.util.Base64.decode(publicKeyBase64, android.util.Base64.DEFAULT)
|
|
||||||
val signature = android.util.Base64.decode(signatureBase64, android.util.Base64.DEFAULT)
|
|
||||||
|
|
||||||
// Calculate fingerprint from public key
|
|
||||||
val digest = MessageDigest.getInstance("SHA-256")
|
|
||||||
val hash = digest.digest(publicKey)
|
|
||||||
val fingerprint = hash.joinToString("") { "%02x".format(it) }
|
|
||||||
|
|
||||||
NoiseIdentityAnnouncement(
|
|
||||||
peerID = peerID,
|
|
||||||
nickname = nickname,
|
|
||||||
publicKey = publicKey,
|
|
||||||
timestamp = Date(timestampMs),
|
|
||||||
signature = signature,
|
|
||||||
fingerprint = fingerprint,
|
|
||||||
previousPeerID = previousPeerID
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.e(TAG, "Failed to parse Noise identity announcement: ${e.message}")
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert hex string peer ID to binary data (8 bytes) - same as iOS implementation
|
* Convert hex string peer ID to binary data (8 bytes) - same as iOS implementation
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package com.bitchat.android.model
|
package com.bitchat.android.model
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import org.json.JSONObject
|
||||||
|
import java.security.MessageDigest
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,4 +44,47 @@ data class NoiseIdentityAnnouncement(
|
|||||||
result = 31 * result + (previousPeerID?.hashCode() ?: 0)
|
result = 31 * result + (previousPeerID?.hashCode() ?: 0)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val TAG = "NoiseIdentityAnnouncement"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse Noise identity announcement from binary payload
|
||||||
|
*/
|
||||||
|
fun fromBinaryData(payload: ByteArray): NoiseIdentityAnnouncement? {
|
||||||
|
return try {
|
||||||
|
val jsonString = String(payload, Charsets.UTF_8)
|
||||||
|
val json = JSONObject(jsonString)
|
||||||
|
|
||||||
|
val peerID = json.getString("peerID")
|
||||||
|
val nickname = json.getString("nickname")
|
||||||
|
val publicKeyBase64 = json.getString("publicKey")
|
||||||
|
val timestampMs = json.getLong("timestamp")
|
||||||
|
val signatureBase64 = json.getString("signature")
|
||||||
|
val previousPeerID = if (json.has("previousPeerID")) json.getString("previousPeerID") else null
|
||||||
|
|
||||||
|
// Decode base64 fields
|
||||||
|
val publicKey = android.util.Base64.decode(publicKeyBase64, android.util.Base64.DEFAULT)
|
||||||
|
val signature = android.util.Base64.decode(signatureBase64, android.util.Base64.DEFAULT)
|
||||||
|
|
||||||
|
// Calculate fingerprint from public key
|
||||||
|
val digest = MessageDigest.getInstance("SHA-256")
|
||||||
|
val hash = digest.digest(publicKey)
|
||||||
|
val fingerprint = hash.joinToString("") { "%02x".format(it) }
|
||||||
|
|
||||||
|
NoiseIdentityAnnouncement(
|
||||||
|
peerID = peerID,
|
||||||
|
nickname = nickname,
|
||||||
|
publicKey = publicKey,
|
||||||
|
timestamp = Date(timestampMs),
|
||||||
|
signature = signature,
|
||||||
|
fingerprint = fingerprint,
|
||||||
|
previousPeerID = previousPeerID
|
||||||
|
)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "Failed to parse Noise identity announcement: ${e.message}")
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user