Algorithm
This is an implementation of the MD2 hash algorithm. This algorithm can hash data of any given length.
Code Documentation
- MD2_CTX
A MD2 object that will hold all hash-related data and calculations as the hash is calculated. - md2_init(MD2_CTX *ctx)
Initialize the MD2_CTX object. - md2_update(MD2_CTX *ctx, unsigned char data[], int len)
Once an object has been created and initialized, the data to be hashed must be added. Due to practical limitations, it may not be optimal (or possible) to add all the data to the MD2 hash in one data chunk, so the function inputs, stores, and calculates data as it is received, allowing the data to be added in as many chunks as necessary.
- unsigned char data[]
This is the data to be added to the hash. - int len
This is the length, in bytes, of the data in the “data” array.
- unsigned char data[]
- md2_final(MD2_CTX *ctx, unsigned char hash[])
Finalize and output the hash. - unsigned char hash[]
This is the array to store the output hash. It must be at least 16 bytes in size.
Code Usage
- Create an MD2_CTX object.
- Initialize it with md2_init().
- Read some/all of the data to hash into an array, calculate the size of the data, and add it to the hash with md2_update().
- Repeat the previous step for all the data you want to hash.
- Finalize and output the hash with md2_final().
Code
• Source Code • Sample Driver Program
Notes
This implementation assumes little endian byte ordering (as the MD2 standard states it should). This algorithm not only has not been optimized, although some general attempts have been made to that effect. This implementation of MD2 has been tested against numerous test vectors (including all official ones) and has proved to be accurate.
Note that MD2 is not considered a secure hash algorithm. It is recommended that a hash algorithm such as Whirlpool or SHA-1 be used, or even MD5, as MD2 is grossly outdated.
