-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from abhriyaroy/unit-test
Add Unit test
- Loading branch information
Showing
16 changed files
with
1,837 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
KCrypt/src/androidMain/kotlin/studio/zebro/kcrypt/CipherProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.