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);