Browse Source

Add nonce checks

connman
adityapk00 5 years ago
parent
commit
3fd9c039b5
  1. 40
      app/src/main/java/com/adityapk/zcash/zqwandroid/DataModel.kt
  2. 2
      app/src/main/java/com/adityapk/zcash/zqwandroid/MainActivity.kt
  3. 3
      app/src/main/java/com/adityapk/zcash/zqwandroid/TransactionItemFragment.kt

40
app/src/main/java/com/adityapk/zcash/zqwandroid/DataModel.kt

@ -30,17 +30,23 @@ object DataModel {
var secret : ByteArray? = null
var nonce : ByteArray? = null
fun ByteArray.toHexString() = joinToString("") { String.format("%02x", it) }
fun ByteArray.toHexString() : String {
return (joinToString("") { String.format("%02x", it) })
}
fun String.hexStringToByteArray(byteSize: Int) : ByteArray {
val s = "00".repeat(byteSize - this.length / 2)
return ByteArray(byteSize) { s.substring(it * 2, it * 2 + 2).toInt(16).toByte() }
}
fun init() {
fun init(context: Context) {
val sodium = NaCl.sodium()
secret = ByteArray(Sodium.crypto_hash_sha256_bytes())
Sodium.crypto_hash_sha256(secret, "secret".toByteArray(), "secret".toByteArray().size)
nonce = ByteArray(Sodium.crypto_box_noncebytes())
Sodium.randombytes_buf(nonce, Sodium.crypto_box_noncebytes())
nonce = ByteArray(Sodium.crypto_secretbox_noncebytes())
/*
val message = "test".toByteArray()
val noncelen = Sodium.crypto_secretbox_noncebytes().toLong()
val nonce = ByteArray(noncelen.toInt())
@ -62,6 +68,7 @@ object DataModel {
Log.i(TAG, ret.toString())
println("Recovered message=" + String(decrypted))
*/
}
fun parseResponse(response: String) : Boolean {
@ -109,12 +116,12 @@ object DataModel {
)) }
Log.w(TAG, payload.toJsonString(true))
ws?.send(payload.toJsonString())
ws?.send(encrypt(payload.toJsonString()))
}
fun makeAPICalls() {
ws?.send(encrypt(json { obj("command" to "getInfo") }.toJsonString()))
ws?.send(json { obj("command" to "getTransactions")}.toJsonString())
ws?.send(encrypt(json { obj("command" to "getTransactions")}.toJsonString()))
}
@ -135,19 +142,32 @@ object DataModel {
val encrypted = ByteArray(msg.size + Sodium.crypto_secretbox_macbytes())
// Increment nonce
Sodium.sodium_increment(nonce, nonce?.size ?: 0)
val localNonce = incAndGetLocalNonce()
val ret = Sodium.crypto_secretbox_easy(encrypted, msg, msg.size, nonce, secret)
val ret = Sodium.crypto_secretbox_easy(encrypted, msg, msg.size, localNonce, secret)
if (ret != 0) {
println("Encryption failed")
}
val j = json { obj("nonce" to nonce?.toHexString(),
val j = json { obj("nonce" to localNonce?.toHexString(),
("payload" to encrypted.toHexString()))}
return j.toJsonString()
}
fun incAndGetLocalNonce() : ByteArray {
doInc(nonce!!)
doInc(nonce!!)
return nonce!!
}
fun doInc(ba : ByteArray) {
ba.reverse()
Sodium.sodium_increment(ba, ba.size)
ba.reverse()
}
private val TAG = "DataModel"
}

2
app/src/main/java/com/adityapk/zcash/zqwandroid/MainActivity.kt

@ -38,7 +38,7 @@ class MainActivity : AppCompatActivity(), TransactionItemFragment.OnFragmentInte
// When creating, clear all the data first
setMainStatus("")
DataModel.init()
DataModel.init(applicationContext)
btnConnect.setOnClickListener {
val intent = Intent(this, QrReaderActivity::class.java)

3
app/src/main/java/com/adityapk/zcash/zqwandroid/TransactionItemFragment.kt

@ -69,7 +69,8 @@ class TransactionItemFragment : Fragment() {
val col = view.findViewById<ImageView>(R.id.typeColor)
val amt = view.findViewById<TextView>(R.id.txamt)
val amtzec = tx?.amount?.toDoubleOrNull() ?: 0.0
amt.text = DataModel.mainResponseData?.tokenName + (if (tx?.type == "send") "" else "+") + DecimalFormat("#0.00########").format(amtzec)
amt.text = DataModel.mainResponseData?.tokenName + " " +
(if (tx?.type == "send") "" else "+") + DecimalFormat("#0.00########").format(amtzec)
if (tx?.type == "send") {
col.setImageResource(R.color.colorAccent)

Loading…
Cancel
Save