我發現有網上寫關於這個話題相當不錯的教程。我不太記得在谷歌,我發現它,但讓我看看,如果我能打破功能下降不夠好自己,因爲它是正確的在我的面前......
首先的功能,它可以創建一個鍵任何大小的長度。我把它發表評論相當大量的自由......
它
function pbkdf2($password,$salt,$iter_count = 1500,$key_length = 32,$algorithm = 'sha512')
{
/*
@param string password -- password to be encrypted
@param string salt -- salt to encrypt with
@param int iter_count -- number of times to iterate blocks
@param key_length -- length of key to return
@param $algorithm -- algorithm to use in hashing
@return string key
*/
//determine the length of the hahs
$hash_length = strlen(hash($algorithm,NULL,TRUE));
//determine the number of key blocks to compute
$key_blocks = ceil($key_length/$hash_length);
//initialize key
$key = '';
//create the key itself
//create blocks
for($block_count = 1;$block_count <= $key_blocks;$block_count++)
{
//initalize hash for this block
$iterated_block = $block = hash_hmac($algorithm,$salt.pack('N',$block_count),$password,TRUE);
//iterate blocks
for($iterate = 1;$iterate <= $iter_count;$iterate++)
{
//xor each iterate
$iterated_block ^= ($block = hash_hmac($algorithm,$block,$password,TRUE));
}
//append iterated block
$key .= $iterated_block;
}
//return the key
return substr($key,0,$key_length);
}
- 第一件事情就是找出散列的長度。
- 接着它確定密鑰塊多少所需的密鑰長度指定
- 然後,它初始化散列(鍵)以返回
- 設置for循環將創建每個塊
- 取初始散列與附加到鹽
- 二進制塊計數器塊的開始循環迭代的塊$ iter_count倍(創建其自身的散列)
- XOR每個迭代並將它附加到iterated_block $(XOR前一哈希到當前)
- XOR循環結束
- 追加$ iterated_block至$鍵每個塊
- 塊循環結束
- 回報的關鍵
我覺得這可能是做到這一點的最好辦法。也許我太偏執了?
你現時應該是鹽。根據定義,鹽是隨機的。我已經寫在這裏醃製長回答:http://stackoverflow.com/questions/1645161/salt-generation-and-open-source-software/1645190#1645190 – Jacco 2011-01-09 17:26:56