php常用hash加密函數(shù)
更新時間:2014年11月22日 14:55:57 投稿:shichen2014
這篇文章主要介紹了php常用hash加密函數(shù),以實例形式詳細分析了PHP的hash加密函數(shù)用法,代碼中備有詳盡的注釋,便于理解,需要的朋友可以參考下
本文實例講述了php常用hash加密函數(shù)。分享給大家供大家參考。具體分析如下:
復制代碼 代碼如下:
$hash_list=hash_algos(); //返回注冊的hash規(guī)則列表
print_r($hash_list); //顯示結(jié)果
創(chuàng)建文件以計算哈希值:file_put_contents('example.txt', 'the quick brown fox jumped over the lazy dog.');
輸出哈希值信息:
復制代碼 代碼如下:
echo hash_file('md5', 'example.txt');
$str="the quick brown fox jumped over the lazy dog."; //定義字符串
echo hash('ripemd160',$str); //生成哈希值
$ctx=hash_init('md5'); //初始化一個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)建一個臨時文件
fwrite($fp,$str); //將字符串寫入到臨時文件
rewind($fp); //倒回文件指針的位置
$ctx=hash_init('md5'); //初始化一個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)建一個文件并將字符串寫入其中*/
$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'); //生成一個包含密鑰的hash值
$ctx=hash_init('sha1'); //定義字符串
hash_update($ctx,'the quick brown fox jumped over the lazy dog.'); //向哈希值中灌注數(shù)據(jù)
echo hash_final($ctx); //輸出結(jié)果
$str="the quick brown fox jumped over the lazy dog."; //定義字符串
echo hash('ripemd160',$str); //生成哈希值
$ctx=hash_init('md5'); //初始化一個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)建一個臨時文件
fwrite($fp,$str); //將字符串寫入到臨時文件
rewind($fp); //倒回文件指針的位置
$ctx=hash_init('md5'); //初始化一個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)建一個文件并將字符串寫入其中*/
$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'); //生成一個包含密鑰的hash值
$ctx=hash_init('sha1'); //定義字符串
hash_update($ctx,'the quick brown fox jumped over the lazy dog.'); //向哈希值中灌注數(shù)據(jù)
echo hash_final($ctx); //輸出結(jié)果
希望本文所述對大家的PHP程序設計有所幫助。
相關文章
PHP中include()與require()的區(qū)別說明
本文主要介紹了PHP中include()與require()的區(qū)別。具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02PHP以指定字段為索引返回數(shù)據(jù)庫所取的數(shù)據(jù)數(shù)組
本文與大家分享幾個使用得PHP編程技巧,有些技巧是在看別人代碼的時候?qū)W來的,有些是自己總結(jié)的,下面為大家介紹下以特定字段為索引,返回數(shù)據(jù)庫取的數(shù)據(jù)數(shù)組,感興趣的朋友可以了解下哈2013-06-06