MCPcopy Create free account
hub / github.com/coder/coder / GenerateName

Function GenerateName

coderd/prebuilds/util.go:16–26  ·  view source on GitHub ↗

GenerateName generates a 20-byte prebuild name which should safe to use without truncation in most situations. UUIDs may be too long for a resource name in cloud providers (since this ID will be used in the prebuild's name). We're generating a 9-byte suffix (72 bits of entropy): 1 - e^(-1e9^2 / (2

()

Source from the content-addressed store, hash-verified

14// 1 - e^(-1e9^2 / (2 * 2^72)) = ~0.01% likelihood of collision in 1 billion IDs.
15// See https://en.wikipedia.org/wiki/Birthday_attack.
16func GenerateName() (string, error) {
17 b := make([]byte, 9)
18
19 _, err := rand.Read(b)
20 if err != nil {
21 return "", err
22 }
23
24 // Encode the bytes to Base32 (A-Z2-7), strip any '=' padding
25 return fmt.Sprintf("prebuild-%s", strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(b))), nil
26}

Callers 3

TestInProgressActionsFunction · 0.92
TestExpiredPrebuildsFunction · 0.92

Calls 1

ReadMethod · 0.65

Tested by 2

TestInProgressActionsFunction · 0.74
TestExpiredPrebuildsFunction · 0.74