sixafter/nanoid

GitHub: sixafter/nanoid

一个简单、快速、安全且经过极致性能优化的 Go 语言 NanoID 唯一标识符生成库。

Stars: 33 | Forks: 5

NanoID Logo

nanoid: 小巧。安全。随机。


[![Go Report Card](https://goreportcard.com/badge/github.com/sixafter/nanoid)](https://goreportcard.com/report/github.com/sixafter/nanoid) [![License: Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue?style=flat-square)](LICENSE) [![Go](https://img.shields.io/github/go-mod/go-version/sixafter/nanoid)](https://img.shields.io/github/go-mod/go-version/sixafter/nanoid) [![Go Reference](https://pkg.go.dev/badge/github.com/sixafter/nanoid.svg)](https://pkg.go.dev/github.com/sixafter/nanoid) [![FIPS-140 Mode Compatible](https://img.shields.io/badge/FIPS‑140--Mode-Compatible-brightgreen)](FIPS-140.md)
## 状态 [![Release](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/1e777293ec075444.svg)](https://github.com/sixafter/nanoid/actions) [![CI](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/6c9cb21c90075445.svg)](https://github.com/sixafter/nanoid/actions) [![GitHub issues](https://img.shields.io/github/issues/sixafter/nanoid)](https://github.com/sixafter/nanoid/issues) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=six-after_nano-id&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=six-after_nano-id) ![CodeQL](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/17a7468e18075447.svg) [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=six-after_nano-id&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=six-after_nano-id) [![OpenSSF Best Practices](https://www.bestpractices.dev/projects/10826/badge)](https://www.bestpractices.dev/projects/10826) [![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/sixafter/nanoid/badge)](https://scorecard.dev/viewer/?uri=github.com/sixafter/nanoid) ## 概述 这是 [Nano ID](https://github.com/ai/nanoid) 的一个简单、快速且高效的 Go 实现,它是一个小巧、安全、URL 友好且抗碰撞的字符串生成器。 请参阅 [godoc](https://pkg.go.dev/github.com/sixafter/nanoid) 获取详细文档。 ## 功能特性 * 使用 Go 的 `crypto/rand` 和 `x/crypto/chacha20` 流密码包来生成加密安全的随机数。这保证了生成的 ID 既不可预测,又适用于对安全性敏感的应用程序。 - 自定义的加密安全伪随机数生成器 (CSPRNG) 包含一个用于并发访问的线程安全全局 `Reader`。 - 在使用 Google 的 [UUID](https://pkg.go.dev/github.com/google/uuid) 包生成 v4 UUID 时,如果使用 `prng.Reader` 作为来源,速度提升高达 98%。 * 定义你自己的用于 ID 生成的字符集,最小长度为 2 个字符,最大长度为 256 个字符。 * 定义你自己的随机数生成器。 * 支持 Unicode 和 ASCII 字母表。 * 设计上可在并发环境中安全使用。 * 结构经过精心优化,以最大程度减少堆分配,降低内存开销并提高缓存局部性;这对于性能和资源使用至关重要的应用程序来说非常关键。 - 无论字母表大小或生成的 ID 长度如何,ASCII 和 Unicode 字母表均为 1 `allocs/op`。 - 无论字母表大小或生成的 ID 长度如何,ASCII 和 Unicode 字母表的 `Reader` 接口均为 0 `allocs/op`。 * 轻量级实现,除了测试之外,不依赖标准库以外的外部依赖。 * 支持 `io.Reader` 接口 - 用于流数据处理、pipeline 和其他 I/O 驱动操作等场景。 * 设计用于在 FIPS‑140 验证环境中运行,仅使用 Go 标准库加密。 - 对于 FIPS‑140 兼容的随机数生成,请使用 [aes-ctr-drbg](https://github.com/sixafter/aes-ctr-drbg) 模块。 - 详情及部署指南请参阅 [FIPS‑140.md](FIPS-140.md)。 请参阅 [nanoid-cli](https://github.com/sixafter/nanoid-cli),这是一个使用此模块生成 Nano ID 的命令行接口 (CLI)。 ## 使用 Cosign 验证 使用 [Cosign](https://github.com/sigstore/cosign) 对发布版本进行签名,以进行完整性验证。 要验证发布版本的完整性,请按照以下步骤操作: ``` # 从 GitHub API 获取最新 release tag(例如 "v1.64.0") TAG=$(curl -s https://api.github.com/repos/sixafter/nanoid/releases/latest | jq -r .tag_name) # 移除文件名前的 "v"(例如 "v1.64.0" -> "1.64.0") VERSION=${TAG#v} # --------------------------------------------------------------------- # 使用 Sigstore bundles 验证源归档文件 # --------------------------------------------------------------------- # 下载 release tarball 及其签名 bundle curl -LO "https://github.com/sixafter/nanoid/releases/download/${TAG}/nanoid-${VERSION}.tar.gz" curl -LO "https://github.com/sixafter/nanoid/releases/download/${TAG}/nanoid-${VERSION}.tar.gz.sigstore.json" # 使用发布的公钥通过 Cosign 验证 tarball cosign verify-blob \ --key "https://raw.githubusercontent.com/sixafter/nanoid/main/cosign.pub" \ --bundle "nanoid-${VERSION}.tar.gz.sigstore.json" \ "nanoid-${VERSION}.tar.gz" # --------------------------------------------------------------------- # 使用 Sigstore bundles 验证 checksums 清单 # --------------------------------------------------------------------- curl -LO "https://github.com/sixafter/nanoid/releases/download/${TAG}/checksums.txt" curl -LO "https://github.com/sixafter/nanoid/releases/download/${TAG}/checksums.txt.sigstore.json" cosign verify-blob \ --key "https://raw.githubusercontent.com/sixafter/nanoid/main/cosign.pub" \ --bundle "checksums.txt.sigstore.json" \ "checksums.txt" # --------------------------------------------------------------------- # 确认本地 artifact 完整性 # --------------------------------------------------------------------- shasum -a 256 -c checksums.txt ``` 如果有效,Cosign 将输出: ``` Verified OK ``` ## 安装 ### 使用 `go get` 要安装 Nano ID包,请运行以下命令: ``` go get -u github.com/sixafter/nanoid ``` 要在你的 Go 项目中使用 NanoID 包,请按如下方式导入: ``` import "github.com/sixafter/nanoid" ``` ## 用法 ### 使用默认设置的基本用法 生成 Nano ID 最简单的方法是使用默认设置。这使用了预定义的字母表和默认的 ID 长度。 ``` package main import ( "fmt" "github.com/sixafter/nanoid" ) func main() { id, err := nanoid.New() if err != nil { panic(err) } fmt.Println("Generated ID:", id) } ``` **输出**: ``` Generated ID: mGbzQkkPBidjL4IP_MwBM ``` ### 生成自定义长度的 Nano ID 生成具有自定义长度的 NanoID。 ``` package main import ( "fmt" "github.com/sixafter/nanoid" ) func main() { id, err := nanoid.NewWithLength(10) if err != nil { panic(err) } fmt.Println("Generated ID:", id) } ``` **输出**: ``` Generated ID: 1A3F5B7C9D ``` ### 使用 `io.Reader` 接口 将 Nano ID 生成器用作 `io.Reader`: ``` package main import ( "fmt" "io" "github.com/sixafter/nanoid" ) func main() { // Nano ID default length is 21 buf := make([]byte, nanoid.DefaultLength) // Read a Nano ID into the buffer _, err := nanoid.Read(buf) if err != nil && err != io.EOF { panic(err) } // Convert the byte slice to a string id := string(buf) fmt.Printf("Generated ID: %s\n", id) } ``` **输出**: ``` Generated ID: 2mhTvy21bBZhZcd80ZydM ``` ### 自定义字母表和 ID 长度 使用 `WithAlphabet` 来自定义字母表。 ``` package main import ( "fmt" "github.com/sixafter/nanoid" ) func main() { // Define a custom alphabet alphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // Create a new generator with a custom alphabet and length hint gen, err := nanoid.NewGenerator( nanoid.WithAlphabet(alphabet), nanoid.WithLengthHint(10), ) if err != nil { fmt.Println("Error creating Nano ID generator:", err) return } // Generate a Nano ID using the custom generator id, err := gen.New() // or gen.NewWithLength(10) if err != nil { fmt.Println("Error generating Nano ID:", err) return } fmt.Println("Generated ID:", id) } ``` **输出**" ``` Generated ID: G5J8K2M0QZ ``` ### 自定义随机数生成器 使用 `WithRandReader` 选项来提供自定义的随机数生成器。 ``` package main import ( "crypto/rand" "fmt" "github.com/sixafter/nanoid" ) func main() { // Create a new generator with a custom random number generator gen, err := nanoid.NewGenerator( nanoid.WithRandReader(rand.Reader), ) if err != nil { fmt.Println("Error creating Nano ID generator:", err) return } // Generate a Nano ID using the custom generator with // the default length. id, err := gen.New() if err != nil { fmt.Println("Error generating Nano ID:", err) return } fmt.Println("Generated ID:", id) } ``` **输出**" ``` Generated ID: A8I8K3J0QY ``` ## 性能优化 此基准测试突显了此 Nano ID 生成器实现中所实现的性能优化。 测试在配备 16 核心的 Apple M4 Max CPU 上进行,结果显示在各种配置下,延迟、吞吐量和内存分配均有显著改善。 | Mode | Latency (ns/op) | Throughput (IDs/sec) | Memory (B/op) | Allocs | Notes | |:-------------------------------------| --------------: | -------------------: | ------------: | -----: | :-------------------------- | | **Serial** | 74.1 | ~13.5 M | 24 | 1 | Single-threaded allocation | | **Parallel (16 cores)** | 5.6 | ~178 M | 24 | 1 | Near-linear scalability | | **Buffered Read (optimal 3–5 B)** | 25.0 | ~40 M | 0 | 0 | Fastest buffered config | | **ASCII ID (21 chars)** | 54.0 | ~18.5 M | 0 | 0 | Default configuration | | **Unicode ID (21 chars)** | 125.0 | ~8.0 M | 48 | 1 | UTF-8 overhead (~2× slower) | 有关实现细节、基准测试结果和用法,请参阅 CSPRNG [prng-chacha](https://github.com/sixafter/prng-chacha)。 ## 执行基准测试 使用 `bench` make 目标运行基准测试: ``` make bench ``` ### 解读结果: 你的输出应类似于以下内容:
展开查看结果 ``` make bench go test -bench=. -benchmem -memprofile=mem.out -cpuprofile=cpu.out goos: darwin goarch: arm64 pkg: github.com/sixafter/nanoid cpu: Apple M4 Max Benchmark_Allocations_Serial-16 14830308 74.10 ns/op 24 B/op 1 allocs/op Benchmark_Allocations_Parallel-16 87374926 13.93 ns/op 24 B/op 1 allocs/op Benchmark_Read_DefaultLength-16 21823753 54.01 ns/op 0 B/op 0 allocs/op Benchmark_Read_VaryingBufferSizes/BufferSize_2-16 49202928 24.94 ns/op 0 B/op 0 allocs/op Benchmark_Read_VaryingBufferSizes/BufferSize_3-16 47922843 25.39 ns/op 0 B/op 0 allocs/op Benchmark_Read_VaryingBufferSizes/BufferSize_5-16 44769436 27.30 ns/op 0 B/op 0 allocs/op Benchmark_Read_VaryingBufferSizes/BufferSize_13-16 31385779 37.98 ns/op 0 B/op 0 allocs/op Benchmark_Read_VaryingBufferSizes/BufferSize_21-16 24563296 49.45 ns/op 0 B/op 0 allocs/op Benchmark_Read_VaryingBufferSizes/BufferSize_34-16 17664668 67.80 ns/op 0 B/op 0 allocs/op Benchmark_Read_ZeroLengthBuffer-16 986404063 1.212 ns/op 0 B/op 0 allocs/op Benchmark_Read_Concurrent/Concurrency_1-16 23469319 50.41 ns/op 0 B/op 0 allocs/op Benchmark_Read_Concurrent/Concurrency_2-16 47287224 25.17 ns/op 0 B/op 0 allocs/op Benchmark_Read_Concurrent/Concurrency_4-16 89502978 13.22 ns/op 0 B/op 0 allocs/op Benchmark_Read_Concurrent/Concurrency_8-16 123994070 9.912 ns/op 0 B/op 0 allocs/op Benchmark_Read_Concurrent/Concurrency_16-16 209096799 5.656 ns/op 0 B/op 0 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen2/IDLen8-16 31861614 37.86 ns/op 8 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen2/IDLen16-16 22977042 51.86 ns/op 16 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen2/IDLen21-16 18545468 64.42 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen2/IDLen32-16 15435464 76.98 ns/op 32 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen2/IDLen64-16 9426519 126.7 ns/op 64 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen2/IDLen128-16 5483312 217.6 ns/op 128 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen16/IDLen8-16 32191970 37.13 ns/op 8 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen16/IDLen16-16 22994911 51.37 ns/op 16 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen16/IDLen21-16 18733461 64.11 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen16/IDLen32-16 15541071 76.51 ns/op 32 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen16/IDLen64-16 9399879 127.0 ns/op 64 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen16/IDLen128-16 5479530 218.4 ns/op 128 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen32/IDLen8-16 32642143 37.36 ns/op 8 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen32/IDLen16-16 23300292 51.40 ns/op 16 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen32/IDLen21-16 18884118 63.52 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen32/IDLen32-16 15857773 75.56 ns/op 32 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen32/IDLen64-16 9551781 126.2 ns/op 64 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen32/IDLen128-16 5524531 217.5 ns/op 128 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen64/IDLen8-16 32380906 37.33 ns/op 8 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen64/IDLen16-16 22809477 51.54 ns/op 16 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen64/IDLen21-16 18773943 63.74 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen64/IDLen32-16 15882687 75.79 ns/op 32 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen64/IDLen64-16 9416259 126.7 ns/op 64 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/ASCII_AlphabetLen64/IDLen128-16 5503002 217.8 ns/op 128 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen2/IDLen8-16 16668662 70.04 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen2/IDLen16-16 11526090 104.1 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen2/IDLen21-16 9527602 125.2 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen2/IDLen32-16 6898809 174.3 ns/op 80 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen2/IDLen64-16 3853819 312.6 ns/op 144 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen2/IDLen128-16 2084970 575.9 ns/op 289 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen16/IDLen8-16 17133789 69.94 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen16/IDLen16-16 11481788 105.0 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen16/IDLen21-16 9534361 125.9 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen16/IDLen32-16 6850174 174.2 ns/op 80 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen16/IDLen64-16 3839973 311.8 ns/op 144 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen16/IDLen128-16 2088130 574.6 ns/op 289 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen32/IDLen8-16 17156673 70.04 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen32/IDLen16-16 11410390 105.0 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen32/IDLen21-16 9537810 125.3 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen32/IDLen32-16 6809893 175.9 ns/op 80 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen32/IDLen64-16 3833170 313.1 ns/op 144 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen32/IDLen128-16 2081092 574.3 ns/op 289 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen64/IDLen8-16 16723262 71.30 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen64/IDLen16-16 11270502 105.6 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen64/IDLen21-16 9602179 125.9 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen64/IDLen32-16 6761265 176.4 ns/op 80 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen64/IDLen64-16 3848028 312.4 ns/op 144 B/op 1 allocs/op Benchmark_Alphabet_Varying_Serial/Unicode_AlphabetLen64/IDLen128-16 2083189 576.3 ns/op 289 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen2/IDLen8-16 194620867 6.209 ns/op 8 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen2/IDLen16-16 141351794 9.845 ns/op 16 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen2/IDLen21-16 100000000 11.55 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen2/IDLen32-16 81193770 14.75 ns/op 32 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen2/IDLen64-16 45330201 26.13 ns/op 64 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen2/IDLen128-16 25851924 46.62 ns/op 128 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen16/IDLen8-16 216777411 5.758 ns/op 8 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen16/IDLen16-16 144298010 8.848 ns/op 16 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen16/IDLen21-16 100000000 11.71 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen16/IDLen32-16 83137520 13.78 ns/op 32 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen16/IDLen64-16 47245101 25.14 ns/op 64 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen16/IDLen128-16 26406150 45.66 ns/op 128 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen32/IDLen8-16 188019464 5.510 ns/op 8 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen32/IDLen16-16 149308290 8.004 ns/op 16 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen32/IDLen21-16 100000000 11.01 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen32/IDLen32-16 83274057 13.92 ns/op 32 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen32/IDLen64-16 46638768 25.90 ns/op 64 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen32/IDLen128-16 26978290 44.27 ns/op 128 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen64/IDLen8-16 206536862 5.456 ns/op 8 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen64/IDLen16-16 152015960 8.130 ns/op 16 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen64/IDLen21-16 100000000 10.83 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen64/IDLen32-16 84979563 13.97 ns/op 32 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen64/IDLen64-16 48281804 24.52 ns/op 64 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/ASCII_AlphabetLen64/IDLen128-16 26492161 43.76 ns/op 128 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen2/IDLen8-16 100000000 12.01 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen2/IDLen16-16 60293805 19.54 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen2/IDLen21-16 53047172 21.30 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen2/IDLen32-16 37889799 32.63 ns/op 80 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen2/IDLen64-16 21920443 54.26 ns/op 144 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen2/IDLen128-16 12110583 99.18 ns/op 288 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen16/IDLen8-16 98768138 12.44 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen16/IDLen16-16 59912625 20.72 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen16/IDLen21-16 55665618 22.08 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen16/IDLen32-16 36814191 31.89 ns/op 80 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen16/IDLen64-16 21810398 54.81 ns/op 144 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen16/IDLen128-16 12246813 98.59 ns/op 288 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen32/IDLen8-16 94218027 11.72 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen32/IDLen16-16 62175088 19.93 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen32/IDLen21-16 52981692 21.81 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen32/IDLen32-16 37626205 31.30 ns/op 80 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen32/IDLen64-16 22032514 53.96 ns/op 144 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen32/IDLen128-16 12033945 98.92 ns/op 288 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen64/IDLen8-16 100000000 11.71 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen64/IDLen16-16 58656163 21.07 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen64/IDLen21-16 55079770 21.62 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen64/IDLen32-16 38111671 31.08 ns/op 80 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen64/IDLen64-16 22505924 53.80 ns/op 144 B/op 1 allocs/op Benchmark_Alphabet_Varying_Parallel/Unicode_AlphabetLen64/IDLen128-16 12325257 97.61 ns/op 288 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen2/IDLen8-16 30982933 38.31 ns/op 8 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen2/IDLen16-16 22225959 53.39 ns/op 16 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen2/IDLen21-16 17688570 67.18 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen2/IDLen32-16 14962710 80.15 ns/op 32 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen2/IDLen64-16 8811254 135.8 ns/op 64 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen2/IDLen128-16 5078203 236.1 ns/op 128 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen16/IDLen8-16 31699906 38.37 ns/op 8 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen16/IDLen16-16 22297476 53.64 ns/op 16 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen16/IDLen21-16 17475834 67.39 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen16/IDLen32-16 14922480 80.63 ns/op 32 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen16/IDLen64-16 8864101 135.1 ns/op 64 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen16/IDLen128-16 5098405 235.2 ns/op 128 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen32/IDLen8-16 31704025 38.13 ns/op 8 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen32/IDLen16-16 22310568 53.15 ns/op 16 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen32/IDLen21-16 17880979 67.22 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen32/IDLen32-16 14910853 79.94 ns/op 32 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen32/IDLen64-16 8856570 135.2 ns/op 64 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen32/IDLen128-16 5126726 233.6 ns/op 128 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen64/IDLen8-16 29376333 39.24 ns/op 8 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen64/IDLen16-16 21777496 53.67 ns/op 16 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen64/IDLen21-16 17666781 66.84 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen64/IDLen32-16 15056240 79.63 ns/op 32 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen64/IDLen64-16 8923198 134.1 ns/op 64 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/ASCII_AlphabetLen64/IDLen128-16 5096005 232.5 ns/op 128 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen2/IDLen8-16 16700318 72.12 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen2/IDLen16-16 10957123 108.2 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen2/IDLen21-16 9271933 129.5 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen2/IDLen32-16 6679417 179.7 ns/op 80 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen2/IDLen64-16 3739922 321.0 ns/op 144 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen2/IDLen128-16 2036672 590.0 ns/op 288 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen16/IDLen8-16 16775415 71.37 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen16/IDLen16-16 11206695 106.8 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen16/IDLen21-16 9401438 127.6 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen16/IDLen32-16 6660018 180.1 ns/op 80 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen16/IDLen64-16 3748488 320.5 ns/op 144 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen16/IDLen128-16 2034178 590.0 ns/op 288 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen32/IDLen8-16 16434753 72.80 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen32/IDLen16-16 11008993 108.5 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen32/IDLen21-16 9391768 127.9 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen32/IDLen32-16 6771036 177.6 ns/op 80 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen32/IDLen64-16 3759814 319.2 ns/op 144 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen32/IDLen128-16 2033529 589.7 ns/op 288 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen64/IDLen8-16 16771731 71.23 ns/op 24 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen64/IDLen16-16 11227098 106.8 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen64/IDLen21-16 9385282 127.8 ns/op 48 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen64/IDLen32-16 6751688 177.9 ns/op 80 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen64/IDLen64-16 3754378 319.9 ns/op 144 B/op 1 allocs/op Benchmark_Alphabet_Varying_Length_Varying_Serial/Unicode_AlphabetLen64/IDLen128-16 2040801 588.3 ns/op 288 B/op 1 allocs/op PASS ok github.com/sixafter/nanoid 199.184s ```
* `ns/op`:每次操作的纳秒数。值越低表示性能越快。 * `B/op`:每次操作分配的字节数。值越低表示代码内存效率越高。 * `allocs/op`:每次操作的内存分配次数。分配次数越少通常性能越好。 ## 字母表约束 1. 字母表长度: * 至少两个字符:字母表必须包含至少两个不同的字符。少于两个字符的字母表无法产生具有足够可变性或随机性的 ID。 * 最大长度 256 个字符:该实现采用基于 rune 的方法,其中每个字符由一个 rune 表示。这允许容纳广泛的唯一字符,支持最多包含 256 个不同 rune 的字母表。尝试使用包含超过 256 个 rune 的字母表将导致错误。 2. 字符的唯一性: * 所有字符必须是唯一的。字母表中的重复字符会导致 ID 生成出现偏差,并损害 ID 的随机性和唯一性。生成器通过在初始化期间检查重复项来强制执行唯一性。如果检测到重复项,它将返回 `ErrDuplicateCharacters` 错误。 3. 字符编码: * 支持 ASCII 和 Unicode:生成器接受包含 Unicode 字符的字母表,允许包含各种符号、表情符号或不同语言的字符。 ## 确定碰撞 要确定适合你用例的 NanoID 实际长度,请参阅[此处](https://sixafter.github.io/nanoid/)的碰撞时间计算器。 ## 贡献 欢迎贡献。请参阅 [CONTRIBUTING](CONTRIBUTING.md) ## 许可证 本项目基于 [Apache 2.0 License](https://choosealicense.com/licenses/apache-2.0/) 授权。请参阅 [LICENSE](LICENSE) 文件。
标签:FIPS-140, Go, Golang, ID生成器, LangChain, NanoID, Ruby工具, URL友好, UUID, 加密, 后端开发, 唯一标识符, 安全, 安全编程, 工具库, 库, 应急响应, 开发组件, 日志审计, 漏洞扫描器, 超时处理, 轻量级, 随机字符串