「php開発」HMAC-SHA1方式を利用してoauth_signatureを生成する方法
PHPコード:
function custom_hmac($algo, $data, $key, $raw_output = false)
{
$algo = strtolower($algo);
$pack = 'H’.strlen($algo('welcome to arkgame.com’));
$size = 64;
$opad = str_repeat(chr(0x5C), $size);
$ipad = str_repeat(chr(0x36), $size);
if (strlen($key) > $size) {
$key = str_pad(pack($pack, $algo($key)), $size, chr(0x00));
} else {
$key = str_pad($key, $size, chr(0x00));
}
for ($i = 0; $i < strlen($key) – 1; $i++) {
$opad[$i] = $opad[$i] ^ $key[$i];
$ipad[$i] = $ipad[$i] ^ $key[$i];
}
$output = $algo($opad.pack($pack, $algo($ipad.$data)));
return ($raw_output) ? pack($pack, $output) : $output;
}
function get_signature($str, $key)
{
$signature = “";
if (function_exists('hash_hmac’))
{
$signature = hash_hmac(“sha1", $str, $key);
}
else
{
$signature = custom_hmac(“sha1", $str, $key);
}
return $signature;
}
echo get_signature(“par1=val1&par2=val2&par3=val3&par4=val4", “pro_startnews24");
?>