|
|
@ -13,8 +13,12 @@ import android.text.SpannableStringBuilder
|
|
|
|
import android.text.method.LinkMovementMethod
|
|
|
|
import android.text.method.LinkMovementMethod
|
|
|
|
import android.text.style.ClickableSpan
|
|
|
|
import android.text.style.ClickableSpan
|
|
|
|
import android.text.style.StyleSpan
|
|
|
|
import android.text.style.StyleSpan
|
|
|
|
|
|
|
|
import android.util.Log
|
|
|
|
import android.view.View
|
|
|
|
import android.view.View
|
|
|
|
import android.widget.Toast
|
|
|
|
import android.widget.Toast
|
|
|
|
|
|
|
|
import com.goterl.lazycode.lazysodium.LazySodiumAndroid
|
|
|
|
|
|
|
|
import com.goterl.lazycode.lazysodium.SodiumAndroid
|
|
|
|
|
|
|
|
import com.goterl.lazycode.lazysodium.utils.KeyPair
|
|
|
|
import kotlinx.android.synthetic.main.activity_register.*
|
|
|
|
import kotlinx.android.synthetic.main.activity_register.*
|
|
|
|
import network.loki.messenger.R
|
|
|
|
import network.loki.messenger.R
|
|
|
|
import org.thoughtcrime.securesms.BaseActionBarActivity
|
|
|
|
import org.thoughtcrime.securesms.BaseActionBarActivity
|
|
|
@ -28,16 +32,15 @@ import org.thoughtcrime.securesms.util.Base64
|
|
|
|
import org.thoughtcrime.securesms.util.Hex
|
|
|
|
import org.thoughtcrime.securesms.util.Hex
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
|
|
|
import org.whispersystems.curve25519.Curve25519
|
|
|
|
import org.whispersystems.curve25519.Curve25519
|
|
|
|
import org.whispersystems.libsignal.ecc.Curve
|
|
|
|
import org.whispersystems.libsignal.ecc.*
|
|
|
|
import org.whispersystems.libsignal.ecc.ECKeyPair
|
|
|
|
|
|
|
|
import org.whispersystems.libsignal.util.KeyHelper
|
|
|
|
import org.whispersystems.libsignal.util.KeyHelper
|
|
|
|
import org.whispersystems.signalservice.loki.utilities.hexEncodedPublicKey
|
|
|
|
import org.whispersystems.signalservice.loki.utilities.hexEncodedPublicKey
|
|
|
|
import java.io.File
|
|
|
|
|
|
|
|
import java.io.FileOutputStream
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RegisterActivity : BaseActionBarActivity() {
|
|
|
|
class RegisterActivity : BaseActionBarActivity() {
|
|
|
|
|
|
|
|
private val sodium = LazySodiumAndroid(SodiumAndroid())
|
|
|
|
private var seed: ByteArray? = null
|
|
|
|
private var seed: ByteArray? = null
|
|
|
|
private var keyPair: ECKeyPair? = null
|
|
|
|
private var ed25519KeyPair: KeyPair? = null
|
|
|
|
|
|
|
|
private var x25519KeyPair: ECKeyPair? = null
|
|
|
|
set(value) { field = value; updatePublicKeyTextView() }
|
|
|
|
set(value) { field = value; updatePublicKeyTextView() }
|
|
|
|
|
|
|
|
|
|
|
|
// region Lifecycle
|
|
|
|
// region Lifecycle
|
|
|
@ -72,7 +75,10 @@ class RegisterActivity : BaseActionBarActivity() {
|
|
|
|
private fun updateKeyPair() {
|
|
|
|
private fun updateKeyPair() {
|
|
|
|
val seedCandidate = Curve25519.getInstance(Curve25519.BEST).generateSeed(16)
|
|
|
|
val seedCandidate = Curve25519.getInstance(Curve25519.BEST).generateSeed(16)
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
this.keyPair = Curve.generateKeyPair(seedCandidate + seedCandidate) // Validate the seed
|
|
|
|
val padding = ByteArray(16) { 0 }
|
|
|
|
|
|
|
|
ed25519KeyPair = sodium.cryptoSignSeedKeypair(seedCandidate + padding)
|
|
|
|
|
|
|
|
val x25519KeyPair = sodium.convertKeyPairEd25519ToCurve25519(ed25519KeyPair)
|
|
|
|
|
|
|
|
this.x25519KeyPair = ECKeyPair(DjbECPublicKey(x25519KeyPair.publicKey.asBytes), DjbECPrivateKey(x25519KeyPair.secretKey.asBytes))
|
|
|
|
} catch (exception: Exception) {
|
|
|
|
} catch (exception: Exception) {
|
|
|
|
return updateKeyPair()
|
|
|
|
return updateKeyPair()
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -80,7 +86,7 @@ class RegisterActivity : BaseActionBarActivity() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun updatePublicKeyTextView() {
|
|
|
|
private fun updatePublicKeyTextView() {
|
|
|
|
val hexEncodedPublicKey = keyPair!!.hexEncodedPublicKey
|
|
|
|
val hexEncodedPublicKey = x25519KeyPair!!.hexEncodedPublicKey
|
|
|
|
val characterCount = hexEncodedPublicKey.count()
|
|
|
|
val characterCount = hexEncodedPublicKey.count()
|
|
|
|
var count = 0
|
|
|
|
var count = 0
|
|
|
|
val limit = 32
|
|
|
|
val limit = 32
|
|
|
@ -112,9 +118,11 @@ class RegisterActivity : BaseActionBarActivity() {
|
|
|
|
// region Interaction
|
|
|
|
// region Interaction
|
|
|
|
private fun register() {
|
|
|
|
private fun register() {
|
|
|
|
IdentityKeyUtil.save(this, IdentityKeyUtil.lokiSeedKey, Hex.toStringCondensed(seed))
|
|
|
|
IdentityKeyUtil.save(this, IdentityKeyUtil.lokiSeedKey, Hex.toStringCondensed(seed))
|
|
|
|
IdentityKeyUtil.save(this, IdentityKeyUtil.IDENTITY_PUBLIC_KEY_PREF, Base64.encodeBytes(keyPair!!.publicKey.serialize()))
|
|
|
|
IdentityKeyUtil.save(this, IdentityKeyUtil.IDENTITY_PUBLIC_KEY_PREF, Base64.encodeBytes(x25519KeyPair!!.publicKey.serialize()))
|
|
|
|
IdentityKeyUtil.save(this, IdentityKeyUtil.IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(keyPair!!.privateKey.serialize()))
|
|
|
|
IdentityKeyUtil.save(this, IdentityKeyUtil.IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(x25519KeyPair!!.privateKey.serialize()))
|
|
|
|
val userHexEncodedPublicKey = keyPair!!.hexEncodedPublicKey
|
|
|
|
IdentityKeyUtil.save(this, IdentityKeyUtil.ED25519_PUBLIC_KEY, Base64.encodeBytes(ed25519KeyPair!!.publicKey.asBytes))
|
|
|
|
|
|
|
|
IdentityKeyUtil.save(this, IdentityKeyUtil.ED25519_SECRET_KEY, Base64.encodeBytes(ed25519KeyPair!!.secretKey.asBytes))
|
|
|
|
|
|
|
|
val userHexEncodedPublicKey = x25519KeyPair!!.hexEncodedPublicKey
|
|
|
|
val registrationID = KeyHelper.generateRegistrationId(false)
|
|
|
|
val registrationID = KeyHelper.generateRegistrationId(false)
|
|
|
|
TextSecurePreferences.setLocalRegistrationId(this, registrationID)
|
|
|
|
TextSecurePreferences.setLocalRegistrationId(this, registrationID)
|
|
|
|
DatabaseFactory.getIdentityDatabase(this).saveIdentity(Address.fromSerialized(userHexEncodedPublicKey),
|
|
|
|
DatabaseFactory.getIdentityDatabase(this).saveIdentity(Address.fromSerialized(userHexEncodedPublicKey),
|
|
|
@ -129,7 +137,7 @@ class RegisterActivity : BaseActionBarActivity() {
|
|
|
|
|
|
|
|
|
|
|
|
private fun copyPublicKey() {
|
|
|
|
private fun copyPublicKey() {
|
|
|
|
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
|
|
|
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
|
|
|
val clip = ClipData.newPlainText("Session ID", keyPair!!.hexEncodedPublicKey)
|
|
|
|
val clip = ClipData.newPlainText("Session ID", x25519KeyPair!!.hexEncodedPublicKey)
|
|
|
|
clipboard.setPrimaryClip(clip)
|
|
|
|
clipboard.setPrimaryClip(clip)
|
|
|
|
Toast.makeText(this, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show()
|
|
|
|
Toast.makeText(this, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|