Krypto

Krypto is a cryptography library for Multiplatform Kotlin 1.3.

https://github.com/korlibs/krypto

Star

Table of contents:

SecureRandom

Krypto provides a SecureRandom class that extends the kotlin.random.Random class, but generating cryptographic secure values.

It uses SecureRandom on the JVM + PRNGFixes on Android. On Native POSIX (including Linux, macOS and iOS), it uses /dev/urandom, on Windows BCryptGenRandom

Hash (MD5/SHA1/SHA256)

fun ByteArray.hash(algo: HashFactory): ByteArray
fun ByteArray.md5()
fun ByteArray.sha1()
fun ByteArray.sha256()

object MD5 : HashFactory
object SHA1 : HashFactory
object SHA256 : HashFactory

open class HashFactory() {
    fun create(): Hash
}

fun HashFactory.digest(data: ByteArray): ByteArray

abstract class Hash {
    val chunkSize: Int
    val digestSize: Int
    
    fun reset(): Hash
    fun update(data: ByteArray, offset: Int, count: Int): Hash
    fun digest(): ByteArray
    fun digestOut(out: ByteArray)
}

AES

object AES {
    fun decryptAes128Cbc(encryptedMessage: ByteArray, cipherKey: ByteArray): ByteArray
    fun encryptEes128Cbc(plainMessage: ByteArray, cipherKey: ByteArray): ByteArray
}

Using with gradle

Requires Gradle 7.1.1 (JVM 8~13) for building and Kotlin >=1.5.20 for running:

build.gradle.kts

val kryptoVersion = "2.2.0"

repositories {
    maven { url("https://dl.bintray.com/korlibs/korlibs") }
    jcenter()
}

// For multiplatform projects
kotlin {
    sourceSets {
        commonMain {
            dependencies {
                implementation("com.soywiz.korlibs.krypto:krypto:$kryptoVersion") 
            }
        }
    }
}

dependencies {
    // For JVM only
    implementation("com.soywiz.korlibs.krypto:krypto-jvm:$kryptoVersion") 
    // For Android only
    implementation("com.soywiz.korlibs.krypto:krypto-android:$kryptoVersion") 
    // For JS only
    implementation("com.soywiz.korlibs.krypto:krypto-js:$kryptoVersion") 
}