05nelsonm / component-encoding

Base16/32/64 encoding for Kotlin Multiplatform

Home Page:https://kotlin-components.matthewnelson.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

component-encoding

Kotlin GitHub license

android apple-silicon ios jvm js node-js linux macos tvos watchos windows

Base16 (Hex)

Base32

Base64

A full list of kotlin-components projects can be found HERE

Get Started

// build.gradle.kts
dependencies {
    val encoding = "1.1.3"
    implementation("io.matthewnelson.kotlin-components:encoding-base16:$encoding")
    implementation("io.matthewnelson.kotlin-components:encoding-base32:$encoding")
    implementation("io.matthewnelson.kotlin-components:encoding-base64:$encoding")
}
// build.gradle
dependencies {
    def encoding = "1.1.3"
    implementation "io.matthewnelson.kotlin-components:encoding-base16:$encoding"
    implementation "io.matthewnelson.kotlin-components:encoding-base32:$encoding"
    implementation "io.matthewnelson.kotlin-components:encoding-base64:$encoding"
}

Kotlin Version Compatibility

Note: as of 1.1.0, the experimental memory model for KotlinNative is enabled.

encoding kotlin
1.1.3 1.6.21
1.1.2 1.6.21
1.1.1 1.6.21
1.1.0 1.6.10
1.0.3 1.5.31

Usage

Encoding/Decoding are extension functions, but below are example classes for demonstration purposes.

import io.matthewnelson.component.encoding.base16.*

class Base16EncodeDecodeExample {
    
    fun base16Encode(bytes: ByteArray): String =
        bytes.encodeBase16()
    
    fun base16EncodeToCharArray(bytes: ByteArray): CharArray =
        bytes.encodeBase16ToCharArray()
        
    fun base16EncodeToByteArray(bytes: ByteArray): ByteArray =
        bytes.encodeBase16ToByteArray()
    
    fun base16Decode(string: String): ByteArray? =
        string.decodeBase16ToArray()
    
    fun base16Decode(chars: CharArray): ByteArray? =
        chars.decodeBase16ToArray()
}
import io.matthewnelson.component.encoding.base32.*

class Base32EncodeDecodeExample {

    // enable whichever for decoding/encoding
    private val base32: Base32 = Base32.Default
    // private val base32: Base32 = Base32.Hex
    // private val base32: Base32 = Base32.Crockford(checkSymbol = null)
    
    fun base32Encode(bytes: ByteArray): String =
        bytes.encodeBase32(base32 = base32)
    
    fun base32EncodeToCharArray(bytes: ByteArray): CharArray =
        bytes.encodeBase32ToCharArray(base32 = base32)
        
    fun base32EncodeToByteArray(bytes: ByteArray): ByteArray =
        bytes.encodeBase32ToByteArray(base32 = base32)
    
    fun base32Decode(string: String): ByteArray? =
        string.decodeBase32ToArray(base32 = base32)
    
    fun base32Decode(chars: CharArray): ByteArray? =
        chars.decodeBase32ToArray(base32 = base32)
}
import io.matthewnelson.component.base64.*

class Base64EncodeDecodeExample {

    // enable whichever for decoding/encoding
    private val base64: Base64 = Base64.Default
    // private val base64: Base64 = Base64.UrlSafe(pad = true)
    
    fun base64Encode(bytes: ByteArray): String =
        bytes.encodeBase64(base64 = base64)
    
    fun base64EncodeToCharArray(bytes: ByteArray): CharArray =
        bytes.encodeBase64ToCharArray(base64 = base64)
        
    fun base64EncodeToByteArray(bytes: ByteArray): ByteArray =
        bytes.encodeBase64ToByteArray(base64 = base64)
    
    fun base64Decode(string: String): ByteArray? =
        string.decodeBase64ToArray()
    
    fun base64Decode(chars: CharArray): ByteArray? =
        chars.decodeBase64ToArray()
}

Git

This project utilizes git submodules. You will need to initialize them when cloning the repository via:

$ git clone --recursive https://github.com/05nelsonm/component-encoding.git

If you've already cloned the repository, run:

$ git checkout master
$ git pull
$ git submodule update --init

In order to keep submodules updated when pulling the latest code, run:

$ git pull --recurse-submodules