dissertation-2-code/proxy/packet.go
Jake Hillion 8e9310fb02
All checks were successful
continuous-integration/drone/push Build is passing
added timestamp todo
2020-11-29 22:07:30 +00:00

47 lines
790 B
Go

package proxy
import (
"encoding/binary"
"time"
)
type Packet interface {
Marshal() []byte
Contents() []byte
}
type SimplePacket []byte
// get the raw data of the IP packet
func (p SimplePacket) Marshal() []byte {
return p
}
func (p SimplePacket) Contents() []byte {
return p
}
func AppendMac(b []byte, g MacGenerator) []byte {
footer := make([]byte, 8)
unixTime := uint64(time.Now().Unix())
binary.LittleEndian.PutUint64(footer, unixTime)
b = append(b, footer...)
mac := g.Generate(b)
return append(b, mac...)
}
func StripMac(b []byte, v MacVerifier) ([]byte, error) {
data := b[:len(b)-v.CodeLength()]
sum := b[len(b)-v.CodeLength():]
if err := v.Verify(data, sum); err != nil {
return nil, err
}
// TODO: Verify timestamp
return data[:len(data)-8], nil
}