package cz.vutbr.benchmark

import android.os.Debug
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import javax.crypto.KeyGenerator

/**
 * Benchmark, which will execute on an Android device.
 *
 * The body of [BenchmarkRule.measureRepeated] is measured in a loop, and Studio will
 * output the result. Modify your code to see how it affects performance.
 */
@RunWith(AndroidJUnit4::class)
class KeyGenerationBenchmark {

    @get:Rule
    val benchmarkRule = BenchmarkRule()

    @Test
    fun benchmarkAES128() {
        benchmarkAES(128)
    }

    @Test
    fun benchmarkAES192() {
        benchmarkAES(192)
    }

    @Test
    fun benchmarkAES256() {
        benchmarkAES(256)
    }

    private fun benchmarkAES(keySize: Int) {
        val keyGenerator = KeyGenerator
            .getInstance("AES", "AndroidKeyStore")
        val keyGenParameterSpec =
            KeyGenParameterSpec.Builder(
                " AES_KEY_ALIAS ",
                KeyProperties.PURPOSE_ENCRYPT
                        or
                        KeyProperties.PURPOSE_DECRYPT
            )
                .setKeySize(keySize)
                .build()
        keyGenerator.init(keyGenParameterSpec)

        benchmarkRule.measureRepeated {
            keyGenerator.generateKey()
        }
    }

}
