作为一名后端开发人员,我想大家肯定都做过一件,那就是做“签名”。
大部分签名都是参数按key排序,然后拼接成字符串,再然后拼接上指定的密钥做加密。
我这次遇到的问题也是如此,作为一名PHPer,再简单不过了
// 获取sign
function getSign($secret, $data) {
// 对数组的值按key排序
ksort($data);
// 生成url的形式
$params = http_build_query($data);
// 生成sign
$sign = md5($params . $secret);
return $sign;
}
//结果
"lists%5B0%5D%5Bcode%5D=110011&lists%5B0%5D%5Bname%5D=%E9%92%B3%E5%B7%A5&lists%5B0%5D%5Bbig_class_id%5D=1&lists%5B0%5D%5Bmin_pay%5D=3&lists%5B0%5D%5Bmax_pay%5D=5&lists%5B0%5D%5Bprovince_code%5D=330000&lists%5B0%5D%5Bcity_code%5D=330100&lists%5B0%5D%5Barea_code%5D=330108&lists%5B0%5D%5Bstreet_code%5D=330108510000&lists%5B0%5D%5Bcorp_name%5D=%E4%BD%B0%E5%AE%89%E7%A7%91%E6%8A%80&lists%5B0%5D%5Barea%5D=%E6%9D%AD%E5%B7%9E%E5%B8%82%E6%BB%A8%E6%B1%9F%E5%8C%BA&lists%5B0%5D%5Baddress%5D=%E5%8D%A7%E9%BE%99%E5%A4%A7%E5%8E%A61501&lists%5B0%5D%5Bperson_num%5D=1&lists%5B0%5D%5Bvalid_start_date%5D=2022-03-01&lists%5B0%5D%5Bvalid_end_date%5D=2022-03-05&lists%5B0%5D%5Bweal%5D=%E4%BA%94%E9%99%A9%E4%B8%80%E9%87%91%E3%80%82%E6%B3%95%E5%AE%9A%E8%8A%82%E5%81%87%E6%97%A5%E3%80%82%E4%B8%8B%E5%8D%88%E8%8C%B6%E3%80%82%E5%B9%B4%E7%BB%88%E5%A5%96&lists%5B0%5D%5Bdemand%5D=%E6%88%91%E4%BB%AC%E7%94%A8%E4%B8%80%E4%B8%AA%E6%8A%80%E6%9C%AF%E6%80%BB%E7%9B%91%E6%8B%9B%E8%81%98%E8%A6%81%E6%B1%82%E6%9D%A5%E7%9C%8B%E4%B8%80%E4%B8%8B%EF%BC%9A%E4%BB%BB%E8%81%8C%E8%B5%84%E6%A0%BC%E7%9A%84%E5%85%B7%E4%BD%93%E6%8F%8F%E8%BF%B0%EF%BC%9A%E8%B4%9F%E8%B4%A3%E6%8C%87%E5%AF%BC%E5%88%B6%E5%AE%9A%E3%80%81%E5%AE%A1%E6%A0%B8%E5%85%AC%E5%8F%B8%E6%8A%80%E6%9C%AF%E7%AE%A1%E7%90%86%E5%88%B6%E5%BA%A6%E5%92%8C%E6%8A%80%E6%9C%AF%E8%A7%84%E7%A8%8B%E6%A0%87%E5%87%86%EF%BC%9B&lists%5B0%5D%5Bstatus%5D=1&lists%5B1%5D%5Bcode%5D=110011&lists%5B1%5D%5Bname%5D=%E9%92%B3%E5%B7%A5&lists%5B1%5D%5Bbig_class_id%5D=1&lists%5B1%5D%5Bmin_pay%5D=3&lists%5B1%5D%5Bmax_pay%5D=5&lists%5B1%5D%5Bprovince_code%5D=330000&lists%5B1%5D%5Bcity_code%5D=330100&lists%5B1%5D%5Barea_code%5D=330108&lists%5B1%5D%5Bstreet_code%5D=330108510000&lists%5B1%5D%5Bcorp_name%5D=%E4%BD%B0%E5%AE%89%E7%A7%91%E6%8A%80&lists%5B1%5D%5Barea%5D=%E6%9D%AD%E5%B7%9E%E5%B8%82%E6%BB%A8%E6%B1%9F%E5%8C%BA&lists%5B1%5D%5Baddress%5D=%E5%8D%A7%E9%BE%99%E5%A4%A7%E5%8E%A61501&lists%5B1%5D%5Bperson_num%5D=1&lists%5B1%5D%5Bvalid_start_date%5D=2022-03-01&lists%5B1%5D%5Bvalid_end_date%5D=2022-03-05&lists%5B1%5D%5Bweal%5D=%E4%BA%94%E9%99%A9%E4%B8%80%E9%87%91%E3%80%82%E6%B3%95%E5%AE%9A%E8%8A%82%E5%81%87%E6%97%A5%E3%80%82%E4%B8%8B%E5%8D%88%E8%8C%B6%E3%80%82%E5%B9%B4%E7%BB%88%E5%A5%96&lists%5B1%5D%5Bdemand%5D=%E6%88%91%E4%BB%AC%E7%94%A8%E4%B8%80%E4%B8%AA%E6%8A%80%E6%9C%AF%E6%80%BB%E7%9B%91%E6%8B%9B%E8%81%98%E8%A6%81%E6%B1%82%E6%9D%A5%E7%9C%8B%E4%B8%80%E4%B8%8B%EF%BC%9A%E4%BB%BB%E8%81%8C%E8%B5%84%E6%A0%BC%E7%9A%84%E5%85%B7%E4%BD%93%E6%8F%8F%E8%BF%B0%EF%BC%9A%E8%B4%9F%E8%B4%A3%E6%8C%87%E5%AF%BC%E5%88%B6%E5%AE%9A%E3%80%81%E5%AE%A1%E6%A0%B8%E5%85%AC%E5%8F%B8%E6%8A%80%E6%9C%AF%E7%AE%A1%E7%90%86%E5%88%B6%E5%BA%A6%E5%92%8C%E6%8A%80%E6%9C%AF%E8%A7%84%E7%A8%8B%E6%A0%87%E5%87%86%EF%BC%9B&lists%5B1%5D%5Bstatus%5D=1×tamp=1646375634"
但是,因为我个人也喜欢前端的东西,所以我就想“前端能够实现http_build_query这样的拼接方式嘛?”
和前端尝试了很多次,但得到的sign总是对不上就很头疼,难道是前端技术太次了?但是两三个前端拼接后生成的sign都对不上,就很郁闷。
http_build_query因为这个函数看了一下官方文档,也没有特别的
所以我就跳过这个函数,看能不能再通过其他的函数或者再转换一下让前端更容易拼接参数来生成sign
所以,我在上面代码中做了修改
// 获取sign
function getSign($secret, $data) {
// 对数组的值按key排序
ksort($data);
// 生成url的形式
$params = http_build_query($data);
$params = urldecode($params);
// 生成sign
$sign = md5($params . $secret);
return $sign;
}
//结果
"lists[0][code]=110011&lists[0][name]=钳工&lists[0][big_class_id]=1&lists[0][min_pay]=3&lists[0][max_pay]=5&lists[0][province_code]=330000&lists[0][city_code]=330100&lists[0][area_code]=330108&lists[0][street_code]=330108510000&lists[0][corp_name]=佰安科技&lists[0][area]=杭州市滨江区&lists[0][address]=卧龙大厦1501&lists[0][person_num]=1&lists[0][valid_start_date]=2022-03-01&lists[0][valid_end_date]=2022-03-05&lists[0][weal]=五险一金。法定节假日。下午茶。年终奖&lists[0][demand]=我们用一个技术总监招聘要求来看一下:任职资格的具体描述:负责指导制定、审核公司技术管理制度和技术规程标准;&lists[0][status]=1&lists[1][code]=110011&lists[1][name]=钳工&lists[1][big_class_id]=1&lists[1][min_pay]=3&lists[1][max_pay]=5&lists[1][province_code]=330000&lists[1][city_code]=330100&lists[1][area_code]=330108&lists[1][street_code]=330108510000&lists[1][corp_name]=佰安科技&lists[1][area]=杭州市滨江区&lists[1][address]=卧龙大厦1501&lists[1][person_num]=1&lists[1][valid_start_date]=2022-03-01&lists[1][valid_end_date]=2022-03-05&lists[1][weal]=五险一金。法定节假日。下午茶。年终奖&lists[1][demand]=我们用一个技术总监招聘要求来看一下:任职资格的具体描述:负责指导制定、审核公司技术管理制度和技术规程标准;&lists[1][status]=1×tamp=1646375634"
这样就容易多了,看着也清晰多了,自己也尝试了前端的拼接方式,可以实现,希望能够帮到你
网友评论