PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

hash_init> <hash_hmac_file
Last updated: Fri, 05 Sep 2008

view this page in

hash_hmac

(PHP 5 >= 5.1.2, PECL hash:1.1-1.5)

hash_hmacHMAC 方式を使用してハッシュ値を生成する

説明

string hash_hmac ( string $algo , string $data , string $key [, bool $raw_output ] )

パラメータ

algo

選択したアルゴリズムの名前 (すなわち "md5"、"sha256"、"haval160,4" など…)。

data

ハッシュするメッセージ。

key

HMAC 方式でのメッセージダイジェストを生成するために使用する 共有の秘密鍵。

raw_output

TRUE を設定すると、生のバイナリデータを出力します。 デフォルト (FALSE) の場合は小文字の 16 進数値となります。

返り値

raw_output が true に設定されていない場合は、 メッセージダイジェストの計算結果を小文字の 16 進数値形式の文字列で 返します。もし true に設定されていた場合は、メッセージダイジェストが そのままのバイナリ形式で返されます。

例1 hash_hmac() の例

<?php
echo hash_hmac('ripemd160''The quick brown fox jumped over the lazy dog.''secret');
?>

上の例の出力は以下となります。

b8e7ae12510bdfb1812e463a7f086122cf37e4f7



add a note add a note User Contributed Notes
hash_hmac
Carlos Averett(caverett*@*corecodec,net)
04-Jul-2008 12:54
Generating OATH-compliant OTP (one time passwords) results in PHP:

<?php
$otp
= oath_truncate (oath_hotp ($key, $counter), $length);
function
oath_hotp ($key, $counter) {
       
// Counter
       
$bin_counter = pack ('C*', $counter);

       
// Pad to 8 chars
       
if (strlen ($bin_counter) < 8) {
               
$bin_counter = str_repeat (chr(0), 8 - strlen ($bin_counter)) . $bin_counter;
        }

       
// HMAC
       
$hash = hash_hmac ('sha1', $bin_counter, $key);
        return
$hash;
}

function
oath_truncate ($hash, $length = 6) {
       
// The last byte is used as an offset
       
$offset = hexdec (substr ($hash, 38)) & 0xf;

       
// Extract the relevant part, and clear the first bit
       
$hex_truncated = substr ($hash, $offset * 2, 8);
       
$bin_truncated = decbin (hexdec ($hex_truncated));
       
$bin_truncated[0] = '0';
       
$dec_truncated = bindec ($bin_truncated);

        return
substr ($dec_truncated, 0 - $length);
}
?>

hash_init> <hash_hmac_file
Last updated: Fri, 05 Sep 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites