Hash Algorithms

1. Hash

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);

2. Hash Method List
 

    1)SHA1

Digest length:
20 bytes.
Block size:
64 bytes.
Max. final block size:
55 bytes.
State size:
20 bytes.

2)MD5

Digest length:
16 bytes.
Block size:
64 bytes.
Max. final block size:
55 bytes.
State size:
16 bytes.
 

3) RIPEMD160

Digest length:
20 bytes.
Block size:
64 bytes.
Max. final block size:
55 bytes.
State size:
20 bytes.
 

3. Message Authentication Code

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);

1)HMAC

Parameters:
Key length:
Any multiple of 8 bits that does not cause the maximum input length for the MessageDigest to be exceeded. Default 128 bits.
Output length:
Minimum 32 bits, maximum equal to the message digest output length. The default is equal to the message digest output length.