Skip to content

NanoId#

A Kotlin Multiplatform port of this JavaScript library, providing a fast, secure and URL-friendly unique ID generator.

GitHub Repository

🎯 Supported Targets#

The following targets are supported:

Platform Targets
JVM & Android jvm, android
Apple ios, macos, tvos, watchos
Web js, wasmJs
Native & Other linux, mingw

✨ Features#

NanoId comes with several features while beeing fully customizable.

🔒 Secure#

NanoId uses Korlibs Crypto SecureRandom to generate cryptographically strong random IDs with a proper distribution of characters.

Crypto 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, iOS) it uses /dev/urandom
  • On Windows BCryptGenRandom is in use

📦 Compact#

NanoId generates compact IDs with just 21 characters.

By using a larger alphabet than UUID, NanoId can generate a greater number of unique IDs, when compared to UUID, with fewer characters (21 vs 36).

🖌️ Customizable#

NanoId is fully customizable.

All default options may be overridden. Supply your own Random Number Generator, alphabet or size.

🚀 Installation#

Integration using Version Catalog is highly recommended for aligned version usage.

First declare the library in your Version Catalog:

[versions]
inkraft-nanoid = "<version>"

[libraries]
inkraft-nanoid = { group = "dev.datlag.inkraft", name = "nanoid", version.ref = "inkraft-nanoid" }

Then add the dependency to your module:

dependencies {
    implementation(libs.inkraft.nanoid)
}

Simply add the dependency like this:

dependencies {
    implementation("dev.datlag.inkraft:nanoid:<version>")
}

🛠️ Usage#

Creating a default URL-friendly unique identifier:

val id = NanoId.generate()

Customizing the generated output:

val generator = Random()
val alphabet = "0123456789"
val size = 20

val id = NanoId.generate(generator, alphabet, size)