Java小强个人技术博客站点    手机版
当前位置: 首页 >> 开源 >> 唯一ID生成器:NanoID

唯一ID生成器:NanoID

100 开源 | 2026-2-11

一个小巧、安全、URL友好、唯一的 JavaScript 字符串ID生成器。

NanoID.png

小巧. 118字节 (经过压缩和Brotli处理)。没有依赖。Size Limit 控制大小。

安全. 它使用硬件随机生成器。可在集群中使用。

紧凑. 它使用比 UUID(A-Za-z0-9_-)更大的字母表。因此,ID 大小从36个符号减少到21个符号。

可移植. Nano ID 已被移植到 20种编程语言。

官网:https://github.com/ai/nanoid/blob/main/README.zh-CN.md 


与 UUID 的比较

Nano ID 与 UUID v4 (基于随机数) 相当。 它们在 ID 中有相似数量的随机位 (Nano ID 为126,UUID 为122),因此它们的碰撞概率相似::要想有十亿分之一的重复机会, 必须产生103万亿个版本4的ID。

Nano ID 和 UUID v4之间有两个主要区别:

Nano ID 使用更大的字母表,所以类似数量的随机位 被包装在21个符号中,而不是36个。

Nano ID 代码比 uuid/v4 包少 4倍: 130字节而不是423字节.


Java集成官方参考:https://github.com/wosherco/jnanoid-enhanced 


下面我们使用写一个测试用例:

POM引入

<dependency>
    <groupId>com.aventrix.jnanoid</groupId>
    <artifactId>jnanoid</artifactId>
    <version>2.0.0</version>
    <scope>runtime</scope>
</dependency>

 

Java使用

package com.example.demo;
import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import java.util.Random;
public class TestNanoId {
    public static void main(String[] args) {
        Random random = new Random();
        for (int i = 0; i < 20; i++) {
            // 生成一个默认长度(21位)的 NanoID
            {
                String id = NanoIdUtils.randomNanoId();
                System.out.println("生成的 NanoID: " + id);
            }
            // 在决定 ID 长度 和 字符集 前,强烈建议使用官方的碰撞概率计算器评估风险:
            // https://zelark.github.io/nano-id-cc/
            // 使用自定义字符集(如只使用数字)和长度(如 8 位)
            {
                char[] alphabet = "0123456789".toCharArray();
                int size = 8;
                String customId = NanoIdUtils.randomNanoId(random, alphabet, size);
                System.out.println("自定义 NanoID: " + customId);
            }
            System.out.println("-------------------------------------------------------");
        }
    }
}


不通版本,和不同框架的实现,使用方法要注意区分,但是意思都是大同小异,例如我们使用的这个,他的主体类只有两个静态方法,一个无参数一个需要三个参数。

源码:

/**
 * Copyright (c) 2017 The JNanoID Authors
 * Copyright (c) 2017 Aventrix LLC
 * Copyright (c) 2017 Andrey Sitnik
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.aventrix.jnanoid.jnanoid;

import java.security.SecureRandom;
import java.util.Random;

/**
 * A class for generating unique String IDs.
 *
 * The implementations of the core logic in this class are based on NanoId, a JavaScript
 * library by Andrey Sitnik released under the MIT license. (https://github.com/ai/nanoid)
 *
 * @author David Klebanoff
 */
public final class NanoIdUtils {

    /**
     * <code>NanoIdUtils</code> instances should NOT be constructed in standard programming.
     * Instead, the class should be used as <code>NanoIdUtils.randomNanoId();</code>.
     */
    private NanoIdUtils() {
        //Do Nothing
    }

    /**
     * The default random number generator used by this class.
     * Creates cryptographically strong NanoId Strings.
     */
    public static final SecureRandom DEFAULT_NUMBER_GENERATOR = new SecureRandom();

    /**
     * The default alphabet used by this class.
     * Creates url-friendly NanoId Strings using 64 unique symbols.
     */
    public static final char[] DEFAULT_ALPHABET =
            "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

    /**
     * The default size used by this class.
     * Creates NanoId Strings with slightly more unique values than UUID v4.
     */
    public static final int DEFAULT_SIZE = 21;

    /**
     * Static factory to retrieve a url-friendly, pseudo randomly generated, NanoId String.
     *
     * The generated NanoId String will have 21 symbols.
     *
     * The NanoId String is generated using a cryptographically strong pseudo random number
     * generator.
     *
     * @return A randomly generated NanoId String.
     */
    public static String randomNanoId() {
        return randomNanoId(DEFAULT_NUMBER_GENERATOR, DEFAULT_ALPHABET, DEFAULT_SIZE);
    }

    /**
     * Static factory to retrieve a NanoId String.
     *
     * The string is generated using the given random number generator.
     *
     * @param random   The random number generator.
     * @param alphabet The symbols used in the NanoId String.
     * @param size     The number of symbols in the NanoId String.
     * @return A randomly generated NanoId String.
     */
    public static String randomNanoId(final Random random, final char[] alphabet, final int size) {

        if (random == null) {
            throw new IllegalArgumentException("random cannot be null.");
        }

        if (alphabet == null) {
            throw new IllegalArgumentException("alphabet cannot be null.");
        }

        if (alphabet.length == 0 || alphabet.length >= 256) {
            throw new IllegalArgumentException("alphabet must contain between 1 and 255 symbols.");
        }

        if (size <= 0) {
            throw new IllegalArgumentException("size must be greater than zero.");
        }

        final int mask = (2 << (int) Math.floor(Math.log(alphabet.length - 1) / Math.log(2))) - 1;
        final int step = (int) Math.ceil(1.6 * mask * size / alphabet.length);

        final StringBuilder idBuilder = new StringBuilder();

        while (true) {

            final byte[] bytes = new byte[step];
            random.nextBytes(bytes);

            for (int i = 0; i < step; i++) {

                final int alphabetIndex = bytes[i] & mask;

                if (alphabetIndex < alphabet.length) {
                    idBuilder.append(alphabet[alphabetIndex]);
                    if (idBuilder.length() == size) {
                        return idBuilder.toString();
                    }
                }

            }

        }

    }
}


推荐您阅读更多有关于“ UUID 唯一 NanoID NodeJS 自定义 ”的文章

下一篇:使用FFmpeg通过RSTP协议拉取视频保存到本地2

猜你喜欢

发表评论: