Note: Use the MD5 Algorithm in Linux

Source: Internet
Author: User
MD5 is required in the project. Algorithm So I found several MD5 algorithm source code files on the Internet, and the result interfaces are different, which is a headache. Turning over the/usr/include directory in Linux, we found that md5.h is provided under/usr/include/OpenSSL/, So we wrote a small Code Tested:
# Include < Stdio. h >
# Include < OpenSSL / Md5.h >
# Include < Assert. h >
# Include < String . H >

Void Printhex ( Char * Buffer, Int Size, file * Fp = Stderr)
... {
Assert (null ! = Buffer );
Fprintf (FP, " ========================================================== ================== " );
Char * P = NULL;
Int I;
For (I = 0 , P = Buffer; I < Size; I ++ , P ++ )
... {
Fprintf (FP, " % 02x " , (Unsigned Char ) * P );
If (I % 16 = 15 )
... {
Fprintf (FP," ");
}
}
Fprintf (FP, " ========================================================== ================== " );
}

Void Test ()
... {
Char * Str =   " It's a test " ;
Int Len = Strlen (STR );
Char Buf [ 16 ] =   ... {0} ;
Char * P = ( Char * ) MD5 (unsigned Char * ) STR, Len, (unsigned Char * ) BUF );
Printhex (BUF, Sizeof (BUF ));
Printhex (p, 16 );
}

Int Main ()
... {
Test ();
Return 1;
}

/**/ /*
G ++-O testmd5.o-C testmd5.cpp-g-wall-werror
G ++-O testmd5 testmd5.o/usr/lib/libssl. So
*/

Note: Link/usr/lib/libssl. So when linking

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.