mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 11:05:20 +00:00
Plumtree sync (#393)
* wip plumtree * sync works * fix logging * ttl to 0 * fix send packet to one peer * spec * wip GCS instead of bloom * remove bloom filter remainders * clean * prune old announcements * remove announcements from sync after LEAVE * sync after 1 second * pruning * track own announcement and prune messages without announcements * fix pruning * getGcsMaxFilterBytes default value 400 bytes * parameters
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.bitchat.android.sync
|
||||
|
||||
import com.bitchat.android.protocol.BitchatPacket
|
||||
import java.security.MessageDigest
|
||||
|
||||
/**
|
||||
* Deterministic packet ID helper for sync purposes.
|
||||
* Uses SHA-256 over a canonical subset of packet fields:
|
||||
* [type | senderID | timestamp | payload] to generate a stable ID.
|
||||
* Returns a 16-byte (128-bit) truncated hash for compactness.
|
||||
*/
|
||||
object PacketIdUtil {
|
||||
fun computeIdBytes(packet: BitchatPacket): ByteArray {
|
||||
val md = MessageDigest.getInstance("SHA-256")
|
||||
md.update(packet.type.toByte())
|
||||
md.update(packet.senderID)
|
||||
// Timestamp as 8 bytes big-endian
|
||||
val ts = packet.timestamp.toLong()
|
||||
for (i in 7 downTo 0) {
|
||||
md.update(((ts ushr (i * 8)) and 0xFF).toByte())
|
||||
}
|
||||
md.update(packet.payload)
|
||||
val digest = md.digest()
|
||||
return digest.copyOf(16) // 128-bit ID
|
||||
}
|
||||
|
||||
fun computeIdHex(packet: BitchatPacket): String {
|
||||
return computeIdBytes(packet).joinToString("") { b -> "%02x".format(b) }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user