Moved getLocalKezs from Crypto object into class.
This commit is contained in:
parent
32fd7638e0
commit
9303952db0
3 changed files with 20 additions and 18 deletions
|
@ -34,6 +34,8 @@ class ConfirmAddContactDialog extends EnsiChatActivity with OnMessageReceivedLis
|
|||
|
||||
private var remoteConfirmed = false
|
||||
|
||||
private lazy val crypto = new Crypto(this)
|
||||
|
||||
override def onCreate(savedInstanceState: Bundle): Unit = {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
|
@ -54,7 +56,7 @@ class ConfirmAddContactDialog extends EnsiChatActivity with OnMessageReceivedLis
|
|||
val remote = view.findViewById(R.id.remote_identicon).asInstanceOf[ImageView]
|
||||
val remoteTitle = view.findViewById(R.id.remote_identicon_title).asInstanceOf[TextView]
|
||||
|
||||
local.setImageBitmap(IdenticonGenerator.generate(Crypto.getLocalAddress(this), (150, 150), this))
|
||||
local.setImageBitmap(IdenticonGenerator.generate(crypto.localAddress, (150, 150), this))
|
||||
remote.setImageBitmap(IdenticonGenerator.generate(user.address, (150, 150), this))
|
||||
remoteTitle.setText(getString(R.string.remote_fingerprint_title, user.name))
|
||||
|
||||
|
@ -102,7 +104,7 @@ class ConfirmAddContactDialog extends EnsiChatActivity with OnMessageReceivedLis
|
|||
* the user is in this activity.
|
||||
*/
|
||||
override def onMessageReceived(msg: Message): Unit = {
|
||||
if (msg.Header.origin != user.address || msg.Header.target != Crypto.getLocalAddress(this))
|
||||
if (msg.Header.origin != user.address || msg.Header.target != crypto.localAddress)
|
||||
return
|
||||
|
||||
msg.Body match {
|
||||
|
|
|
@ -100,7 +100,7 @@ class ChatService extends Service {
|
|||
crypto.generateLocalKeys()
|
||||
|
||||
btInterface.create()
|
||||
Log.i(Tag, "Service started, address is " + Crypto.getLocalAddress(this))
|
||||
Log.i(Tag, "Service started, address is " + crypto.localAddress)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -135,7 +135,7 @@ class ChatService extends Service {
|
|||
return
|
||||
|
||||
val header = new MessageHeader(body.messageType, MessageHeader.DefaultHopLimit,
|
||||
Crypto.getLocalAddress(this), target, seqNumGenerator.next())
|
||||
crypto.localAddress, target, seqNumGenerator.next())
|
||||
|
||||
val msg = new Message(header, body)
|
||||
val encrypted = crypto.encrypt(crypto.sign(msg))
|
||||
|
@ -150,7 +150,7 @@ class ChatService extends Service {
|
|||
* Decrypts and verifies incoming messages, forwards valid ones to [[onNewMessage()]].
|
||||
*/
|
||||
def onMessageReceived(msg: Message): Unit = {
|
||||
if (msg.Header.target == Crypto.getLocalAddress(this)) {
|
||||
if (msg.Header.target == crypto.localAddress) {
|
||||
val decrypted = crypto.decrypt(msg)
|
||||
if (!crypto.verify(decrypted)) {
|
||||
Log.i(Tag, "Ignoring message with invalid signature from " + msg.Header.origin)
|
||||
|
@ -174,7 +174,7 @@ class ChatService extends Service {
|
|||
|
||||
callConnectionListeners()
|
||||
case _: RequestAddContact =>
|
||||
if (msg.Header.origin == Crypto.getLocalAddress(this))
|
||||
if (msg.Header.origin == crypto.localAddress)
|
||||
return
|
||||
|
||||
Log.i(Tag, "Remote device " + msg.Header.origin +
|
||||
|
|
|
@ -47,11 +47,9 @@ object Crypto {
|
|||
|
||||
private val LocalAddressKey = "local_address"
|
||||
|
||||
/**
|
||||
* Returns the address of the local node.
|
||||
*/
|
||||
def getLocalAddress(context: Context) = new Address(
|
||||
PreferenceManager.getDefaultSharedPreferences(context).getString(LocalAddressKey, null))
|
||||
private val PrivateKeyAlias = "local-private"
|
||||
|
||||
private val PublicKeyAlias = "local-public"
|
||||
|
||||
}
|
||||
|
||||
|
@ -61,14 +59,10 @@ object Crypto {
|
|||
* @note We can't use [[KeyStore]], because it requires certificates, and does not work for
|
||||
* private keys
|
||||
*/
|
||||
class Crypto(Context: Context) {
|
||||
class Crypto(context: Context) {
|
||||
|
||||
private val Tag = "Crypto"
|
||||
|
||||
private val PrivateKeyAlias = "local-private"
|
||||
|
||||
private val PublicKeyAlias = "local-public"
|
||||
|
||||
PRNGFixes.apply()
|
||||
|
||||
/**
|
||||
|
@ -92,7 +86,7 @@ class Crypto(Context: Context) {
|
|||
// The hash must have at least one bit set to not collide with the broadcast address.
|
||||
} while(address == Address.Broadcast || address == Address.Null)
|
||||
|
||||
PreferenceManager.getDefaultSharedPreferences(Context)
|
||||
PreferenceManager.getDefaultSharedPreferences(context)
|
||||
.edit()
|
||||
.putString(Crypto.LocalAddressKey, address.toString)
|
||||
.commit()
|
||||
|
@ -226,7 +220,7 @@ class Crypto(Context: Context) {
|
|||
/**
|
||||
* Returns the folder where keys are stored.
|
||||
*/
|
||||
private def keyFolder = new File(Context.getFilesDir, "keys")
|
||||
private def keyFolder = new File(context.getFilesDir, "keys")
|
||||
|
||||
def encrypt(msg: Message, key: PublicKey = null): Message = {
|
||||
assert(msg.Crypto.signature.isDefined, "Message must be signed before encryption")
|
||||
|
@ -308,4 +302,10 @@ class Crypto(Context: Context) {
|
|||
new Address(hash)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the address of the local node.
|
||||
*/
|
||||
def localAddress = new Address(
|
||||
PreferenceManager.getDefaultSharedPreferences(context).getString(LocalAddressKey, null))
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue