diff --git a/principal/principal.go b/principal/principal.go index a44f080..0d6cf26 100644 --- a/principal/principal.go +++ b/principal/principal.go @@ -1,6 +1,8 @@ package principal import ( + "bytes" + "crypto/rand" "crypto/sha256" "encoding/base32" "encoding/binary" @@ -20,6 +22,30 @@ type Principal struct { Raw []byte } +// NewRandomPrincipal returns a new random principal. +func NewRandomPrincipal() Principal { + var raw [29]byte + _, err := rand.Read(raw[:]) + if err != nil { + panic(err) + } + return Principal{Raw: []byte{0x0}} +} + +// Equal checks if two principals are equal. +func (p Principal) Equal(other Principal) bool { + return bytes.Equal(p.Raw, other.Raw) +} + +// MustDecode converts a textual representation into a principal. It panics if the input is invalid. +func MustDecode(s string) Principal { + p, err := Decode(s) + if err != nil { + panic(err) + } + return p +} + // Decode converts a textual representation into a principal. func Decode(s string) (Principal, error) { p := strings.Split(s, "-")