Skip to content

wabbit-corp/kotlin-base58

Repository files navigation

kotlin-base58

Maven Central Kotlin Multiplatform

kotlin-base58 is a Kotlin Multiplatform library for encoding binary data as compact, human-friendly Base58 strings and decoding those strings back to bytes or typed values.

It is designed for cases where hexadecimal is too long and Base64 is too punctuation-heavy, especially for IDs that humans may need to read, paste, or type.

Why Base58

The Base58 alphabet removes visually ambiguous characters:

  • 0
  • O
  • I
  • l

That makes it useful for invitation codes, external IDs, short opaque tokens, and UUID-like values that should stay copyable without looking noisy.

This library implements plain Base58 encoding. It does not implement Base58Check or any checksum-bearing Bitcoin-specific format.

🚀 Installation

repositories {
    mavenCentral()
}

dependencies {
    implementation("one.wabbit:kotlin-base58:1.1.1")
}

🚀 Usage

import one.wabbit.base58.Base58

val payload = "Hello, world!".encodeToByteArray()
val encoded = Base58.encode(payload)
val decoded = Base58.decode(encoded).decodeToString()

check(encoded == "72k1xXWG59wUsYv7h2")
check(decoded == "Hello, world!")

Primitive Helpers

The library also supports typed helpers for fixed-width values:

import one.wabbit.base58.Base58

val orderId = 42
val encodedOrderId = Base58.encodeInt(orderId)
val decodedOrderId = Base58.decodeInt(encodedOrderId)

check(decodedOrderId == orderId)

These helpers use fixed-width big-endian byte order, which keeps the encoding deterministic across JVMs and interoperable with non-Kotlin implementations that use the same convention.

They are binary serialization helpers, not compact numeric Base58 helpers. For example, encodeInt(42) encodes the four-byte sequence 00 00 00 2A, so decodeInt("j") fails even though "j" is the compact Base58 representation of numeric 42.

Uuid Example

import one.wabbit.base58.Base58
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid

@OptIn(ExperimentalUuidApi::class)
fun example() {
    val sessionId = Uuid.parse("123e4567-e89b-12d3-a456-426614174000")
    val encodedSessionId = Base58.encodeUuid(sessionId)
    val decodedSessionId = Base58.decodeUuid(encodedSessionId)

    check(decodedSessionId == sessionId)
}

The UUID helpers use Kotlin's experimental kotlin.uuid.Uuid type, so call sites may need to opt in with @OptIn(ExperimentalUuidApi::class).

Leading Zeros

Leading zero bytes are preserved. That matters when you are encoding binary identifiers instead of text:

import one.wabbit.base58.Base58

val bytes = byteArrayOf(0, 0, 1, 2, 3)
val encoded = Base58.encode(bytes)
val decoded = Base58.decode(encoded)

check(decoded.contentEquals(bytes))

Error Handling

Base58.decode and the typed decode helpers throw Base58DecodingException when:

  • the input contains characters outside the Base58 alphabet
  • the input contains non-ASCII characters
  • the decoded byte length does not match the requested target type

For example, decodeUuid rejects values that do not decode to exactly 16 bytes.

Status

This library is small and stable in scope. It implements plain Base58 only; checksum-bearing formats such as Base58Check are intentionally out of scope.

Documentation

Generated API docs can be built locally with Dokka. See API reference notes for the command.

Release Notes

Licensing

This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0) for open source use.

For commercial use, contact Wabbit Consulting Corporation at wabbit@wabbit.one.

Contributing

Before contributions can be merged, contributors need to agree to the repository CLA.

About

No description, website, or topics provided.

Resources

License

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors