From 1f308c130bd6129c6b74729451cf8c7d70244043 Mon Sep 17 00:00:00 2001 From: siddharth Date: Mon, 11 Oct 2021 11:06:19 -0400 Subject: lib: add Mac and MacVerify for md4 --- lib/md4.go | 13 +++++++++++++ lib/md4_test.go | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+) (limited to 'lib') diff --git a/lib/md4.go b/lib/md4.go index 632f6da..eab3073 100644 --- a/lib/md4.go +++ b/lib/md4.go @@ -251,3 +251,16 @@ func (md *Md4) Hash() []byte { return d } + +func (md *Md4) Mac(secret, msg []byte) []byte { + md.Message(append(secret, msg...)) + return md.Hash() +} + +func (md *Md4) MacVerify(secret, msg, mac []byte) bool { + md.Message(append(secret, msg...)) + if BytesEqual(md.Hash(), mac) { + return true + } + return false +} diff --git a/lib/md4_test.go b/lib/md4_test.go index 31b7b31..8f832b8 100644 --- a/lib/md4_test.go +++ b/lib/md4_test.go @@ -56,3 +56,23 @@ func TestMd4Hash(t *testing.T) { } } + +func TestMd4MacVerify(t *testing.T) { + md4 := Md4{} + md4.Init([]uint32{}) + + sec := StrToBytes("honey") + msg := StrToBytes("abc") + + // Test Mac + mac := md4.Mac(sec, msg) + e := "22fd3b98ca4a901fec432ed2c49d3f83" // Expected Mac + if BytesToHexStr(mac) != e { + t.Errorf("Error: md5 mac failed %x != %s\n", mac, e) + } + + // Test MacVerify. + if !md4.MacVerify(sec, msg, mac) { + t.Errorf("Error: mac verification failed\n") + } +} -- cgit v1.2.3