1. What is the MESSAGE authentication code

Message Authorization Code, a three-word acronym known simply as MAC, is a technique for verifying integrity and authentication

Sender :((raw data) + secret key) * hash function = hash value (message authentication code)

  • Send raw data and message authentication codes

Receiver:

  • Receive message authentication code and raw data

(Receive raw data + secret key) * hash function = new hash value =? Received hash value (message authentication code)

The secret keys must be the same, and the hash functions used must be the same

2. Use the message authentication code in GO

package main

import (
	"crypto/hmac"
	"crypto/sha256"
)

func GenerateHamc(plainText, key []byte) []byte {
	//1. Initialize a hash interface and specify the hash algorithm and secret key to use
	hMAC := hmac.New(sha256.New, key)
	2. Add data
	hMAC.Write(plainText)
	/ / 3. The calculation
	sum := hMAC.Sum(nil)
	return sum
}

func VerityHmac(plainText, key, hMACOld []byte) bool {
	//1. Initialize a hash interface and specify the hash algorithm and secret key to use
	hMAC := hmac.New(sha256.New, key)
	//2. Add raw data
	hMAC.Write(plainText)
	//3. Calculate the hash value
	hMacNew := hMAC.Sum(nil)
	//4. Compare message authentication codes
	res := hmac.Equal(hMacNew, hMACOld)
	return res
}

func main(a) {
	src := []byte("Message Authorization Code is a three-word acronym, MAC, for verifying integrity and authentication.")
	key := []byte("12345678")
	hMacOld := GenerateHamc(src, key)
	res := VerityHmac(src, key, hMacOld)
	fmt.Printf("Message authentication result: %t\n", res)
}
Copy the code

3. The message authentication code is faulty

Disadvantages:

  • Difficult shared key distribution problem (can be solved asymmetrically)

Unsolvable problems

  • Cannot be verified by a third party
  • Can’t prevent denial