VC ++ network security programming example (7)-implement hash Digest algorithm

Source: Internet
Author: User
Tags md5 hash

hash algorithm maps binary values of any length to smaller binary values of a fixed length, this small binary value is called a hash value. A hash value is a unique and extremely compact numeric representation of a piece of data. If a piece of plain text is hashed and only one letter of the paragraph is modified, the subsequent hash will generate different values. It is impossible to calculate two different inputs with the same hash value. Therefore, the hash value of the data can be used to check the integrity of the data. A hash table maps a set of keywords to a limited address range based on the set hash function H (key) and the method for handling conflicts, the storage location of a table is recorded by the keyword in the address range. Such a table is called a hash table or a hash table, and the obtained storage location is called a hash address or a hash address. As a linear data structure, hash tables are faster than tables and queues. Hash results are obtained by applying a one-way mathematical function (sometimes called a "hash algorithm") to any amount of data. If the input data changes, the hash will also change. Hash can be used for many operations, including identity authentication and digital signature. It is also called "Message Digest ".

Algorithm used to generate hash values of some data fragments (such as messages or session items. By using a good hash algorithm, all the bits in the hash value of the result can be changed by making changes in the input data. Therefore, hash is useful for detecting changes in data objects (such as messages. In addition, a good hash algorithm makes it impossible to construct two mutually independent inputs with the same hash. Typical hash algorithms include md2, md4, MD5, and SHA-1. A hash algorithm is also called a "hash function ". For more information, see HMAC, md2, md4, MD5, message digest, and SHA-1) MD5 is a one-way 128-bit hash solution that complies with industrial standards. It consists of RSA Data Security, Inc. development. Various "Point-to-Point Protocol (PPP)" vendors use it for encrypted authentication. The hash scheme is a method that converts data (such as passwords) in a unique result and cannot return to its original format. The Challenge Handshake Authentication Protocol (CHAP) uses the question response and one-way MD5 hash method in response. In this way, you do not need to send a password over the network to prove to the server that you know the password. A question response verification protocol for a question handshake (CHAP) "Point-to-Point Protocol (PPP)" connection, as described in RFC 1994. This protocol uses the industry-standard MD5 hash algorithm to hash a query string (published by the authentication server) and a combination of user passwords in the response.

 

# Include <stdio. h> <br/> # include <string. h> <br/> # include <OpenSSL/EVP. h> </P> <p> void main (INT argc, char * argv []) <br/> {<br/> evp_md_ctx mdctx; <br/> const evp_md * md; <br/> // array of messages to be calculated <br/> char msgs [] [64] = {"it's just for test ", <br/> "Author: Jian Shen", "Hello world from OpenSSL "}; <br/> // Digest algorithm name <br/> const char * digestname = "MD5 "; // "sha1" <br/> // const char * digestname = "sha1"; </P> <p> unsigned Char md_value [evp_max_md_size]; <br/> int md_len, I; </P> <p> openssl_add_all_digests (); <br/> // obtain the abstract algorithm object based on the abstract name <br/> MD = evp_get_digestbyname (digestname); <br/> If (! MD) {<br/> printf ("Incorrect Digest algorithm name: % s \ n", digestname); <br/> exit (1 ); <br/>}< br/> evp_md_ctx_init (& mdctx); <br/> // initialize Digest Calculation <br/> evp_digestinit (& mdctx, MD ); <br/> // Add the data to calculate the summary in a loop <br/> for (I = 0; I <sizeof (MSGs)/sizeof (MSGs [0]); I ++) <br/>{< br/> evp_digestupdate (& mdctx, // digest computing context <br/> msgs [I], // information to be added <br/> strlen (MSGs [I]) // information length <br/> ); <br/>}< br/> // end the Digest Calculation and output the digest value. md_len is the length of the digest value. <br/> evp_digestfinal (& mdctx, md_value, & md_len); <br/> evp_md_ctx_cleanup (& mdctx); </P> <p> printf ("the digest value is: (type = % s, length = % d bytes): ", digestname, md_len); <br/> for (I = 0; I <md_len; I ++) printf (" % 02x ", md_value [I]); <br/> printf ("\ n"); </P> <p> printf ("\ n click any key to continue. "); <br/> // It is equivalent to pausing. Observe the <SPAN class = 'wp _ keywordlink'> Program </span> running result <br/> getchar (); <br/>}< br/>

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.