Skip to content

Commit

Permalink
userhash: don't use pointers for no reason
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrtronium committed Nov 21, 2024
1 parent ac62ba9 commit f6dfe4e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions privmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions userhash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
4 changes: 2 additions & 2 deletions userhash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit f6dfe4e

Please sign in to comment.