PHP做api研发时,签名验证你是怎么设计的
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">商务合作加<span style="color: black;">微X</span>:2230304070 </strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">学习与交流:</span></strong><a style="color: black;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">PHP技术交流<span style="color: black;">微X</span>群 </span></strong></span></a></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">一个jetbrains<span style="color: black;">整家</span>桶,copilot,ai assistant的激活网站</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">https://web.52shizhan.cn</span></strong></span></p><img src="https://mmbiz.qpic.cn/mmbiz_gif/X36HLl2EicOfbCTTtzwpbdicOhvibmKu9O9jGSLPicT5IVjk2U8YETibumKAAa5K55Y3mdvGOvhjfbUGdEibKx8E5BJQ/640?wx_fmt=gif&wxfrom=5&wx_lazy=1&tp=webp" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">研发</span>过程中,<span style="color: black;">咱们</span>经常会与接口打交道,有的时候是调取别人网站的接口,有的时候是为他人<span style="color: black;">供给</span>自己网站的接口,但是在这调取的过程中都离不开签名验证。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">咱们</span>在设计签名验证的时候,请<span style="color: black;">重视</span>要满足以下几点:</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">可变性:每次的签名必须是不<span style="color: black;">同样</span>的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">时效性:每次请求的时效,过期作废等。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">独一</span>性:每次的签名是<span style="color: black;">独一</span>的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">完整性:能够对传入数据进行验证,防止篡改。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>介绍一种方式,是<span style="color: black;">日前</span>国内互联网<span style="color: black;">机构</span>常用的一种方式,其中淘宝的支付宝支付接口、淘宝开放平台接口、腾讯开放平台等应用的一种方式。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">1、</span>签名参数sign生成的<span style="color: black;">办法</span></span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第1步: 将所有参数(<span style="color: black;">重视</span>是所有参数),除去sign本身,以及值是空的参数,按参数名字母升序排序。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第2步: <span style="color: black;">而后</span>把排序后的参数按参数1值1参数2值2…参数n值n(<span style="color: black;">这儿</span>的参数和值必须是传输参数的原始值,<span style="color: black;">不可</span>是经过处理的,如<span style="color: black;">不可</span>将"转成”后再拼接)的方式拼接成一个字符串。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第3步: 把分配给接入方的验证密钥key拼接在第2步得到的字符串前面。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第2步: 在上一步得到的字符串前面加上验证密钥key(<span style="color: black;">这儿</span>的密钥key是接口<span style="color: black;">供给</span>方分配给接口接入方的),<span style="color: black;">而后</span>计算md5值,得到32位字符串,<span style="color: black;">而后</span>转成大写.</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第4步: 计算第3步字符串的md5值(32位),<span style="color: black;">而后</span>转成大写,得到的字符串<span style="color: black;">做为</span>sign的值。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">举例:</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">假设传输的数据是/interface.php?sign=sign_value&p2=v2& p1=v1&method=cancel&p3=&pn=vn(<span style="color: black;">实质</span><span style="color: black;">状况</span>最好是<span style="color: black;">经过</span>post方式发送),其中sign参数对应的sign_value<span style="color: black;">便是</span>签名的值。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">第1</span>步,拼接字符串,<span style="color: black;">首要</span>去除sign参数本身,<span style="color: black;">而后</span>去除值是空的参数p3,剩下p2=v2&p1=v1&method=cancel& amp;pn=vn,<span style="color: black;">而后</span>按参数名字符升序排序,method=cancel&p1=v1&p2=v2&pn=vn.</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第二步,<span style="color: black;">而后</span>做参数名和值的拼接,最后得到methodcancelp1v1p2v2pnvn</p>第三步,在上面拼接得到的字符串前加上验证密钥key,<span style="color: black;">咱们</span>假设是abc,得到新的字符串abcmethodcancelp1v1p2v2pnvn
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第四步,<span style="color: black;">而后</span>将这个字符串进行md5计算,假设得到的是abcdef,<span style="color: black;">而后</span>转为大写,得到ABCDEF这个值即为sign签名值。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>,计算md5之前请<span style="color: black;">保证</span>接口与接入方的字符串编码一致,如统一<span style="color: black;">运用</span>utf-8编码<span style="color: black;">或</span>GBK编码,<span style="color: black;">倘若</span>编码方式不一致则计算出来的签名会校验失败。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">2、</span>签名验证<span style="color: black;">办法</span>:</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">按照</span>前面描述的签名参数sign生成的<span style="color: black;">办法</span>规则,计算得到参数的签名值,和参数中<span style="color: black;">通告</span>过来的sign对应的参数值进行对比,<span style="color: black;">倘若</span>是一致的,<span style="color: black;">那样</span>就校验<span style="color: black;">经过</span>,<span style="color: black;">倘若</span>不一致,说明参数被修改过。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">3、</span>下面直接看代码</span></strong></p><span style="color: black;"><span style="color: black;"><?php</span><span style="color: black;">// 设置一个公钥(key)和私钥(secret),公钥用于区分用户,私钥加密数据,<span style="color: black;">不可</span>公开</span>$key = <span style="color: black;">"c4ca4238a0b923820dcc509a6f75849b"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>$secret = <span style="color: black;">"28c8edde3d61a0411511d3b1866f0636"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;">// 待发送的数据包</span>$data = <span style="color: black;">array</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(</p> <span style="color: black;">username</span> => <span style="color: black;">abc@qq.com</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">sex</span> => <span style="color: black;">1</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">age</span> => <span style="color: black;">16</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">addr</span> => <span style="color: black;">guangzhou</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">key</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> => $key,</p> <span style="color: black;">timestamp</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> => time(),</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">// 获取sign</span><span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">getSign</span><span style="color: black;">($secret, $data)</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">// 对数组的值按key排序</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ksort($data);</p> <span style="color: black;">// 生成url的形式</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $params = http_build_query($data);</p> <span style="color: black;">// 生成sign</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $sign = md5($params . $secret);</p> <span style="color: black;">return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $sign;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">// 发送的数据加上sign</span>$data[<span style="color: black;">sign</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">] = getSign($secret, $data);</p><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> * 后台验证sign<span style="color: black;">是不是</span>合法</p> * <span style="color: black;">@param</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $secret </p> * <span style="color: black;">@param</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $data </p> * <span style="color: black;">@return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> </p> */
</span><span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">verifySign</span><span style="color: black;">($secret, $data)</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">// 验证参数中<span style="color: black;">是不是</span>有签名</span> <span style="color: black;">if</span> (!<span style="color: black;">isset</span>($data[<span style="color: black;">sign</span>]) || !$data[<span style="color: black;">sign</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">]) {</p> <span style="color: black;">echo</span> <span style="color: black;">发送的数据签名不存在</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">die</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">if</span> (!<span style="color: black;">isset</span>($data[<span style="color: black;">timestamp</span>]) || !$data[<span style="color: black;">timestamp</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">]) {</p> <span style="color: black;">echo</span> <span style="color: black;">发送的数据参数不合法</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">die</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">// 验证请求, 10分钟失效</span> <span style="color: black;">if</span> (time() - $data[<span style="color: black;">timestamp</span>] > <span style="color: black;">600</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">echo</span> <span style="color: black;">验证失效, 请重新发送请求</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">die</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> $sign = $data[<span style="color: black;">sign</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">];</p> <span style="color: black;">unset</span>($data[<span style="color: black;">sign</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">]);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ksort($data);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $params = http_build_query($data);</p> <span style="color: black;">// $secret是<span style="color: black;">经过</span>key在api的数据库中<span style="color: black;">查找</span>得到</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $sign2 = md5($params . $secret);</p> <span style="color: black;">if</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ($sign == $sign2) {</p> <span style="color: black;">die</span>(<span style="color: black;">验证<span style="color: black;">经过</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> } <span style="color: black;">else</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> <span style="color: black;">die</span>(<span style="color: black;">请求不合法</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
</span><strong style="color: blue;"><span style="color: black;">参考链接:<strong style="color: blue;"><span style="color: black;">https://segmentfault.com/a/1190000019142952</span></strong></span></strong><span style="color: black;"><span style="color: black;">以上<span style="color: black;">便是</span>本篇分钟的<span style="color: black;">所有</span>内容,</span><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">期盼</span>各位程序员们<span style="color: black;">奋斗</span><span style="color: black;">提高</span>个人技术。</span></strong></span><span style="color: black;">最后,<span style="color: black;">博主</span>温馨提示:<span style="color: black;">每日</span>阅读5分钟,<span style="color: black;">每日</span>学习一点点,<span style="color: black;">每日</span>进步一点点。</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_gif/X36HLl2EicOfSLeDjYdhheic53iaMxy6jzuQaBEXzfu12r8KsocY8g1D5GGfcUJ4nibr37JY3ZZBu1wjKiaNxEASXUA/640?wx_fmt=gif&wxfrom=5&wx_lazy=1&tp=webp" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/uIqA9zdyleyDIhxfbt7DaTT5a4QibDoG9cjrINXv1wpUImSribq2abGGGYeN0j5tsgJ6yQBytrXmUFprtSOA4Ceg/640?wx_fmt=other&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></span></p>
页:
[1]