-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3c8d1d
commit 264424d
Showing
8 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
composeApp/src/commonMain/kotlin/com/clipevery/encrypt/SignalProtocol.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.clipevery.encrypt | ||
|
||
import org.whispersystems.libsignal.IdentityKeyPair | ||
import org.whispersystems.libsignal.state.IdentityKeyStore | ||
import org.whispersystems.libsignal.state.PreKeyRecord | ||
import org.whispersystems.libsignal.state.PreKeyStore | ||
import org.whispersystems.libsignal.state.SessionStore | ||
import org.whispersystems.libsignal.state.SignedPreKeyRecord | ||
import org.whispersystems.libsignal.state.SignedPreKeyStore | ||
import org.whispersystems.libsignal.state.impl.InMemoryIdentityKeyStore | ||
import org.whispersystems.libsignal.state.impl.InMemoryPreKeyStore | ||
import org.whispersystems.libsignal.state.impl.InMemorySessionStore | ||
import org.whispersystems.libsignal.state.impl.InMemorySignedPreKeyStore | ||
|
||
interface SignalProtocol { | ||
val identityKeyPair: IdentityKeyPair | ||
|
||
val registrationId: Int | ||
|
||
val preKeys: List<PreKeyRecord> | ||
|
||
val signedPreKey: SignedPreKeyRecord | ||
|
||
val sessionStore: SessionStore | ||
get() = InMemorySessionStore() | ||
|
||
val preKeyStore: PreKeyStore | ||
get() = InMemoryPreKeyStore() | ||
|
||
val signedPreKeyStore: SignedPreKeyStore | ||
get() = InMemorySignedPreKeyStore() | ||
|
||
val identityKeyStore: IdentityKeyStore | ||
get() = InMemoryIdentityKeyStore(identityKeyPair, registrationId) | ||
} |
12 changes: 12 additions & 0 deletions
12
composeApp/src/commonMain/kotlin/com/clipevery/utils/JsonUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.clipevery.utils | ||
|
||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
|
||
inline fun <reified T> readJson(json: String): T { | ||
return Json.decodeFromString<T>(json) | ||
} | ||
|
||
inline fun <reified T : Any> writeJson(obj: T): String { | ||
return Json.encodeToString(obj) | ||
} |
60 changes: 60 additions & 0 deletions
60
composeApp/src/desktopMain/kotlin/com/clipevery/encrypt/DesktopSignalProtocol.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.clipevery.encrypt | ||
|
||
import com.clipevery.utils.readJson | ||
import org.whispersystems.libsignal.IdentityKeyPair | ||
import org.whispersystems.libsignal.state.PreKeyRecord | ||
import org.whispersystems.libsignal.state.SignedPreKeyRecord | ||
import org.whispersystems.libsignal.util.KeyHelper | ||
|
||
class DesktopSignalProtocol(override val identityKeyPair: IdentityKeyPair, | ||
override val registrationId: Int, | ||
override val preKeys: List<PreKeyRecord>, | ||
override val signedPreKey: SignedPreKeyRecord | ||
): SignalProtocol { | ||
|
||
constructor(): this(KeyHelper.generateIdentityKeyPair(), | ||
KeyHelper.generateRegistrationId(false), | ||
KeyHelper.generatePreKeys(0, 5), | ||
KeyHelper.generateSignedPreKey(KeyHelper.generateIdentityKeyPair(), 5)) | ||
} | ||
|
||
|
||
data class StringEncodeSignalProtocol(val identityKeyPairStr: String, | ||
val registrationIdStr: Int, | ||
val preKeysStr: List<String>, | ||
val signedPreKeyStr: String) | ||
|
||
|
||
fun readSignalProtocol(data: String): SignalProtocol { | ||
val stringEncodeSignalProtocol = readJson<StringEncodeSignalProtocol>(data) | ||
|
||
val identityKeyPair = IdentityKeyPair(asciiStringToBytes(stringEncodeSignalProtocol.identityKeyPairStr)) | ||
|
||
val registrationId = stringEncodeSignalProtocol.registrationIdStr | ||
|
||
val preKeys = stringEncodeSignalProtocol.preKeysStr.map { PreKeyRecord(asciiStringToBytes(it)) } | ||
|
||
val signedPreKey = SignedPreKeyRecord(asciiStringToBytes(stringEncodeSignalProtocol.signedPreKeyStr)) | ||
|
||
return DesktopSignalProtocol(identityKeyPair, registrationId, preKeys, signedPreKey) | ||
} | ||
|
||
fun writeSignalProtocol(signalProtocol: SignalProtocol): StringEncodeSignalProtocol { | ||
val identityKeyPairStr = bytesToAsciiString(signalProtocol.identityKeyPair.serialize()) | ||
|
||
val registrationIdStr = signalProtocol.registrationId | ||
|
||
val preKeysStr = signalProtocol.preKeys.map { bytesToAsciiString(it.serialize()) } | ||
|
||
val signedPreKeyStr = bytesToAsciiString(signalProtocol.signedPreKey.serialize()) | ||
|
||
return StringEncodeSignalProtocol(identityKeyPairStr, registrationIdStr, preKeysStr, signedPreKeyStr) | ||
} | ||
|
||
fun bytesToAsciiString(bytes: ByteArray): String { | ||
return bytes.joinToString(separator = "") { it.toInt().toChar().toString() } | ||
} | ||
|
||
fun asciiStringToBytes(str: String): ByteArray { | ||
return str.map { it.code.toByte() }.toByteArray() | ||
} |
31 changes: 31 additions & 0 deletions
31
composeApp/src/desktopMain/kotlin/com/clipevery/encrypt/SessionStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.clipevery.encrypt | ||
|
||
import org.whispersystems.libsignal.SignalProtocolAddress | ||
import org.whispersystems.libsignal.state.SessionRecord | ||
import org.whispersystems.libsignal.state.SessionStore | ||
|
||
class MacSessionStore: SessionStore { | ||
override fun loadSession(address: SignalProtocolAddress?): SessionRecord { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun getSubDeviceSessions(name: String?): MutableList<Int> { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun storeSession(address: SignalProtocolAddress?, record: SessionRecord?) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun containsSession(address: SignalProtocolAddress?): Boolean { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun deleteSession(address: SignalProtocolAddress?) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun deleteAllSessions(name: String?) { | ||
TODO("Not yet implemented") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters