Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions libs/encryption/utils/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
plugins {
alias(libs.plugins.flipcash.android.library)
kotlin("multiplatform")
id("com.android.kotlin.multiplatform.library")
}

android {
namespace = "${Gradle.codeNamespace}.encryption.utils"
}
kotlin {
android {
namespace = "com.getcode.encryption.utils"
compileSdk = 37
minSdk = 29
withHostTest {}
}

dependencies {
implementation(project(":libs:encryption:base58"))
implementation(libs.protobuf.kotlin.lite)
implementation(project(":libs:encryption:ed25519"))
implementation(libs.bundles.kotlinx.serialization)
iosArm64()
iosSimulatorArm64()
iosX64()

testImplementation(kotlin("test"))
testImplementation(libs.robolectric)
sourceSets {
commonMain {
dependencies {
implementation(project(":libs:encryption:base58"))
}
}
androidMain {
dependencies {
implementation(project(":libs:encryption:ed25519"))
implementation(libs.protobuf.kotlin.lite)
implementation(libs.bundles.kotlinx.serialization)
implementation(libs.timber)
}
}
commonTest {
dependencies {
implementation(kotlin("test"))
}
}
getByName("androidHostTest") {
dependencies {
implementation(kotlin("test"))
implementation(libs.robolectric)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.getcode.utils

import kotlin.test.Test
import kotlin.test.assertEquals

class UrlExtensionsTest {

@Test
fun urlEncodeDecodeRoundtrip() {
val original = "hello world & foo=bar"
assertEquals(original, original.urlEncode().urlDecode())
}

@Test
fun urlEncodeSpaces() {
assertEquals("hello+world", "hello world".urlEncode())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.getcode.utils

import android.util.Base64
import com.getcode.ed25519.Ed25519
import com.getcode.vendor.Base58
import com.google.protobuf.ByteString
import java.net.URLDecoder
import java.net.URLEncoder
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException

fun List<Byte>.toByteString(): ByteString = ByteString.copyFrom(this.toByteArray())
fun ByteArray.toByteString(): ByteString = ByteString.copyFrom(this)

val List<Byte>.base64: String
get() = Base64.encodeToString(toByteArray(), Base64.NO_WRAP)

fun String.decodeBase64(): ByteArray = Base64.decode(this, Base64.NO_WRAP)

fun String.decodeBase64UrlSafe(): ByteArray = Base64.decode(this, Base64.NO_WRAP or Base64.URL_SAFE)

fun ByteArray.decodeBase64(): ByteArray = Base64.decode(this, Base64.NO_WRAP)

val ByteArray.base64: String
get() = Base64.encodeToString(this, Base64.NO_WRAP)

fun ByteArray.encodeBase64(urlSafe: Boolean = false): String {
val flags = if (urlSafe) Base64.NO_WRAP or Base64.URL_SAFE else Base64.NO_WRAP
return Base64.encodeToString(this, flags)
}

fun ByteArray.encodeBase64ToArray(): ByteArray = Base64.encode(this, Base64.NO_WRAP)

fun ByteArray.sha512(): ByteArray {
return try {
MessageDigest.getInstance("SHA-512")
.apply { update(this@sha512) }
.digest()
} catch (e: NoSuchAlgorithmException) {
throw RuntimeException("SHA-512 not implemented")
}
}

fun String.urlEncode(): String = URLEncoder.encode(this, "UTF-8")

fun String.urlDecode(): String = URLDecoder.decode(this, "UTF-8")

fun Ed25519.KeyPair.getPublicKeyBase58(): String = Base58.encode(publicKeyBytes)
Loading
Loading