PHP Extended Hash Module Basic use of the sample code

Source: Internet
Author: User
PHP Extended Hash Module Basic use of the sample code

<?phpecho ' <pre> '; $algos = Hash_algos (); List all supported hash algorithms//Print_r ($algos);//------------------------------------------------------//String hash$data = ' The Quick brown fox jumped over the lazy dog. echo Hash (' MD5 ', $data); MD5 hash $key = ' md5-key '; Echo hash_hmac (' MD5 ', $data, $key); Use the HMAC method to generate a hash value with a key//------------------------------------------------------//File Hash$file = ' hmac.txt '; Echo hash_ File (' MD5 ', $file), Echo hash_hmac_file (' MD5 ', $file, $key);//----------------------------------------------------- -/** * @param $algo hash Algorithm * @param $data String|array string or array of strings * @param optional settings for hashing $options, currently only supported: Hash_hmac. When you specify this option, you must specify the key parameter * @param $key when the options parameter is HASH_HMAC, use this parameter to pass in the shared key for the HMAC hash */function my_hash_data ($algo, $data , $options = 0, $key = null) {//Resource Hash_init (string $algo [, int $options = 0 [, String $key = NULL]]) $ctx = ha Sh_init ($algo, $options, $key), if (is_string ($data)) {hash_update ($ctx, $data),} else if (Is_array ($data)) {foreach ($ Data As $s) {hash_update ($ctx, $s);//Fill data, can be called multiple times, and stitching string effect}}return hash_final ($CTX); Output final data}//test Codeecho my_hash_data (' MD5 ', $data);//------------------------------------------------------/** * File type hash */function my_hash_file ($algo, $filename, $options = 0, $key = NULL) {$ctx = Hash_init ($algo, $options, $key);/* The difference between the two functions: 1. Hash_update_stream The second argument is an open file handle 2. Hash_update_file The second parameter is a filename */hash_update_file ($ctx, $filename); return hash_final ($CTX);} Test Codeecho my_hash_file (' SHA1 ', $file);

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.