2026-10-20 23:15:19 -07:00
|
|
|
package ids
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/rand"
|
|
|
|
|
"crypto/sha256"
|
|
|
|
|
"encoding/binary"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
randomLength = 11
|
|
|
|
|
)
|
|
|
|
|
|
2026-10-21 09:05:33 -07:00
|
|
|
type Prefix string
|
|
|
|
|
|
2026-10-20 23:15:19 -07:00
|
|
|
const (
|
2026-10-21 09:05:33 -07:00
|
|
|
Project Prefix = "prj"
|
|
|
|
|
Task Prefix = "tsk"
|
|
|
|
|
Session Prefix = "ses"
|
|
|
|
|
Snapshot Prefix = "snp"
|
|
|
|
|
Review Prefix = "rev"
|
|
|
|
|
InboxItem Prefix = "inb"
|
|
|
|
|
InboxResponse Prefix = "rsp"
|
|
|
|
|
Blob Prefix = "blb"
|
|
|
|
|
LeadNote Prefix = "not"
|
|
|
|
|
WorkflowDefinition Prefix = "wfd"
|
|
|
|
|
WorkflowRun Prefix = "run"
|
|
|
|
|
Send Prefix = "snd"
|
|
|
|
|
Client Prefix = "cli"
|
|
|
|
|
ClientRequest Prefix = "req"
|
|
|
|
|
Transfer Prefix = "xfr"
|
|
|
|
|
Terminal Prefix = "trm"
|
|
|
|
|
SavedCommand Prefix = "cmd"
|
2026-10-21 15:15:20 -07:00
|
|
|
Schedule Prefix = "sch"
|
2026-10-20 23:15:19 -07:00
|
|
|
)
|
|
|
|
|
|
2026-10-21 09:05:33 -07:00
|
|
|
func New(prefix Prefix) string {
|
2026-10-20 23:15:19 -07:00
|
|
|
out := make([]byte, randomLength)
|
|
|
|
|
buf := make([]byte, 1)
|
|
|
|
|
for i := 0; i < len(out); {
|
|
|
|
|
rand.Read(buf)
|
|
|
|
|
if int(buf[0]) >= 256/len(alphabet)*len(alphabet) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
out[i] = alphabet[int(buf[0])%len(alphabet)]
|
|
|
|
|
i++
|
|
|
|
|
}
|
2026-10-21 09:05:33 -07:00
|
|
|
return string(prefix) + string(out)
|
2026-10-20 23:15:19 -07:00
|
|
|
}
|
|
|
|
|
|
2026-10-21 09:05:33 -07:00
|
|
|
func Derive(prefix Prefix, parts ...string) string {
|
2026-10-20 23:15:19 -07:00
|
|
|
sum := sha256.Sum256([]byte(strings.Join(parts, "\x00")))
|
|
|
|
|
n := binary.BigEndian.Uint64(sum[:8])
|
|
|
|
|
out := make([]byte, randomLength)
|
|
|
|
|
for i := range out {
|
|
|
|
|
out[i] = alphabet[n%uint64(len(alphabet))]
|
|
|
|
|
n /= uint64(len(alphabet))
|
|
|
|
|
}
|
2026-10-21 09:05:33 -07:00
|
|
|
return string(prefix) + string(out)
|
2026-10-20 23:15:19 -07:00
|
|
|
}
|