之前做iOS使用的是AES+RSA加密,这次flutter使用sha1验签
php给的文档。php通过hash_hmac
方法直接传入两个参数token和数据,token
对数据字符串加密获得签名。
function signature(array $data, $token)
{
$signStr = '';
ksort($data);
foreach ($data as $key=>$val){
if(!is_scalar($val)){
$val = json_encode($val);
}
$signStr .= $key . $val;
}
return strtoupper(bin2hex(hash_hmac('sha1', $signStr, $token, true)));
}
flutter中解决 解密出来的结果是103c6e83fa9c75a1f20486f950faaddafd9e3f65 跟php验签成功
const token = "qmbn6jmgxidclbarne9srczb92yz53pe";
Map parameters = {
"auth_name": "qqqqqqq",
"APP-ID": "123456",
};
//获得字典中所有key并排序
List attrKeys = parameters.keys.toList();
attrKeys.sort();
//获得字典中数据的字符串
String parameterStr = '';
for (var i = 0; i < attrKeys.length; i++) {
parameterStr += "${attrKeys[i]}${parameters[attrKeys[i]]}";
}
var key = utf8.encode(token);
var bytes = utf8.encode(parameterStr);
//使用token对字典加密
var hmacSha1 = Hmac(sha1, key);
Digest sha1Result = hmacSha1.convert(bytes);
//转大写字符串
var sha1ResultStr = sha1Result.toString().toUpperCase();
print("sha1ResultStr
:$sha1ResultStr");