dissertation-2-code/proxy/packet.go
Jake Hillion 75f02802ad
Some checks failed
continuous-integration/drone/push Build is failing
Refactored for multiple macverifiers/generators
Tests not fixed yet.
2021-05-13 21:56:25 +01:00

40 lines
663 B
Go

package proxy
import "mpbl3p/shared"
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 {
mac := g.Generate(b)
return append(b, mac...)
}
func StripMac(b []byte, v MacVerifier) ([]byte, error) {
if len(b) < v.CodeLength() {
return nil, shared.ErrNotEnoughBytes
}
data := b[:len(b)-v.CodeLength()]
sum := b[len(b)-v.CodeLength():]
if err := v.Verify(data, sum); err != nil {
return nil, err
}
return data, nil
}