1
0
mirror of https://github.com/Yubico/yubikey-val.git synced 2025-01-20 10:52:15 +01:00

sign example

This commit is contained in:
Paul Chen 2008-10-08 06:29:19 +00:00
parent 2ebaaaeb1f
commit b18af416f8
3 changed files with 46 additions and 7 deletions

View File

@ -13,9 +13,9 @@ function debug($msg, $exit = false) {
global $trace; global $trace;
if ($trace) { if ($trace) {
if (is_array($msg)) { if (is_array($msg)) {
print_r($msg); //print_r($msg);
} else { } else {
echo 'debug> ' . $msg; echo '<p>Debug> ' . $msg;
} }
echo "\n"; echo "\n";
} }
@ -42,7 +42,7 @@ function getUTCTimeStamp() {
// Sign a http query string in the array of key-value pairs // Sign a http query string in the array of key-value pairs
// return b64 encoded hmac hash // return b64 encoded hmac hash
function sign($a, $apiKey) { function sign($a, $apiKey, $debug=false) {
ksort($a); ksort($a);
$qs = ''; $qs = '';
$n = count($a); $n = count($a);
@ -56,15 +56,18 @@ function sign($a, $apiKey) {
// Generate the signature // Generate the signature
//debug('API key: '.$apiKey); // API key of the client //debug('API key: '.$apiKey); // API key of the client
debug('Signing: '.$qs); debug('SIGN: '.$qs);
// the TRUE at the end states we want the raw value, not hexadecimal form // the TRUE at the end states we want the raw value, not hexadecimal form
$hmac = hash_hmac('sha1', utf8_encode($qs), $apiKey, true); $hmac = hash_hmac('sha1', utf8_encode($qs), $apiKey, true);
$hmac = base64_encode($hmac); $hmac = base64_encode($hmac);
if ($debug) {
debug('h='.$hmac);
debug('<a href=verify_debug.php?'.$qs.'&h='.urlencode($hmac).'>Submit the request >> </a>');
}
return $hmac; return $hmac;
} // SignWithTime } // sign an array of query string
function outputToFile($outFname, $content, $mode, $append = false) { function outputToFile($outFname, $content, $mode, $append = false) {
$out = fopen($outFname, ($append ? "a" : "w")); $out = fopen($outFname, ($append ? "a" : "w"));

37
sign_demo.php Normal file
View File

@ -0,0 +1,37 @@
<h3>Generate a signature</h3>
<?php require_once '../yubiphpbase/appinclude.php';
require_once '../yubiphpbase/yubi_lib.php';
require_once 'common.php';
$trace = true;
$act = getHttpVal('act', '');
if ($act == '') {
echo '<form action=sign_demo.php method=post>'.
'<input name=act value=sign type=hidden>'.
'api key: (use your api key issued to you by Yubico in b64 format): ' .
'<input name=apikey size=45 maxlength=100 value="kNapft02c1a81N4MEMDcC/mgcGc="><p>'.
'id (your client id): <input name=id size=5 maxlength=10><p>'.
'otp: <input name=otp size=45 maxlength=100><p>'.
'<input type=submit value=Sign>'.
'</form>';
exit;
}
$id = getHttpVal('id', '');
$otp = getHttpVal('otp', '');
$t = getHttpVal('t', '');
$apiKey = base64_decode(getHttpVal('apikey', ''));
$a['id']=$id;
$a['otp']=$otp;
if ($t != '') {
$a['t']=$t;
}
$hmac = sign($a, $apiKey, true);
?>

View File

@ -24,7 +24,6 @@ if ($otp == '') {
// //
$devId = substr($otp, 0, 12); $devId = substr($otp, 0, 12);
$ad = getAuthData($devId); $ad = getAuthData($devId);
debug('Auth Data from DB:');
if ($ad == null) { if ($ad == null) {
debug('Invalid Yubikey '.$devId); debug('Invalid Yubikey '.$devId);