<?phpecho '<md5', $data); //md5 哈希$key = 'md5-key';echo hash_hmac('md5', $data, $key); //使用 HMAC 方法生成帶有密鑰的哈希值// ------------------------------------------------------// 文件hash$file = 'hmac.txt';echo hash_file('md5', $file);echo hash_hmac_file('md5', $file, $key);// ------------------------------------------------------/** * @param $algo hash算法 * @param $data string|array 字符串或者字符串數組 * @param $options 進行哈希運算的可選設置,目前僅支持:HASH_HMAC。當指定此選項時,必須指定 key 參數 * @param $key 當 options 參數為 HASH_HMAC 時,使用此參數傳入進行 HMAC 哈希運算時的共享密鑰 */function my_hash_data($algo, $data, $options = 0, $key = NULL) { // resource hash_init ( string $algo [, int $options = 0 [, string $key = NULL ]] ) $ctx = hash_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); //填充數據, 可以多次調用, 和拼接字符串效果一樣 } } return hash_final($ctx); //輸出最后的數據}// test codeecho my_hash_data('md5', $data);// ------------------------------------------------------/** * 文件類型 hash */function my_hash_file($algo, $filename, $options = 0, $key = NULL) { $ctx = hash_init($algo, $options, $key); /* 兩個函數的不同之處: 1. hash_update_stream 第二個參數是一個打開的文件句柄 2. hash_update_file 第二個參數是一個文件名 */ hash_update_file($ctx, $filename); return hash_final($ctx);}// test codeecho my_hash_file('sha1', $file);
新聞熱點
疑難解答
圖片精選