Hi there,
If you use this code:
<?php
echo "<pre>";
print_r(hash_algos());
echo "</pre>";
?>
The result with a php 5.3.10 version could be this:
Array
(
[0] => md2
[1] => md4
[2] => md5
[3] => sha1
[4] => sha224
[5] => sha256
[6] => sha384
[7] => sha512
[8] => ripemd128
[9] => ripemd160
[10] => ripemd256
[11] => ripemd320
[12] => whirlpool
[13] => tiger128,3
[14] => tiger160,3
[15] => tiger192,3
[16] => tiger128,4
[17] => tiger160,4
[18] => tiger192,4
[19] => snefru
[20] => snefru256
[21] => gost
[22] => adler32
[23] => crc32
[24] => crc32b
[25] => salsa10
[26] => salsa20
[27] => haval128,3
[28] => haval160,3
[29] => haval192,3
[30] => haval224,3
[31] => haval256,3
[32] => haval128,4
[33] => haval160,4
[34] => haval192,4
[35] => haval224,4
[36] => haval256,4
[37] => haval128,5
[38] => haval160,5
[39] => haval192,5
[40] => haval224,5
[41] => haval256,5
)
Hash 関数
目次
- hash_algos — 登録されているハッシュアルゴリズムの一覧を返す
- hash_copy — ハッシュコンテキストをコピーする
- hash_file — ファイルの内容から、ハッシュ値を生成する
- hash_final — 段階的なハッシュ処理を終了し、出来上がったダイジェストを返す
- hash_hmac_file — HMAC 方式を使用して、指定されたファイルの内容からハッシュ値を生成する
- hash_hmac — HMAC 方式を使用してハッシュ値を生成する
- hash_init — 段階的なハッシュコンテキストを初期化する
- hash_update_file — アクティブなハッシュコンテキストに、ファイルから データを投入する
- hash_update_stream — アクティブなハッシュコンテキストに、オープンしているストリームから データを投入する
- hash_update — アクティブなハッシュコンテキストにデータを投入する
- hash — ハッシュ値 (メッセージダイジェスト) を生成する
jonadabe at [NOSPAM]hotmail dot com
15-Apr-2012 12:02
ocarter at mirabeau dot nl
11-Jan-2010 08:22
Note for FreeBSD with PHP-5.
This is disabled by default; use the security/php5-hash port:
cd /usr/ports/security/php5-hash ; make install clean
(do not use the security/pecl-hash port as in the past, see the /usr/ports/UPDATING post from 20081211)
kungla at gmail dot com
07-Dec-2009 06:00
For Samba ueserPassword you can use:
(I am not very sure aboute the salt part, but it works for me like that)
<?php
function CRYPThash($string, $salt = null)
{
if (!$salt)
$salt = rand(10,99);
return "{CRYPT}".crypt($string, $salt);
}
?>
In posted NTLMHash function you can also use:
<?php
// Encrypt it with the MD4 hash
$MD4Hash=hash('md4',$Input);
?>
So you don't need to install mhash libraries
artem at it-nt dot ru
20-Jul-2008 02:35
And some code for LM hash:
<?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);
}
?>
Some optimization?
CraquePipe
24-Mar-2008 05:42
For thoes of you who're looking to generate an NTLM hash (not an LM hash), I found out it's quite simple..
It uses the basic MD4 function, but the password needs to be in Unicode Little Endian encode first (this canbe achieved by the iconv function).
This can be done easily by using the following code:
<?php
function NTLMHash($Input) {
// Convert the password from UTF8 to UTF16 (little endian)
$Input=iconv('UTF-8','UTF-16LE',$Input);
// Encrypt it with the MD4 hash
$MD4Hash=bin2hex(mhash(MHASH_MD4,$Input));
// You could use this instead, but mhash works on PHP 4 and 5 or above
// The hash function only works on 5 or above
//$MD4Hash=hash('md4',$Input);
// Make it uppercase, not necessary, but it's common to do so with NTLM hashes
$NTLMHash=strtoupper($MD4Hash);
// Return the result
return($NTLMHash);
}
?>
To produce an LM hash requires a fully-written script containing the algorithm used to make it.
Enjoy,
CraquePipe.
martin
27-Sep-2007 02:28
I tried to use Hash() function on my PHP 5.2.3 (FreeBSD 6.2) but there was no Hash() function. (it should be available by default from version 5.1.2)
Hash() function was working after installing "security/pecl-hash" port.
nb_removethis_ at wtal dot de
03-Sep-2007 07:24
Some tests with mhash and hash revealed on my machine that hash needs more than twice as long to create a hash than mhash. The average of one million cylces of Haval128, Haval256, Gost, CRC32, MD5, SHA1 and SHA256 was 450ms for mhash and 1150ms for hash.
