Skip to content

Commit

Permalink
Add principal utility funcs.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed May 6, 2024
1 parent fbd56e7 commit 601b5bb
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions principal/principal.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package principal

import (
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/base32"
"encoding/binary"
Expand All @@ -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, "-")
Expand Down

0 comments on commit 601b5bb

Please sign in to comment.