唯一ID生成类NanoId

NanoIDh和UUID一样都是是软件开发中最常用的通用标识符之一。

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

使用自定义工具类可以做定制化开发

import java.security.SecureRandom;

public final class NanoIdUtils {
	private static final SecureRandom DEFAULT_NUMBER_GENERATOR = new SecureRandom();
	//长度必须大于0,小于等于256,自定义字符
	private static final char[] DEFAULT_ALPHABET = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
	//位数必须大于0,自定义长度
	private static final int DEFAULT_SIZE = 21;

	private NanoIdUtils() {
	}

	public static String randomNanoId() {
		return randomNanoId(DEFAULT_SIZE);
	}

	public static String randomNanoId(int size) {
		if (size <= 0) {
			throw new IllegalArgumentException("size must be greater than zero.");
		}
		if (DEFAULT_ALPHABET.length == 0 || DEFAULT_ALPHABET.length > 256) {
			throw new IllegalArgumentException("alphabet must contain between 1 and 255 symbols.");
		}

		int mask = (2 << (int) Math.floor(Math.log((double) (DEFAULT_ALPHABET.length - 1)) / Math.log(2.0D))) - 1;
		int step = (int) Math.ceil(1.6D * (double) mask * (double) size / (double) DEFAULT_ALPHABET.length);
		StringBuilder idBuilder = new StringBuilder();
		while (true) {
			byte[] bytes = new byte[step];
			DEFAULT_NUMBER_GENERATOR.nextBytes(bytes);
			for (int i = 0; i < step; ++i) {
				int alphabetIndex = bytes[i] & mask;
				if (alphabetIndex < DEFAULT_ALPHABET.length) {
					idBuilder.append(DEFAULT_ALPHABET[alphabetIndex]);
					if (idBuilder.length() == size) {
						return idBuilder.toString();
					}
				}
			}
		}
	}
}

源码

/**
 * 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();
                    }
                }

            }

        }

    }
}

NanoId的特点

1. 容量小

NanoID 比UUID要小 4.5 倍,并且没有任何依赖关系。使用 NanoID 的对象小而紧凑,能够用于数据传输和存储。随着应用程序的增长,这些数字变得明显起来。

2. 更安全

在大多数随机生成器中,它们使用不安全的Math.random()。NanoID 使用SecureRandom更安全。

3. 速度快

NanoID 比 UUID 快 60%。与 UUID 字母表中的 36 个字符不同,NanoID 只有 21 个字符。

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-

4. 兼容性

NanoID 支持 14 种不同的编程语言,它们分别是:

C#、C++、Clojure 和 ClojureScript、Crystal、Dart & Flutter、Deno、Go、Elixir、Haskell、Janet、Java、Nim、Perl、PHP、带字典的 Python、Ruby、Rust、Swift

它还支持 PouchDB、CouchDB WebWorkers、Rollup 以及 React 和 Reach-Native 等库。

我们可以使用npx nanoid在终端中获得唯一 ID。在 JavaScript 中使用 NanoID 唯一的要求是要先安装 NodeJS。

此外,我们还可以在 Redux toolkit 中找到 NanoID,并将其用于其他用例,如下所示;

import{nanoid}from‘@reduxjs/toolkit’ console.log(nanoid())//‘dgPXxUz_6fWIQBD8XmiSy’

5. 自定义字符

NanoID 的另一个现有功能是它允许开发人员使用自定义字符表和字符长度。

import{customAlphabet}from'nanoid'; constnanoid=customAlphabet('ABCDEF1234567890',12); model.id=nanoid();

自定义字母表定义为ABCDEF1234567890,并将 Id 的大小定义为 12。

6. 没有第三方依赖

由于 NanoID 不依赖任何第三方依赖,随着时间的推移,它能够变得更加稳定自治。

从长远来看,这有利于优化包的大小,并使其不太容易出现依赖项带来的问题。

参考连接:换掉 UUID,更快更安全! - 知乎


  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值