⚠️ 10th of October this library will be moved to https://github.com/wosherco/jnanoid-enhanced, together with the jitpack repo.
A unique string ID generator for Java.
Original JNanoId seems to not be maintained anymore. This fork is to add some features.
JNanoID uses Java’s SecureRandom to generate cryptographically strong random IDs with a proper distribution of characters.
JNanoID generates compact IDs with just 21 characters. By using a larger alphabet than UUID, JNanoID can generate a greater number of unique IDs, when compared to UUID, with fewer characters (21 versus 36).
JNanoID uses URL-friendly characters (A-Za-z0-9_-
). Perfect for unique identifiers in web applications.
JNanoID is fully customizable. All default options may be overridden. Supply your own Random Number Generator, alphabet, or size.
JNanoID is thoroughly tested with JUnit.
The most recent release is JNanoId 2.0.2.
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.soundicly</groupId>
<artifactId>jnanoid-enhanced</artifactId>
<version>main-SNAPSHOT</version>
</dependency>
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.soundicly:jnanoid-enhanced:main-SNAPSHOT'
}
JNanoId provides one easy-to-use utility class (NanoIdUtils
) with two methods to generate IDs.
The default method creates secure, url-friendly, unique ids. It uses a url-friendly alphabet (A-Za-z0-9_-
), a secure random number generator, and generates a unique ID with 21 characters.
String id = NanoIdUtils.randomNanoId(); // "ku-qLNv1wDmIS5_EcT3j7"
An additional method that allows you to generate a secure unique ID with a customizable number of characters.
String id = NanoIdUtils.randomNanoId(5); // "f7Yd4"
An additional method that allows you to generate a secure unique ID with a customizable alphabet and number of characters.
// Use a custom alphabet containing only a, b, and c
char[] alphabet = {'a','b','c'};
String id = NanoIdUtils.randomNanoId(alphabet, 5); // "accab"
An additional method allows you to generate custom IDs by specifying your own random number generator, alphabet, or size.
// Use a faster, but non-secure, random generator
Random random = new Random();
// Use a custom alphabet containing only a, b, and c
char[] alphabet = {'a','b','c'};
// Make IDs 10 characters long
int size = 10;
String id = NanoIdUtils.randomNanoId(random, alphabet, 10); // "babbcaabcb"
Original Author: Aventrix LLC.
Code released under the MIT License.
Based on the original NanoId for JavaScript by Andrey Sitnik.
- C#
- Clojure and ClojureScript
- Crystal
- Dart
- Go
- Elixir
- Haskell
- JavaScript
- Nim
- PHP
- Python
- Ruby
- Rust
- Swift
Also, a CLI tool is available to generate IDs from the command line.