Audit of fe2a202
This commit is contained in:
+23
-38
@@ -4,7 +4,6 @@ import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -13,36 +12,29 @@ const (
|
||||
randomLength = 11
|
||||
)
|
||||
|
||||
type Prefix string
|
||||
|
||||
const (
|
||||
Project = "prj"
|
||||
Task = "tsk"
|
||||
Session = "ses"
|
||||
Snapshot = "snp"
|
||||
Review = "rev"
|
||||
InboxItem = "inb"
|
||||
InboxResponse = "rsp"
|
||||
Blob = "blb"
|
||||
LeadNote = "not"
|
||||
WorkflowDefinition = "wfd"
|
||||
WorkflowRun = "run"
|
||||
Send = "snd"
|
||||
Client = "cli"
|
||||
ClientRequest = "req"
|
||||
Transfer = "xfr"
|
||||
Terminal = "trm"
|
||||
SavedCommand = "cmd"
|
||||
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"
|
||||
)
|
||||
|
||||
var prefixes = map[string]bool{
|
||||
Project: true, Task: true, Session: true, Snapshot: true, Review: true,
|
||||
InboxItem: true, InboxResponse: true, Blob: true, LeadNote: true,
|
||||
WorkflowDefinition: true, WorkflowRun: true, Send: true,
|
||||
Client: true, ClientRequest: true, Transfer: true,
|
||||
Terminal: true, SavedCommand: true,
|
||||
}
|
||||
|
||||
func New(prefix string) string {
|
||||
check(prefix)
|
||||
func New(prefix Prefix) string {
|
||||
out := make([]byte, randomLength)
|
||||
buf := make([]byte, 1)
|
||||
for i := 0; i < len(out); {
|
||||
@@ -53,11 +45,10 @@ func New(prefix string) string {
|
||||
out[i] = alphabet[int(buf[0])%len(alphabet)]
|
||||
i++
|
||||
}
|
||||
return prefix + string(out)
|
||||
return string(prefix) + string(out)
|
||||
}
|
||||
|
||||
func Derive(prefix string, parts ...string) string {
|
||||
check(prefix)
|
||||
func Derive(prefix Prefix, parts ...string) string {
|
||||
sum := sha256.Sum256([]byte(strings.Join(parts, "\x00")))
|
||||
n := binary.BigEndian.Uint64(sum[:8])
|
||||
out := make([]byte, randomLength)
|
||||
@@ -65,11 +56,5 @@ func Derive(prefix string, parts ...string) string {
|
||||
out[i] = alphabet[n%uint64(len(alphabet))]
|
||||
n /= uint64(len(alphabet))
|
||||
}
|
||||
return prefix + string(out)
|
||||
}
|
||||
|
||||
func check(prefix string) {
|
||||
if !prefixes[prefix] {
|
||||
panic(fmt.Sprintf("no id prefix %q", prefix))
|
||||
}
|
||||
return string(prefix) + string(out)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user