<?php
namespace Freyo\LaravelQueueCMQ\Queue\Driver;
use Exception;
class Signature
{
public static function sign($srcStr, $secretKey, $method = 'HmacSHA1')
{
switch ($method) {
case 'HmacSHA1':
$retStr = base64_encode(hash_hmac('sha1', $srcStr, $secretKey, true));
break;
case 'HmacSHA256':
$retStr = base64_encode(hash_hmac('sha256', $srcStr, $secretKey, true));
break;
default:
throw new Exception($method . ' is not a supported encrypt method');
break;
}
return $retStr;
}
public static function makeSignPlainText($requestParams,
$requestMethod = 'POST', $requestHost = '',
$requestPath = '/v2/index.php')
{
$url = $requestHost . $requestPath;
$paramStr = self::_buildParamStr($requestParams, $requestMethod);
$plainText = $requestMethod . $url . $paramStr;
return $plainText;
}
protected static function _buildParamStr($requestParams, $requestMethod = 'POST')
{
$paramStr = '';
ksort($requestParams);
$i = 0;
foreach ($requestParams as $key => $value) {
if ($key == 'Signature') {
continue;
}
if ($requestMethod == 'POST' && substr($value, 0, 1) == '@') {
continue;
}
if (strpos($key, '_')) {
$key = str_replace('_', '.', $key);
}
if ($i == 0) {
$paramStr .= '?';
} else {
$paramStr .= '&';
}
$paramStr .= $key . '=' . $value;
++$i;
}
return $paramStr;
}
}