九月丶

一段JAVA签名算法的PHP改写
private function batchGetDataInterafe($StartDate,$End...
扫描右侧二维码阅读全文
03
2018/01

一段JAVA签名算法的PHP改写

    private function batchGetDataInterafe($StartDate,$EndDate)
    {

        $AppSecret = "填写接口方提供的appSecret";
        $AppKey = " 填写接口方提供的appkey";

        // 请求头信息
        $header = array(
            'Content-Type: application/json',
        );

        // $p1 = 1508999906;
        $p1 = time() + 28800;//失效时间戳,获取5分钟后的时间戳,

        $p2 = base64_encode(urlencode($p1)); //p2 将p1转换为urlencode 再转换为base64encode

        $bytes = $this->computeSignature($AppSecret,$p2);
        $p4 = $this->toStr($bytes);

        $token = $AppKey.'@'.$p4.'@'.$p2;
        // $token = '51B3-AID4-4EB3-A1H3-3321@wBCgayDprHtMeo88IiUvr9RxaNA=@MTUwOTAyOTM3NQ==';

        $array = array('Token' => $token,
            'StartDate' => $StartDate,
            'EndDate' => $EndDate);

        $json = json_encode($array);

        return http_post('接口请求url地址', $json, $header);
    }

    private function computeSignature($key, $data) {
        $hash = $this->hmac ( $data, $key );
        $hash = str_split ( $hash );
        foreach ( $hash as $index => $value ) {
            $asc = ord ( $value );
            if ($asc > 128) {
                $hash [$index] = ord ( $value ) - 128 * 2;
            } else {
                $hash [$index] = ord ( $value );
            }
        }
        $bytes = $this->encodeBase64($hash);

        return $bytes;
    }

    private function hmac($data, $key, $hashFunc = 'sha1', $rawOutput = true) {
        if (! in_array ( $hashFunc, hash_algos () )) {
            $hashFunc = 'sha1';
        }
        return hash_hmac ( $hashFunc, $data, $key, $rawOutput );
    }


    private static function encodeBase64($data) {
        $encodes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

        $dataLength = intval ( count ( $data ) );
        $modulus = intval ( $dataLength % 3 );
        //计算结果应有位数
        if ($modulus == 0) {
            //byte位数能被3整除
            $sbLength = intval ( (4 * $dataLength) / 3 );
        } else {
            $sbLength = intval ( 4 * (intval ( ($dataLength / 3) ) + 1) );
        }
        $sb = array ();
        $pos = 0;
        $val = 0;
        foreach ( $data as $i => $byte ) {
            $val = ($val << 8) | ($data [$i] & 0xFF);
            $pos += 8;
            while ( $pos > 5 ) {
                $index = $val >> ($pos -= 6);
                $sb [] = $encodes [$index];
                $val &= ((1 << $pos) - 1);
            }
        }
        if ($pos > 0) {
            $index = $val << (6 - $pos);
            $sb [] = $encodes [$index];
        }
        //位数不够的用=字符(ascII值为61)填充
        $real = count ( $sb );
        if ($real < $sbLength) {
            for($i = 0; $i < $sbLength - $real; $i++) {
                $sb [] = '=';
            }
        }
        return $sb;
    }

    private function toStr($bytes)
    {
        return implode("",str_replace('+', '-', str_replace('/', "_", $bytes)));
    }
最后修改:2018 年 01 月 04 日 08 : 18 PM
如果觉得我的文章对你有用,请随意赞赏

发表评论