MCPcopy
hub / github.com/unkeyed/unkey / New

Function New

pkg/uid/new.go:20–46  ·  view source on GitHub ↗

New generates a prefixed random identifier. The identifier consists of the prefix, an underscore separator, and random alphanumeric characters. Default random portion is 9 characters; pass a custom length to override. Pass an empty prefix to generate an identifier without a prefix. Uses math/rand

(prefix Prefix, length ...int)

Source from the content-addressed store, hash-verified

18// Uses math/rand/v2 which is NOT cryptographically secure. Do not use for
19// API keys, tokens, or security-sensitive purposes.
20func New(prefix Prefix, length ...int) string {
21 n := 9
22 if len(length) > 0 {
23 n = length[0]
24 }
25
26 if n == 0 && prefix == "" {
27 return ""
28 }
29
30 var id strings.Builder
31 if prefix == "" {
32 id.Grow(n)
33 for i := 0; i < n; i++ {
34 id.WriteByte(defaultAlphabet[rand.IntN(len(defaultAlphabet))])
35 }
36 return id.String()
37 }
38
39 id.Grow(len(prefix) + 1 + n)
40 id.WriteString(string(prefix))
41 id.WriteByte('_')
42 for i := 0; i < n; i++ {
43 id.WriteByte(defaultAlphabet[rand.IntN(len(defaultAlphabet))])
44 }
45 return id.String()
46}

Callers 15

TestRedisCounterFunction · 0.92
TestRedisCounterMultiGetFunction · 0.92
InitMethod · 0.92
BenchmarkComparisonFunction · 0.92
BenchmarkNewFunction · 0.92
BenchmarkParallelFunction · 0.92
NewCorrelationIDFunction · 0.92
TestKeyVerificationsFunction · 0.92
createVerificationsFunction · 0.92

Calls 1

StringMethod · 0.45

Tested by 15

TestRedisCounterFunction · 0.74
TestRedisCounterMultiGetFunction · 0.74
BenchmarkComparisonFunction · 0.74
BenchmarkNewFunction · 0.74
BenchmarkParallelFunction · 0.74
TestKeyVerificationsFunction · 0.74
createVerificationsFunction · 0.74
createRatelimitsFunction · 0.74