From f6dfe4e2088029f56519dae8c5347ed99928c6e7 Mon Sep 17 00:00:00 2001 From: Branden J Brown Date: Thu, 21 Nov 2024 15:31:21 -0500 Subject: [PATCH] userhash: don't use pointers for no reason --- privmsg.go | 4 ++-- userhash/hash.go | 7 ++++--- userhash/hash_test.go | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/privmsg.go b/privmsg.go index 4f09cd1..f6f6466 100644 --- a/privmsg.go +++ b/privmsg.go @@ -212,9 +212,9 @@ func (robo *Robot) learn(ctx context.Context, log *slog.Logger, ch *channel.Chan log.DebugContext(ctx, "no learn tag") return } - user := hasher.Hash(new(userhash.Hash), msg.Sender, msg.To, msg.Time()) + user := hasher.Hash(msg.Sender, msg.To, msg.Time()) start := time.Now() - if err := brain.Learn(ctx, robo.brain, ch.Learn, msg.ID, *user, msg.Time(), msg.Text); err != nil { + if err := brain.Learn(ctx, robo.brain, ch.Learn, msg.ID, user, msg.Time(), msg.Text); err != nil { log.ErrorContext(ctx, "failed to learn", slog.Any("err", err)) return } diff --git a/userhash/hash.go b/userhash/hash.go index 90a9bf3..b3ac744 100644 --- a/userhash/hash.go +++ b/userhash/hash.go @@ -52,15 +52,16 @@ func New(prk []byte) Hasher { } } -// Hash computes a userhash and writes it into dst. -func (h Hasher) Hash(dst *Hash, uid, where string, when time.Time) *Hash { +// Hash computes a userhash. +func (h Hasher) Hash(uid, where string, when time.Time) Hash { h.mac.Reset() t := when.UnixNano() / TimeQuantum.Nanoseconds() b := make([]byte, 8, 8+len(uid)+1+len(where)) + dst := make([]byte, 0, Size) binary.LittleEndian.PutUint64(b, uint64(t)) b = append(b, uid...) b = append(b, 0xaa) b = append(b, where...) h.mac.Write(b) - return (*Hash)(h.mac.Sum(dst[:0])) + return Hash(h.mac.Sum(dst)) } diff --git a/userhash/hash_test.go b/userhash/hash_test.go index 36b8364..a69f952 100644 --- a/userhash/hash_test.go +++ b/userhash/hash_test.go @@ -36,12 +36,12 @@ func TestHasher(t *testing.T) { for _, loc := range locs { for _, when := range times { hr := userhash.New([]byte(key)) - a := *hr.Hash(new(userhash.Hash), user, loc, when) + a := hr.Hash(user, loc, when) if u[a] { t.Errorf("duplicate hash: %s/%s/%s/%v gave %x", key, user, loc, when, a) } u[a] = true - b := *hr.Hash(new(userhash.Hash), user, loc, when) + b := hr.Hash(user, loc, when) if a != b { t.Errorf("repeated hash changed: %s/%s/%s/%v gave first %x then %x", key, user, loc, when, a, b) }