2024 年 PHP Conference Japan

雜湊函式

目錄

  • hash — 產生雜湊值(訊息摘要)
  • hash_algos — 返回已註冊雜湊演算法的清單
  • hash_copy — 複製雜湊上下文
  • hash_equals — 時序攻擊安全字串比較
  • hash_file — 使用指定檔案的內容產生雜湊值
  • hash_final — 完成增量雜湊並返回結果摘要
  • hash_hkdf — 產生 HKDF 金鑰衍生
  • hash_hmac — 使用 HMAC 方法產生金鑰雜湊值
  • hash_hmac_algos — 傳回適用於 hash_hmac 的已註冊雜湊演算法清單
  • hash_hmac_file — 使用 HMAC 方法和指定檔案的內容產生金鑰雜湊值
  • hash_init — 初始化增量雜湊內容
  • hash_pbkdf2 — 產生指定密碼的 PBKDF2 金鑰衍生
  • hash_update — 將資料輸入到作用中的雜湊內容
  • hash_update_file — 從檔案將資料輸入到作用中的雜湊內容
  • hash_update_stream — 從開啟的串流將資料輸入到作用中的雜湊內容
新增註解

使用者貢獻的註解 4 則註解

artem at it-nt dot ru
16 年前
以及一些用於 LM 雜湊的程式碼

<?php
function LMhash($string)
{
$string = strtoupper(substr($string,0,14));

$p1 = LMhash_DESencrypt(substr($string, 0, 7));
$p2 = LMhash_DESencrypt(substr($string, 7, 7));

return
strtoupper($p1.$p2);
}

function
LMhash_DESencrypt($string)
{
$key = array();
$tmp = array();
$len = strlen($string);

for (
$i=0; $i<7; ++$i)
$tmp[] = $i < $len ? ord($string[$i]) : 0;

$key[] = $tmp[0] & 254;
$key[] = ($tmp[0] << 7) | ($tmp[1] >> 1);
$key[] = ($tmp[1] << 6) | ($tmp[2] >> 2);
$key[] = ($tmp[2] << 5) | ($tmp[3] >> 3);
$key[] = ($tmp[3] << 4) | ($tmp[4] >> 4);
$key[] = ($tmp[4] << 3) | ($tmp[5] >> 5);
$key[] = ($tmp[5] << 2) | ($tmp[6] >> 6);
$key[] = $tmp[6] << 1;

$is = mcrypt_get_iv_size(MCRYPT_DES, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($is, MCRYPT_RAND);
$key0 = "";

foreach (
$key as $k)
$key0 .= chr($k);
$crypt = mcrypt_encrypt(MCRYPT_DES, $key0, "KGS!@#$%", MCRYPT_MODE_ECB, $iv);

return
bin2hex($crypt);
}
?>

一些優化?
CraquePipe
16 年前
對於那些正在尋找產生 NTLM 雜湊(而非 LM 雜湊)方法的人,我發現它相當簡單..

它使用基本的 MD4 函式,但密碼需要先以 Unicode Little Endian 編碼(這可以使用 iconv 函式來達成)。

可以使用以下程式碼輕鬆完成

<?php
function NTLMHash($Input) {
// 將密碼從 UTF8 轉換為 UTF16 (小端序)
$Input=iconv('UTF-8','UTF-16LE',$Input);

// 使用 MD4 雜湊加密
$MD4Hash=bin2hex(mhash(MHASH_MD4,$Input));

// 您可以改用這個,但 mhash 適用於 PHP 4 和 5 或以上版本
// hash 函式僅適用於 5 或以上版本
//$MD4Hash=hash('md4',$Input);

// 將其轉換為大寫,非必要,但 NTLM 雜湊通常會這樣做
$NTLMHash=strtoupper($MD4Hash);

// 傳回結果
return($NTLMHash);
}
?>

要產生 LM 雜湊,需要一個完整編寫的腳本,其中包含用於產生它的演算法。

敬請享用,
CraquePipe.
kungla at gmail dot com
14 年前
對於 Samba ueserPassword,您可以使用

(我不太確定 salt 部分,但它對我來說是這樣工作的)

<?php
函式 CRYPThash($string, $salt = null)
{
if (!
$salt)
$salt = rand(10,99);

return
"{CRYPT}".crypt($string, $salt);
}
?>

在已發布的 NTLMHash 函式中,您也可以使用

<?php
// 使用 MD4 雜湊演算法加密
$MD4Hash=hash('md4',$Input);
?>

所以您不需要安裝 mhash 函式庫
ocarter at mirabeau dot nl
14 年前
FreeBSD 搭配 PHP-5 的注意事項。

此功能預設為停用;請使用 security/php5-hash port
cd /usr/ports/security/php5-hash ; make install clean
(不要使用過去的 security/pecl-hash port,請參閱 20081211 的 /usr/ports/UPDATING 文章)
To Top