package nixcache
import (
"crypto/sha256"
"encoding/base32"
"errors"
"regexp"
"strings"
)
var (
// attic reads * and ? in a cache name as wildcards
validName = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9-_+]{0,49}$`)
plcID = regexp.MustCompile(`^[a-z2-7]{24}$`)
base32Low = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding)
)
func CacheName(repoDID string) (string, error) {
if repoDID == "" {
return "", errors.New("empty repo did")
}
if id, ok := strings.CutPrefix(repoDID, "did:plc:"); ok && plcID.MatchString(id) {
return "plc-" + id, nil
}
sum := sha256.Sum256([]byte(repoDID))
return "h-" + base32Low.EncodeToString(sum[:])[:32], nil
}