Skip to content

Commit

Permalink
Merge pull request #30 from ionspin/better-sample
Browse files Browse the repository at this point in the history
Better sample
  • Loading branch information
ionspin authored Oct 9, 2022
2 parents d0e9920 + c843ea5 commit 8994787
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

[![pipeline status](https://gitlab.com/ionspin-github-ci/kotlin-multiplatform-libsodium/badges/master/pipeline.svg)](https://gitlab.com/ionspin-github-ci/kotlin-multiplatform-libsodium/-/commits/master)
[![pipeline status](https://gitlab.com/ionspin-github-ci/kotlin-multiplatform-libsodium/badges/main/pipeline.svg)](https://gitlab.com/ionspin-github-ci/kotlin-multiplatform-libsodium/-/commits/main)

![Danger: Experimental](https://camo.githubusercontent.com/275bc882f21b154b5537b9c123a171a30de9e6aa/68747470733a2f2f7261772e6769746875622e636f6d2f63727970746f7370686572652f63727970746f7370686572652f6d61737465722f696d616765732f6578706572696d656e74616c2e706e67)

Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ object Versions {
val kotlinBigNumVersion = "0.3.7"
val jna = "5.10.0"
val kotlinPoet = "1.6.0"
val sampleLibsodiumBindings = "0.8.5-SNAPSHOT"
val sampleLibsodiumBindings = "0.8.8-SNAPSHOT"
val ktor = "1.3.2"
val timber = "4.7.1"
val kodeinVersion = "7.1.0"
Expand Down
2 changes: 0 additions & 2 deletions multiplatform-crypto-libsodium-bindings/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,6 @@ kotlin {
kotlin.srcDirs("src/jvmSpecific", "src/jvmMain/kotlin")
dependencies {
implementation(kotlin(Deps.Jvm.stdLib))
implementation(kotlin(Deps.Jvm.test))
implementation(kotlin(Deps.Jvm.testJUnit))

implementation(Deps.Jvm.resourceLoader)

Expand Down
1 change: 1 addition & 0 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ kotlin {
implementation(kotlin(Deps.Common.stdLib))
implementation(kotlin(Deps.Common.test))
implementation(Deps.Common.serialization)
// api(Deps.Common.libsodiumBindings)
api(project(":multiplatform-crypto-libsodium-bindings"))
implementation(Deps.Common.coroutines)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ionspin.kotlin.crypto.sample

import com.ionspin.kotlin.crypto.aead.AuthenticatedEncryptionWithAssociatedData
import com.ionspin.kotlin.crypto.aead.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES
import com.ionspin.kotlin.crypto.hash.Hash
import com.ionspin.kotlin.crypto.util.LibsodiumRandom
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
Expand All @@ -10,10 +12,24 @@ object Sample {
fun runSample() {
val random = LibsodiumRandom.buf(32)
println("Random: ${random.toHexString()}")
println("Hashed 123 ${hashSomething()}")
encryptThenDecrypt()
}

fun hashSomething() : String {
val hash = Hash.sha512("123".encodeToUByteArray())
return "Hash (SHA512) of 123: ${hash.toHexString()}"
}

fun encryptThenDecrypt() {
val data = LibsodiumRandom.buf(2048)
val associatedData = LibsodiumRandom.buf(256)
val key = AuthenticatedEncryptionWithAssociatedData.xChaCha20Poly1305IetfKeygen()
val nonce = LibsodiumRandom.buf(crypto_aead_xchacha20poly1305_ietf_NPUBBYTES)
val encrypted = AuthenticatedEncryptionWithAssociatedData.xChaCha20Poly1305IetfEncrypt(data, associatedData, nonce, key)
println("Original data ${data.toHexString()}")
println("Encrypted ${encrypted.toHexString()}")
val decrypted = AuthenticatedEncryptionWithAssociatedData.xChaCha20Poly1305IetfDecrypt(encrypted, associatedData, nonce, key)
println("Decrypted ${decrypted.toHexString()}")
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegated
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime

@ExperimentalTime
import com.ionspin.kotlin.crypto.sample.Sample

fun main() {
println("Test")
// Blake
val blake = Blake2bDelegated()
blake.digest()
Sample.runSample()
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import com.ionspin.kotlin.crypto.sample.Sample
import kotlin.time.ExperimentalTime

@ExperimentalTime
fun main() {

Sample.runSample()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import com.ionspin.kotlin.crypto.sample.Sample

fun main() {
Sample.runSample()
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import com.ionspin.kotlin.crypto.sample.Sample
import kotlin.time.ExperimentalTime

@ExperimentalTime

fun main() {
Sample.runSample()
Expand Down
7 changes: 4 additions & 3 deletions sample/src/nativeMain/kotlin/Runner.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.ionspin.kotlin.crypto.sample.Sample

fun main() : Unit {
Sample.runSample()
}
// Clashes with platform specific main()
//fun main() : Unit {
// Sample.runSample()
//}

0 comments on commit 8994787

Please sign in to comment.