我如何解析这个非格式化的 sha 密钥响应列表?

我正在调用 haveibeenpwned api 来搜索数据库中存在的密码 sha1 值的前缀。(表单提交绑定到对 php 脚本的 ajax 调用)。响应是 sha1 中所有泄露的密码,它们的前缀与我的密码相同,以及它们的出现次数。


响应如下所示,没有括号或引号。您可以在此处查看示例调用。


0084E2D508D46A4D5FDB509625EE9BE99CE:1

0159EB03D329C98DD0DF294CD5B9CA52854:2

02662E0A92868C8B65DBFC8764C9A54B646:2

03BBAEF4539EA59F8B0784E64323A3B8E9D:2

04B631308CABEB3038D04B0F114D699A022:6

0556B126AA9AF70D83EA0DD7FB6865A6338:1

05B2FBDE67E25293A020855683A12A8AEB6:2

05CCD00B8F9010E60CF6DA4E1E637EF7664:1

069836ADDB456322375224A6D2374D3309D:1

07402605745C387BEF36C2BC54619EC4573:2

07FB36570851A136481E6B8138AC4834484:2

0829F51F120B5F8B99D7AF00602053556BF:2

089BACBDF1C6214D69F1A3BDC20459D57EE:3

08EB1AA8F3C15FC70D6CD4B1F42C9462671:1

09EEFDDA1D7253593138B66DEA14911B7FA:6

0A1359437D05D0A06CA5C840A297A49EE3E:1

0A84D4E8D914D7A782A81838AD57142B352:1

0A9DAA8398558A5448C327E9621446955F1:2

0BA107CFAC7EE7222F7E23E05D2984B38DC:1

我可以在 jquery 或 php 中搜索我的密码出现率。在 jquery 中,我没有运气尝试将响应转换为字符串并使用 response.includes(my_password_sha)。此外,在 php 中设置 content-type: json 似乎不会破坏服务器。


      curl_setopt($ch, CURLOPT_HTTPHEADER, array(

         'Content-Type: application/json',

     );

这是我一直在努力工作的 javascript:


            success: function(response) { 

                console.log(response);


                console.log("pw checker responded");                

                var resp_str = response.toString();

                resp_str = resp_str.substring(40)


                var pw_sha = response.toString().substring(0,40);

                pw_sha = pw_sha.substring(7,35);


                if(resp_str.includes(pw_sha))

                { //fails to work

                    console.log("password compromised");

                }



            },



慕森王
浏览 84回答 1
1回答

偶然的你

您从服务器获得的响应是纯文本格式,返回的数据是表单中的一系列行ppp : nnn其中ppp是 40 个字符长的 sha1 散列密码,nnn是发生次数。您可以轻松地将响应转换为 PHP 中的关联数组,将数组转换为 JSON 并将 JSON 编码数据发送回前端 JavaScript:$response = explode( "\n", $response );$out = [];foreach( $response as $r ){&nbsp; &nbsp; $r = explode( ":", $r );&nbsp; &nbsp; $out[] = [ 'sha1' => $r[0], 'count' => $r[1] ];}$out = json_encode( $out );echo $out;JavaScript AJAXsuccess()回调将接收一个可以轻松检查的解码对象:success: function(response) {&nbsp; &nbsp; var found,&nbsp; &nbsp; &nbsp; &nbsp; n,&nbsp; &nbsp; &nbsp; &nbsp; i;&nbsp; &nbsp; found = false;&nbsp; &nbsp; n = response.length;&nbsp; &nbsp; for( i = 0; i < n; i++ )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if( pw_sha1_to_check === response[i].sha1 )&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; found = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if( found )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // `pw_sha1_to_check` was found in the list received as response&nbsp; &nbsp; }确保在使用 jQuery 进行 AJAX 调用时指定需要 JSON 响应:&nbsp;$.ajax( {&nbsp; &nbsp; &nbsp;dataType: 'json',&nbsp; &nbsp; &nbsp;// ...
打开App,查看更多内容
随时随地看视频慕课网APP