[PHP] 서명(signature) 생성 샘플코드

Modified on 2012/06/04 21:14 by Administrator — Categorized as: mQR

서명(signature) 생성 PHP 샘플코드



here is slight difference between .Net and PHP. While .Net expects byte array as key, PHP expects string as key. Thus we have to form string representing byte array.

예제1


<?php
$key = pack('c*', 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
$sig = hash_hmac('sha256', "t=HelloWorld&r=10", $key);
echo '<img src="http://mqr.kr/qr/?t=HelloWorld&amp;r=10&amp;sign='.$sig.'"/>';
?>

예제2


<?php
$key = pack('c*', 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
$text = date('Y.m.d H:i:s');
$query = 't='.urlencode($text).'&r=10&j=1&m=20&lb=fccd13&lt=ed1e2e&rt=155ca2';
$sig = hash_hmac('sha256', $query, $key);
echo '<img src="http://mqr.kr/qr/?'.htmlentities($query).'&amp;sign='.$sig.'"/>';
?>


예제3


<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
?>

<?php
$key = pack('c*', 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff);
$text = curPageURL();
$query = 't='.urlencode($text).'&r=10&j=1&m=20&lb=fccd13&lt=ed1e2e&rt=155ca2';
$sig = hash_hmac('sha256', $query, $key);
echo '<img src="http://mqr.kr/qr/?'.htmlentities($query).'&amp;sign='.$sig.'"/>';
?>