From 56773c56aa603f3160f49f8bc221402662546f3a Mon Sep 17 00:00:00 2001 From: Paul Wells Date: Mon, 11 Aug 2025 13:23:40 -0700 Subject: [PATCH 1/2] add hash function to generate common guid --- utils/guid/id.go | 13 ++++++++++++- utils/guid/id_test.go | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/utils/guid/id.go b/utils/guid/id.go index ea1777a17..9f3e92c71 100644 --- a/utils/guid/id.go +++ b/utils/guid/id.go @@ -17,6 +17,7 @@ package guid import ( "crypto/rand" "crypto/sha1" + "crypto/sha256" "fmt" mrand "math/rand/v2" "os" @@ -48,6 +49,7 @@ const ( WHIPResourcePrefix = "WH_" RTMPResourcePrefix = "RT_" URLResourcePrefix = "UR_" + AgentPrefix = "A_" AgentWorkerPrefix = "AW_" AgentJobPrefix = "AJ_" AgentDispatchPrefix = "AD_" @@ -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() @@ -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) { diff --git a/utils/guid/id_test.go b/utils/guid/id_test.go index a03f46b40..c322e3f5c 100644 --- a/utils/guid/id_test.go +++ b/utils/guid/id_test.go @@ -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 From 689a5064d82d7b8b0ed359afc506525774005ee4 Mon Sep 17 00:00:00 2001 From: Paul Wells Date: Mon, 11 Aug 2025 13:36:57 -0700 Subject: [PATCH 2/2] Create nine-melons-appear.md --- .changeset/nine-melons-appear.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/nine-melons-appear.md diff --git a/.changeset/nine-melons-appear.md b/.changeset/nine-melons-appear.md new file mode 100644 index 000000000..d186904b5 --- /dev/null +++ b/.changeset/nine-melons-appear.md @@ -0,0 +1,5 @@ +--- +"@fake-scope/fake-pkg": patch +--- + +add hash function to generate common guid