Nano ID: Popular, Secure, and URL-Friendly Unique Identifiers
What is Nano ID and why is it suitable to use this standard for web apps?
The truth is, that Nano ID looks much more friendly in URLs when combined with a so-called slug text compared to the UUID. Nano ID is created similarly to random-based UUID v4, with a similar number of random bits in the ID (126
in Nano ID and 122
UUID), thus having a comparable collision probability.
To gain a clearer grasp, if duplications are a concern, generating 103 trillion IDs in version 4 is necessary to attain a probability of one in a billion.
Comparison of Nano ID and UUID v4
Nano ID employs a larger alphabet, allowing the packing of a comparable number of random bits into just 21
symbols rather than 36
. As a result, the Nano ID code is four times smaller than the UUID v4 package, comprising 136
bytes instead of 423
.
What About Security?
Nano ID, of course, prioritizes security. It utilizes several significant standards which we will discuss below.
Unpredictability
This lib uses the crypto
module in NodeJs
and Web Crypto API
in web browsers. It doesn’t use anything predictable like “Math.random()
”.
Ingenuity
Utilizes a sophisticated algorithm and undergoes testing for uniformity. You can view the complete algorithm here. So it doesn’t exploit a known vulnerability in generation (random % alphabet
), reducing the number of brute-forcing attempts.
Support
Tidelift serves as the designated security point of contact for numerous open-source projects.
What does the Nano ID look like?
Here is an example of what generated Nano IDs look like.
// eCsd2NHKc6bb4w-ZD3b1G
// bMGJ4vsKbGrrdv-L8Qqmw
// XuQHF8cjnqycZPIsqvJpe
// bfpX_NcdztmbLW78LXhR3
// bbUteXTuC6elaKWKTldMn
// _gOIheG92VAZ3UYTIkx6O
// XRi0LyTOxJPzj2TaKVfe_
// orRgcUctn4egGVtfZg1Gr
// USSCyMOFEMyrpoosbgU9X
// AwIXnabeTBe4UmA522P60
Installing and Using Nano ID in JavaScript
Installation of Nano ID via NPM
npm install nanoid
import { nanoid } from 'nanoid';
console.log(nanoid()); // output -> "eZ3E_akRVB_MU8KKmmdA8"
Flexibility and Adaptability
ID size
Should you wish to decrease the size of the ID (thus increasing the probability of collisions), you have the option to specify the size as an argument.
console.log(nanoid(10)); // output -> "vedFhT6co"
Remember to verify the security of your ID size using the Nano ID Collision Calculator.
Nano ID CLI
Nano ID has CLI support. Below are a few usage examples.
$ npx nanoid
Need to install the following packages:
nanoid@5.0.6
Ok to proceed? (y) y
JSagbVIa9IwfdChXLnGT6
$ npx nanoid
4S9SmdQ_OzcDdKUlKbI0e
$ npx nanoid --size 10
D7r7SAjlLj
$ npx nanoid --size 24
Nm55fp7tx3aovty_eTVGbj0n
$ npx nanoid --alphabet xyz --size 15
xzyzyzyyxyzxxxz
Custom Random Bytes
Nano ID is very flexible, and we can define our own character palette and ID length.
import { customRandom } from 'nanoid';
const nanoid = customRandom('uvwxyz', 10, size => {
return (new Uint8Array(size)).map(() => 256 * Math.random());
// Math.random is used only for testing purposes
// please use a custom function instead.
});
console.log(nanoid()); // output -> "uyvzzwyzzz"
Nano ID Collision Calculator
If you want to control or optimize the character count of your IDs for your project, I recommend checking out the Nano ID Collision Calculator tool. Using this tool, you can easily determine the probability of at least one collision.
Usability Scale
Nano ID can be used in various domains and applications where unique identifiers need to be generated. Some of the possible uses of Nano ID include:
Database applications
For generating unique identifiers for records in a database.
Authentication and security
For creating random tokens used in authentication, especially in multi-user systems.
Shortened URL addresses
For creating short and unique URL addresses for various resources.
File or directory name generation
For creating unique file or directory names in multi-user systems or file management applications.
Identity management applications
For creating unique identifiers for users, devices, or other entities in identity management systems.
And much more
These are just some examples of the use of Nano ID, but there are many other possibilities depending on specific requirements and applications.
Growth of Nano ID’s Popularity in the Chart (NPM)
Source: npmtrends.com
Nano ID and Programming Languages
Nano ID has been adapted for numerous languages, allowing you to employ these adaptations for a consistent ID generation process across client and server environments.
Below you can see a list of supported languages and technologies.
C#, C++, Clojure and ClojureScript, ColdFusion/CFML, Crystal, Dart & Flutter, Deno, Elixir, Go, Haskell, Haxe, Janet, Java, JavaScript, Kotlin, MySQL/MariaDB, Nim, OCaml, Perl, PHP, Python, R, Ruby, Rust, Swift, Unison, V, Zig
Nano ID Generator Online
To generate some Nano IDs quickly, you can use the Nano ID Generator at https://www.devgyver.tech/nanoid
Do you like Nano ID? Claps 👏, follow, and share on social media.
Reference
Are You Exploring Alternatives to Nano ID? Consider UUID for Your Project
Do you want to learn more about UUIDs? I recommend checking out my article.