Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nine-melons-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fake-scope/fake-pkg": patch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably going to fail CI in the next PR?

---

add hash function to generate common guid
13 changes: 12 additions & 1 deletion utils/guid/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package guid
import (
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"fmt"
mrand "math/rand/v2"
"os"
Expand Down Expand Up @@ -48,6 +49,7 @@ const (
WHIPResourcePrefix = "WH_"
RTMPResourcePrefix = "RT_"
URLResourcePrefix = "UR_"
AgentPrefix = "A_"
AgentWorkerPrefix = "AW_"
AgentJobPrefix = "AJ_"
AgentDispatchPrefix = "AD_"
Expand All @@ -70,6 +72,11 @@ func New(prefix string) string {
return g.New(prefix)
}

func Hash(prefix string, data []byte) string {
g := newGeneratorFromSeed(sha256.Sum256(data))
return g.New(prefix)
}

// HashedID creates a hashed ID from a unique string
func HashedID(id string) string {
h := sha1.New()
Expand Down Expand Up @@ -108,9 +115,13 @@ func newGenerator() (*guidGenerator, error) {
return nil, err
}

return newGeneratorFromSeed(seed), nil
}

func newGeneratorFromSeed(seed [32]byte) *guidGenerator {
return &guidGenerator{
rng: mrand.NewChaCha8(seed),
}, nil
}
}

func (g *guidGenerator) readIDChars(b []byte) {
Expand Down
5 changes: 5 additions & 0 deletions utils/guid/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func TestMarshalAppend(t *testing.T) {
require.Equal(t, id1, Unmarshal[livekit.RoomID](livekit.GuidBlock(b[9:])))
}

func TestHash(t *testing.T) {
id := Hash(AgentPrefix, []byte("test"))
require.Equal(t, "A_SFo4igEG5Dg5", id)
}

func BenchmarkNew(b *testing.B) {
b.Run("new", func(b *testing.B) {
var guid string
Expand Down
Loading