All checks were successful
Test / Create distribution (push) Successful in 26s
Test / Sandbox (push) Successful in 1m44s
Test / Fortify (push) Successful in 2m37s
Test / Sandbox (race detector) (push) Successful in 2m59s
Test / Fpkg (push) Successful in 3m34s
Test / Fortify (race detector) (push) Successful in 4m6s
Test / Flake checks (push) Successful in 59s
This reduces the scope of the fst package, which was growing questionably large. Signed-off-by: Ophestra <cat@gensokyo.uk>
49 lines
665 B
Go
49 lines
665 B
Go
package app
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
type ID [16]byte
|
|
|
|
var (
|
|
ErrInvalidLength = errors.New("string representation must have a length of 32")
|
|
)
|
|
|
|
func (a *ID) String() string {
|
|
return hex.EncodeToString(a[:])
|
|
}
|
|
|
|
func NewAppID(id *ID) error {
|
|
_, err := rand.Read(id[:])
|
|
return err
|
|
}
|
|
|
|
func ParseAppID(id *ID, s string) error {
|
|
if len(s) != 32 {
|
|
return ErrInvalidLength
|
|
}
|
|
|
|
for i, b := range s {
|
|
if b < '0' || b > 'f' {
|
|
return fmt.Errorf("invalid char %q at byte %d", b, i)
|
|
}
|
|
|
|
v := uint8(b)
|
|
if v > '9' {
|
|
v = 10 + v - 'a'
|
|
} else {
|
|
v -= '0'
|
|
}
|
|
if i%2 == 0 {
|
|
v <<= 4
|
|
}
|
|
id[i/2] += v
|
|
}
|
|
|
|
return nil
|
|
}
|