aleris / alphaid

Random string id generator with web friendly ids by default.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AlphaId

Random pseudo-unique string id generator with a default web friendly alphabet.

Based on JNanoId by Aventrix with:

  • Written in Kotlin.
  • Performance improvements.
  • Allow creating a reusable instance with custom alphabet and/or random generator.

Secure

Uses SecureRandom by default to generate cryptographically strong random IDs with a good distribution of characters.

Compact

Generates compact IDs. By using a larger alphabet than UUID, AlphaId can generate a greater number of unique IDs in the same space, when compared to UUID.

URL-Friendly

Uses URL-friendly characters (A-Za-z0-9_-). Perfect for unique identifiers in web applications.

Customizable

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

Tested

Tested thoroughly. Look in the test to see sample usages.

Dependency

Maven

<dependency>
  <groupId>com.semanticmart.alphaid</groupId>
  <artifactId>alphaid</artifactId>
  <version>0.0.2</version>
</dependency>

Gradle

Groovy DSL

compile 'com.semanticmart.alphaid:alphaid:0.0.2'

Kotlin DSL

implementation("com.semanticmart.alphaid:alphaid:0.0.2")

Usage

Provides one easy-to-use utility class (AlphaId) with methods to generate IDs.

Default IDs - randomId()

The default method creates secure, url-friendly, unique ids. It uses an url-friendly alphabet (A-Za-z0-9_-), a secure random number generator, and generates a unique ID with 32 characters.

val id = AlphaId.randomId()
// id is something like `DVUQIrcvdhuAZhEWYapF58j4_m-5pT6j`

Specify length - randomId(length)

An additional method allows to generate custom IDs by specifying a custom length.

val id = AlphaId.randomId(4)
// id is something like `5Osn`

AlphaId.random(21) generates an id with the smaller length than a UUID (32 characters) with a greater number of unique IDs.

Customize alphabet and/or random generator

Generate with a custom alphabet:

val alphaId = AlphaId(alphabet = "xyz".toCharArray())
val id1 = alphaId.next(10)
// id1 is something like `xxzxzyyyzy`
val id2 = alphaId.next(10)
// id2 is something like `yyyxyzzzxz`

Generate with a faster, less secure random generator, with a fixed seed:

val alphaId = AlphaId(randomGenerator = Random(123))
val id1 = alphaId.next(10)
// id1 is `rLvTj2WWfa`
val id2 = alphaId.next(10)
// id2 is `kfl_MAUg3l`

Publish

Execute sh ./scripts/publish.sh patch script to automatically increase the version and publish to sonatype.

The tags can be: major, minor or patch corresponding to semantic version major.minor.patch.

License

This project is licensed under the terms of the MIT license.

Based on the NanoId by Andrey Sitnik and JNanoId by Aventrix.

About

Random string id generator with web friendly ids by default.

License:MIT License


Languages

Language:Kotlin 92.6%Language:Shell 7.4%