2025-07-12 18:53:25 +02:00
|
|
|
// Copyright 2025 The Forgejo Authors
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2025-08-14 15:46:01 +00:00
|
|
|
"fmt"
|
2025-07-12 18:53:25 +02:00
|
|
|
)
|
|
|
|
|
2025-08-14 15:46:01 +00:00
|
|
|
func randName(size int) (string, error) {
|
2025-07-12 18:53:25 +02:00
|
|
|
randBytes := make([]byte, size)
|
|
|
|
if _, err := rand.Read(randBytes); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(randBytes), nil
|
|
|
|
}
|
2025-08-14 15:46:01 +00:00
|
|
|
|
|
|
|
func MustRandName(size int) string {
|
|
|
|
name, err := randName(size)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("RandName(%d): %v", size, err))
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|