Target platform: LOONGSON-1B Development Board
Kernel: Linux 3.0
Compiling platform: ubuntu10.04
Cross tool chain: gcc-3.4.6-2f
Note: The following actions are performed under root user
Brief Introduction
This article is intended to provide technical documentation for MD5 porting and MD5 encryption programming under Linux C.
MD5 (Message Digest algorithm 5: The fifth edition), a hash function widely used in the field of computer security to provide integrity protection for messages, is one of the most widely used hashing algorithms (also translated digest algorithm, hashing algorithm).
The MD5 algorithm receives a string of any length and then hashes it into a 128-bit encoding. It is widely used in all kinds of computer encryption fields.
1. Install MD5 Tools
First download MD5 tool Source:
Entry Address: http://martin.hinner.info/libmd/
Download Libmd Library libmd-0.3.tar.bz2
Install Libmd tools in ubuntu10.04:
#tar JXF libmd-0.3.tar.bz2
#cd libmd-0.3
#./configure
#make
#make Install
Ubuntu10.04 Complete the MD5 tool installation.
2. Cross-compile MD5 tools
Note: If porting to arm platform, then change accordingly to Arm-linux
#cd libmd-0.3
#./configure--host=mipsel=mipsel-linux--prefix=/home/md5_install
#vi Makefile
Open Makefile and modify:
Modify CC = gcc to cc = MIPSEL-LINUX-GCC
Modify Ranlib = Ranlib to Ranlib = Mipsel-linux-ranlib
Change ar = ar to AR = Mipsel-linux-ar
Add a statement under install =/USR/BIN/INSTALL-C statement:
BuildRoot =/home/md5_install
Save Exit Makefile
#make
#make Install
After installation, create usr/include/, usr/lib/, Usr/man folders under the directory/home/md5_install/, which will be generated by the cross compilation in/home/md5_install/usr/lib/ libmd.so.1.0 move to the/lib directory of the target platform to complete the MD5 transplant.
3, MD5 programming
LIBMD provides a series of C function interfaces through which MD5 encryption can be easily implemented.
The Md5.h file provides an important structural body struct md5context in MD5 programming, and its prototype is as follows:
struct md5context{
u_int32_t Buf[4];
u_int32_t bits[2];
Unsigned Char in[64];
}md5_ctx;
This type of structure runs through the entire MD5 programming.
3.1 MD5 Common function interface
#include <sys/types.h>
#include <md5.h>
void Md5init (Md5_ctx *context);
void Md5update (Md5_ctx *context, const void *data, unsigned int len);
void Md5pad (Md5_ctx *context);
void md5final (unsigned char digest[16], md5_ctx *context);
Char *md5end (md5_ctx *context, Char *buf);
Char *md5file (const char *filename, char *buf);
Char *md5filechunk (const char *filename, char *buf, off_t offset, off_t lenghth);
Char *md5data (const void *data, unsigned int len, char *buf);
Function Description:
Md5init (), Md5update () and Md5final (): Is the core function, MD5 programming, first assign a MD5_CTX structure space, use Md5init () to initialize the structure, use Md5update () to update it, Finally, the results are extracted using md5final ().
Md5pad (): Similar to Md5final (), the difference is that it does not terminate the calculation.
Md5end (): Is the encapsulation of the md5final () function, which converts the converted 1286 encoding into a string of ASCII code with 33 characters (containing the Terminator ' ").
Md5file (): Calculates a summary of the file and returns the result using Md5end (). Returns a null pointer if the specified file cannot be opened.
Md5filechunk (): A function is similar to Md5file (), but it only calculates a file summary that specifies the offset in the file to start to the length of the subsequent byte. If length is 0, or is greater than the remaining lengths after the offset, md5filechunk () calculates the summary of the file from the offset to the end of the file.
4, MD5 Encryption Program example:
/*md5.c*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <md5.h>
int main (void)
{
unsigned char passwd[10] = {0};
unsigned char sout[md5_hashbytes] = {0};
int i;
Md5_ctx CTX;
memset (&ctx, 0, sizeof (MD5_CTX));
Md5init (&CTX);
Md5update (&ctx, (unsigned char *) passwd,sizeof (passwd));
Md5final ((unisned char *) sout, &ctx);
printf ("MD5 Code is:\n");
for (i = 0; i < md5_hashbytes, i++)
{
printf ("%02x", Sout[i]);
printf ("\ n");
return 0;
}
Ubuntu Local compilation and Operation:
#gcc-O MD5 MD5.C-LMD
#./md5
MD5 code is:
C5 6b d5 0f 6e (CB 6e a0 AD) 3a
The resulting field is passwd 128-bit encoded after MD5 encryption.
Cross-compile and run:
#mipsel-linux-gcc-i/home/md5_install/usr/include-o MD5 MD5.C-L/HOME/MD5_INSTALL/USR/LIB-LMD
MD5 the executable file to the target platform to get the same encryption code as above.