b1gc8v 发表于 2024-10-4 19:15:46

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&nbsp;</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>群&nbsp;</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&amp;wxfrom=5&amp;wx_lazy=1&amp;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&amp;p2=v2&amp; p1=v1&amp;method=cancel&amp;p3=&amp;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&amp;p1=v1&amp;method=cancel&amp; amp;pn=vn,<span style="color: black;">而后</span>按参数名字符升序排序,method=cancel&amp;p1=v1&amp;p2=v2&amp;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;">&lt;?php</span><span style="color: black;">//&nbsp;设置一个公钥(key)和私钥(secret),公钥用于区分用户,私钥加密数据,<span style="color: black;">不可</span>公开</span>$key&nbsp;=&nbsp;<span style="color: black;">"c4ca4238a0b923820dcc509a6f75849b"</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>$secret&nbsp;=&nbsp;<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;">//&nbsp;待发送的数据包</span>$data&nbsp;=&nbsp;<span style="color: black;">array</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">username</span>&nbsp;=&gt;&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">sex</span>&nbsp;=&gt;&nbsp;<span style="color: black;">1</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">age</span>&nbsp;=&gt;&nbsp;<span style="color: black;">16</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">addr</span>&nbsp;=&gt;&nbsp;<span style="color: black;">guangzhou</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">key</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;=&gt;&nbsp;$key,</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">timestamp</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;=&gt;&nbsp;time(),</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">//&nbsp;获取sign</span><span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">getSign</span><span style="color: black;">($secret,&nbsp;$data)</span>&nbsp;</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;对数组的值按key排序</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;ksort($data);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;生成url的形式</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;$params&nbsp;=&nbsp;http_build_query($data);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;生成sign</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;$sign&nbsp;=&nbsp;md5($params&nbsp;.&nbsp;$secret);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;$sign;</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;发送的数据加上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;">]&nbsp;=&nbsp;getSign($secret,&nbsp;$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;">&nbsp;*&nbsp;后台验证sign<span style="color: black;">是不是</span>合法</p>&nbsp;*&nbsp;<span style="color: black;">@param</span>
            <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;$secret&nbsp;</p>&nbsp;*&nbsp;<span style="color: black;">@param</span>
            <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;$data&nbsp;&nbsp;&nbsp;</p>&nbsp;*&nbsp;<span style="color: black;">@return</span>
            <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>&nbsp;*/
      </span><span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">verifySign</span><span style="color: black;">($secret,&nbsp;$data)</span>&nbsp;</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;验证参数中<span style="color: black;">是不是</span>有签名</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(!<span style="color: black;">isset</span>($data[<span style="color: black;">sign</span>])&nbsp;||&nbsp;!$data[<span style="color: black;">sign</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">])&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;<span style="color: black;">发送的数据签名不存在</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(!<span style="color: black;">isset</span>($data[<span style="color: black;">timestamp</span>])&nbsp;||&nbsp;!$data[<span style="color: black;">timestamp</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">])&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;<span style="color: black;">发送的数据参数不合法</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;验证请求,&nbsp;10分钟失效</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(time()&nbsp;-&nbsp;$data[<span style="color: black;">timestamp</span>]&nbsp;&gt;&nbsp;<span style="color: black;">600</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;<span style="color: black;">验证失效,&nbsp;请重新发送请求</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;$sign&nbsp;=&nbsp;$data[<span style="color: black;">sign</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">];</p>&nbsp;&nbsp;&nbsp;&nbsp;<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;">&nbsp;&nbsp;&nbsp;&nbsp;ksort($data);</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;$params&nbsp;=&nbsp;http_build_query($data);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;$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;">&nbsp;&nbsp;&nbsp;&nbsp;$sign2&nbsp;=&nbsp;md5($params&nbsp;.&nbsp;$secret);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;($sign&nbsp;==&nbsp;$sign2)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span style="color: black;">else</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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;">&nbsp;&nbsp;&nbsp;&nbsp;}</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&amp;wxfrom=5&amp;wx_lazy=1&amp;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&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">‍</span></span></p>




qzmjef 发表于 6 天前

请问、你好、求解、谁知道等。

wrjc1hod 发表于 昨天 02:36

期待楼主的下一次分享!”
页: [1]
查看完整版本: PHP做api研发时,签名验证你是怎么设计的