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

How do I parse this non-formatted list of sha key response?

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

响应如下所示,没有括号或引号。你可以看到一个示例调用 here.

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");
                }


            },

这是 php:

      $service_url = 'https://api.pwnedpasswords.com/range/42331';
      //$service_url = 'https://haveibeenpwned.com/range/aaf4c';


      $ch = curl_init ($service_url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt ($ch, CURLOPT_HTTPGET, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      $returndata = curl_exec ($ch);
      $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); 
      //print_r($status_code);

      //send back my key and the response
      echo $pw_sha." ";
      echo $returndata;

您从服务器获得的响应是​​纯文本,返回的数据是表单中的一系列行

ppp : nnn

其中 ppp 是 40 个字符长的 sha1 散列密码,nnn 是发生次数。

您可以轻松地将响应转换为 PHP 中的关联数组,将数组转换为 JSON 并将 JSON 编码数据发送回前端 JavaScript:

$response = explode( "\n", $response );
$out = [];
foreach( $response as $r )
{
    $r = explode( ":", $r );
    $out[] = [ 'sha1' => $r[0], 'count' => $r[1] ];
}
$out = json_encode( $out );
echo $out;

JavaScriptAJAXsuccess()回调将接收一个可以轻松检查的解码对象:

success: function(response) {
    var found,
        n,
        i;

    found = false;
    n = response.length;
    for( i = 0; i < n; i++ )
    {
        if( pw_sha1_to_check === response[i].sha1 )
        {
            found = true;
            break;
        }
    }

    if( found )
    {
        // `pw_sha1_to_check` was found in the list received as response
    }

确保在使用 jQuery 进行 AJAX 调用时指定需要 JSON 响应:

 $.ajax( {
     dataType: 'json',
     // ...