First, we should divide a long msg into standard size blocks. Naturally, padding bits or even length field should be appended to this msg before this division. Thus, block size is an important parameter as for different hash methods. Moreover, the final block size is always less than standard block size.
The source codes are as follows. Please attach corresponding head files in test.cpp, eg, #include "sha.h".
SHA().CalculateDigest((byte*)(ciphertext+j*64), (byte*)(plaintext+j*64), 64);
message authentication code (MAC): 1. A bit string that is a function of both data (either plaintext or ciphertext) and a secret key, and that is attached to the data in order to allow data authentication. Note: The function used to generate the message authentication code must be a one-way function. 2. Data associated with an authenticated message allowing a receiver to verify the integrity of the message.
If an attacker does not have the secret key, then even though he is able to modify the message, he cannot produce the matching MAC. Therefore legitimate receiver will detect the alteration.
Digital signatures are in some ways superior because they don't require secret keys to be exchanged. But with MACs, the receiver can't even check to see if a message goes with a particular MAC unless he has the secret key, whereas he can check a message against a digital signature easily. Also, MAC functions are much faster than digital signature functions.
A hash function such as SHA-1 was not designed for use as a MAC and cannot be used directly for that purpose because it does not rely on a secret key.
A MAC method can use different hash methods to produce a hash code before encrypt it. The source code is as follows, we use default key length of 16 bytes:
HMAC<SHA >(passPhrase, 16).CalculateDigest((byte*)(ciphertext+j*64), (byte*)(plaintext+j*64), 64);
String digest
[creation/read, no default] - the name of the
Block MessageDigest on which this MAC is to be based.