PHP Hash cipher Function Sample code

Source: Internet
Author: User
Tags rewind
A hash is the conversion of the target text into an irreversible hash string (or message digest) of the same length, whereas encryption (Encrypt) converts the target text into a reversible cipher with different lengths. This article mainly introduces PHP commonly used hash encryption function, in the case of a detailed analysis of PHP hash encryption function usage, code with detailed comments, easy to understand, the need for friends can refer to, The specific analysis is as follows:

$hash _list=hash_algos ();  Returns the list of registered hash rules Print_r ($hash _list); Show results

Create a file to calculate the hash value: file_put_contents (' example.txt ', ' The quick brown fox jumped over the the lazy Dog. ');

Output hash Value information:

echo hash_file (' MD5 ', ' example.txt ');      $str = "The quick brown fox jumped over the lazy dog.";           Defines the string echo hash (' ripemd160 ', $str);          Generates a hash value $ctx =hash_init (' MD5 ');       Initialize a hash value of hash_update ($ctx, ' The quick brown fox ');      Pour the data into the hash value hash_update ($ctx, ' jumped over the lazy dog ');          Perfusion data to the hash echo hash_final ($CTX);    The final result of the output $STR = "The quick brown fox jumped over the the lazy dog.";            Defines the string $fp =tmpfile ();            Create a temporary file fwrite ($fp, $STR);            Writes a string to a temporary file rewind ($FP);          Rewind the position of the file pointer $ctx =hash_init (' MD5 ');         Initializes a hash value of Hash_update_stream ($ctx, $fp);          Perfusion data into the data flow echo hash_final ($CTX);    The Output $str = "The quick brown fox jumped over the the lazy dog.";      Defines the string echo hash_hmac (' ripemd160 ', $str, ' secret ');          Generate a hash value that contains the key/* Create a file and write the string to it */$file = "Example.txt";   Define the file name $str = "The quick brown fox jumped over the the lazy dog.";        Defines the string file_put_contents ($file, $STR); Writing a string to a file echo Hash_hmac_filE (' MD5 ', $file, ' secret ');          Generates a hash value that contains the key $ctx =hash_init (' SHA1 ');  Define the string hash_update ($ctx, ' The quick brown fox jumped over the the lazy Dog. ');  Perfusion data into hashes echo hash_final ($CTX); Output results
Related Article

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.