]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/cypherpunks.ru/govpn/identity.go
Do not allocate memory in each iteration
[govpn.git] / src / cypherpunks.ru / govpn / identity.go
index 09f7e337b361bb629f1213616567048d5108e9b1..89b12ab5cb118687415ef05c0cbbed79cbd10a9c 100644 (file)
@@ -106,6 +106,7 @@ func (mc *MACCache) Find(data []byte) *PeerId {
                return nil
        }
        buf := make([]byte, 8)
+       sum := make([]byte, 32)
        mc.l.RLock()
        for pid, mt := range mc.cache {
                copy(buf, data)
@@ -113,9 +114,9 @@ func (mc *MACCache) Find(data []byte) *PeerId {
                mt.l.Lock()
                mt.mac.Reset()
                mt.mac.Write(buf)
-               mt.mac.Sum(buf[:0])
+               mt.mac.Sum(sum[:0])
                mt.l.Unlock()
-               if subtle.ConstantTimeCompare(buf, data[len(data)-8:]) == 1 {
+               if subtle.ConstantTimeCompare(sum[len(sum)-8:], data[len(data)-8:]) == 1 {
                        ppid := PeerId(pid)
                        mc.l.RUnlock()
                        return &ppid