$value){ $str .= " $key=$value"; } } else { $str = ' ' . $msg; } echo '
Debug>' . $str . "\n";
}
if ($exit) {
die('Exit
');
}
}
// Return eg. 2008-11-21T06:11:55Z0711
//
function getUTCTimeStamp() {
date_default_timezone_set('UTC');
$tiny = substr(microtime(false), 2, 3);
return date('Y-m-d\TH:i:s\Z0', time()) . $tiny;
}
// Sign a http query string in the array of key-value pairs
// return b64 encoded hmac hash
function sign($a, $apiKey, $debug=false) {
ksort($a);
$qs = '';
$n = count($a);
$i = 0;
foreach (array_keys($a) as $key) {
$qs .= trim($key).'='.trim($a[$key]);
if (++$i < $n) {
$qs .= '&';
}
}
// Generate the signature
// debug('API key: '.base64_encode($apiKey)); // API key of the client
debug('SIGN: '.$qs);
// the TRUE at the end states we want the raw value, not hexadecimal form
$hmac = hash_hmac('sha1', utf8_encode($qs), $apiKey, true);
$hmac = base64_encode($hmac);
if ($debug) {
debug('h='.$hmac);
}
return $hmac;
} // sign an array of query string
define('DEVICE_ID_LEN', 12);
function modhexToB64($modhex_str) {
$s = ModHex::Decode($modhex_str);
return base64_encode($s);
}
function b64ToModhex($b64_str) {
$s = base64_decode($b64_str);
return ModHex::Encode($s);
}
function b64ToHex($b64_str) {
$s = '';
$tid = base64_decode($b64_str);
$a = str_split($tid);
for ($i=0; $i < count($a); $i++) {
$s .= dechex(ord($a[$i]));
}
return $s;
}
// $devId: The first 12 chars from the OTP
function getAuthData($devId) {
$tokenId = modhexToB64($devId);
$stmt = 'SELECT id, client_id, secret, active, counter, '.
' sessionUse, low, high, accessed '.
' FROM yubikeys WHERE active AND tokenId='.mysql_quote($tokenId);
$r = query($stmt);
if (mysql_num_rows($r) > 0) {
$row = mysql_fetch_assoc($r);
mysql_free_result($r);
return $row;
}
return null;
} // End getAuthData
// $clientId: The decimal client identity
function getClientData($clientId) {
$stmt = 'SELECT secret, chk_sig, chk_owner, chk_time'.
' FROM clients WHERE active AND id='.mysql_quote($clientId);
$r = query($stmt);
if (mysql_num_rows($r) > 0) {
$row = mysql_fetch_assoc($r);
mysql_free_result($r);
return $row;
}
return null;
} // End getClientData
?>