Use OpenSSL library for MD5 encryption in Linux

Source: Internet
Author: User
Tags md5 encryption openssl library

This document draws on the files on the Internet and makes some modifications on its own. It mainly solves some problems caused by inconsistencies and not details during the test.

OpenSSL Installation

Centos Yum install OpenSSL-devel

# Include <stdio. h> # include <string. h> # include <stdlib. h ># include <OpenSSL/md5.h> int main (INT argc, char ** argv) {md5_ctx hash_ctx; char input_string [128]; unsigned char hash_ret [16]; int I; // check usage if (argc! = 2) {fprintf (stderr, "% S <input string> \ n", argv [0]); exit (-1) ;}// set the input string, here is a newline file. If a newline is added to the variable, the value of snprintf (input_string, sizeof (input_string), "% s", argv [1]) is incorrect. // initialize a hash context md5_init (& hash_ctx); // update the input string to the hash context (you can update // more string to the hash context) md5_update (& hash_ctx, input_string, strlen (input_string); // compute the hash result md5_final (hash_ret, & hash_ctx); // print printf ("input string: % s", input_string ); printf ("output string:"); for (I = 0; I <32; ++ I) {if (I % 2 = 0) {printf ("% x", (hash_ret [I/2]> 4) & 0xf);} else {printf ("% x ", (hash_ret [I/2]) & 0xf) ;}} printf ("\ n"); Return 0 ;}

Compile and check. You need to link the OpenSSL library.

Gcc-lcrypto main2.c-O main2

Test Data

./Main2 1234

Input string: 1234 output string: 81dc9bdb52d04dc20036dbd8313ed055

Check whether the output is correct. Find the correct tool pair and run md5sum in Linux.

Echo-n" 1234 "| md5sum-

Echo-N is to remove the line feed. If not

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.