[php]代码库
/**
* 请求组装验证串
*
* @param array $req 请求参数
* @param string $appsecret 验证秘钥
*/
static protected function makeSign(&$req, $appsecret)
{
ksort($req);
$arr = array();
foreach ($req as $key => $val) {
if ($key == 'sign') continue;
if ($val === null) {
$val = "";
$req[$key] = "";
}
if (count($arr))
$arr[] = ';';
$arr[] = sprintf("%02d", iconv_strlen($key, 'UTF-8'));
$arr[] = '-';
$arr[] = $key;
$arr[] = ':';
$arr[] = sprintf("%04d", iconv_strlen($val, 'UTF-8'));
$arr[] = '-';
$arr[] = $val;
}
$sign = md5(implode('', $arr) . $appsecret);
$req['sign'] = $sign;
}
例子:
$req = array(name=>'lengmo',gender=>'male',sign=>'qwert')
ksort($req)
$req = array(gender=>'male',name=>'lengmo',sign=>'qwert')
$arr['06','-','gender',':','0004','-','male',';','04','-','name',':','0006','-','lengmo',';','04','-','sign',':','0005','-','qwert']
md5(06-gender:0004-male;04-name:0006-lengmo;04-sign:0005-qwert.$appsecret)