這篇文章主要介紹了php常用hash加密函數(shù),以實(shí)例形式詳細(xì)分析了PHP的hash加密函數(shù)用法,代碼中備有詳盡的注釋,便于理解,需要的朋友可以參考下
本文實(shí)例講述了php常用hash加密函數(shù)。分享給大家供大家參考。具體分析如下:
復(fù)制代碼代碼如下:
$hash_list=hash_algos(); //返回注冊(cè)的hash規(guī)則列表
print_r($hash_list); //顯示結(jié)果
創(chuàng)建文件以計(jì)算哈希值:file_put_contents('example.txt', 'the quick brown fox jumped over the lazy dog.');
輸出哈希值信息:
復(fù)制代碼代碼如下:
echo hash_file('md5', 'example.txt');
$str="the quick brown fox jumped over the lazy dog."; //定義字符串
echo hash('ripemd160',$str); //生成哈希值
$ctx=hash_init('md5'); //初始化一個(gè)hash值
hash_update($ctx,'the quick brown fox'); //向哈希值灌注數(shù)據(jù)
hash_update($ctx,'jumped over the lazy dog.'); //向哈希值灌注數(shù)據(jù)
echo hash_final($ctx); //輸出最后的結(jié)果
$str="the quick brown fox jumped over the lazy dog."; //定義字符串
$fp=tmpfile(); //創(chuàng)建一個(gè)臨時(shí)文件
fwrite($fp,$str); //將字符串寫入到臨時(shí)文件
rewind($fp); //倒回文件指針的位置
$ctx=hash_init('md5'); //初始化一個(gè)hash值
hash_update_stream($ctx,$fp); //向數(shù)據(jù)流中灌注數(shù)據(jù)
echo hash_final($ctx); //輸出結(jié)果
$str="the quick brown fox jumped over the lazy dog."; //定義字符串
echo hash_hmac('ripemd160',$str,'secret'); //生成包含密鑰的hash值
/*創(chuàng)建一個(gè)文件并將字符串寫入其中*/
$file="example.txt"; //定義文件名
$str=" the quick brown fox jumped over the lazy dog."; //定義字符串
file_put_contents($file,$str); //向文件中寫入字符串
echo hash_hmac_file('md5',$file,'secret'); //生成一個(gè)包含密鑰的hash值
$ctx=hash_init('sha1'); //定義字符串
hash_update($ctx,'the quick brown fox jumped over the lazy dog.'); //向哈希值中灌注數(shù)據(jù)
echo hash_final($ctx); //輸出結(jié)果
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。