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.
Uses SecureRandom by default to generate cryptographically strong random IDs with a good distribution of characters.
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.
Uses URL-friendly characters (A-Za-z0-9_-
). Perfect for unique identifiers in web applications.
All default options may be overridden. Supply your own Random Number Generator, alphabet, or size.
Tested thoroughly. Look in the test to see sample usages.
<dependency>
<groupId>com.semanticmart.alphaid</groupId>
<artifactId>alphaid</artifactId>
<version>0.0.2</version>
</dependency>
Groovy DSL
compile 'com.semanticmart.alphaid:alphaid:0.0.2'
Kotlin DSL
implementation("com.semanticmart.alphaid:alphaid:0.0.2")
Provides one easy-to-use utility class (AlphaId
) with methods to generate IDs.
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`
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.
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`
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
.
This project is licensed under the terms of the MIT license.
Based on the NanoId by Andrey Sitnik and JNanoId by Aventrix.