Skip to content

Commit

Permalink
Merge pull request #1 from abhriyaroy/unit-test
Browse files Browse the repository at this point in the history
Add Unit test
  • Loading branch information
abhriyaroy authored Dec 8, 2023
2 parents 078c95d + 79312ac commit 5a30459
Show file tree
Hide file tree
Showing 16 changed files with 1,837 additions and 280 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: KCrypt Build and Test

on: [pull_request]

jobs:
build-and-test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
name: Check out repository code

- name: Set up JDK 17
uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'adopt'

- name: Grant execute permission for gradlew
run: chmod +x ./gradlew

- name: Build and run tests
run: ./gradlew allTests
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.iml
.gradle
.idea
.DS_Store
build
captures
.externalNativeBuild
.cxx
local.properties
xcuserdata
secring.gpg
25 changes: 24 additions & 1 deletion KCrypt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id("io.realm.kotlin") version "1.10.0"
id("convention.publication")
kotlin("plugin.serialization") version "1.9.0"
id("com.google.devtools.ksp") version "1.9.10-1.0.13"
}

kotlin {
Expand Down Expand Up @@ -35,10 +36,21 @@ kotlin {
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting
val androidUnitTest by getting
val androidUnitTest by getting {
dependencies {
implementation("junit:junit:4.13.2")
implementation("org.jetbrains.kotlin:kotlin-test-junit:1.6.10")

implementation("org.mockito:mockito-core:4.0.0")
implementation("org.mockito.kotlin:mockito-kotlin:4.0.0")

}
}
val iosX64Main by getting
val iosArm64Main by getting
val iosSimulatorArm64Main by getting
Expand All @@ -56,10 +68,21 @@ kotlin {
iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
iosSimulatorArm64Test.dependsOn(this)
dependencies {
implementation("io.mockative:mockative:2.0.1")
}
}
}
}

dependencies {
configurations
.filter { it.name.startsWith("ksp") && it.name.contains("Test") }
.forEach {
add(it.name, "io.mockative:mockative-processor:2.0.1")
}
}

android {
namespace = "studio.zebro.kcrypt"
compileSdk = 34
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package studio.zebro.kcrypt

import java.security.Key
import java.util.Base64
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec

interface CipherProvider {
fun encrypt(input : ByteArray, key : Key) : String
fun decrypt(key : Key, data : String) : ByteArray
}

class CipherProviderImpl : CipherProvider {

override fun encrypt(input: ByteArray, key: Key): String {
val cipher = Cipher.getInstance("AES/CBC/PKCS7Padding")
cipher.init(Cipher.ENCRYPT_MODE, key)
val (cipherText, iv) = cipher.doFinal(input) to cipher.iv
return (Base64.getEncoder()
.encodeToString(cipherText) +
"," +
Base64.getEncoder().encodeToString(iv)).apply {
println("the encoded param is $this")
}
}

override fun decrypt(key: Key, data: String): ByteArray {
val parts = data.split(",")
val plainTextByteArray = Base64.getDecoder().decode(parts[0])
val iv = Base64.getDecoder().decode(parts[1])
val cipher = Cipher.getInstance("AES/CBC/PKCS7Padding")
cipher.init(Cipher.DECRYPT_MODE, key, IvParameterSpec(iv))
return cipher.doFinal(plainTextByteArray)
}
}
Loading

0 comments on commit 5a30459

Please sign in to comment.