# TenMinuteTutor

Coding, maths and art

## Common MAC algorithms

Here are some of the most common MAC algorithms.

## Simple MAC (not secure!)

It is possible to use any hash algorithm as the basis of a MAC. We can illustrate this using a simple (though unfortunately not secure) algorithm.

The algorithm uses a secret a key (which can be any sequence of bytes of any length you wish). To calculate the MAC, concatenate the key and the message, and takes the hash value of the concatenated data. This hash value is the MAC. It is then possible to store or transmit the original message and the MAC together. Of course, the message which is stored or transmitted must NOT have the secret key still prepended. To validate the data, repeat the MAC calculation, and ensure that the MAC values match.

An attacker cannot alter the message without detection, because he does not know the key and so cannot recalculate the MAC value. However, the attacker can add blocks to the end of the message, and recalculate the new MAC incrementally. This is a fairly limited attack, but still serious in some cases. It is like signing an important letter and then allowing a total stranger to add any extra data they like to it after you posted it, so that their additions appear to come from you.

## HMAC

A defence against the weakness of the simple algorithm is to append a secret key at the start and end of the message when calculating the MAC. attack. The [[HMAC algorithm]] does something similar to this, but not exactly the same.

HMAC can be used with any hash method. These are identified by adding the hash name, eg HMAC-MD5, HMAC-SHA1 etc.