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 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