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
()
| 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. |
| 16 | func 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 | } |