search
Homephp教程PHP源码PHP接口开发数据签名程序代码

PHP接口开发数据签名这个非常的重要不但要有好的算法同时也要保密了,下面我们来看一篇由php实现的关于PHP接口开发数据签名例子吧,具体如下所示。

<script>ec(2);</script>

现在应用开发中通常会用到接口,其数据是通过开放的互联网传输,对数据的安全性有一定要求,为了防止数据在传输过程中被篡改,常用数据签名(sign)的方式来校验。

数据签名sign生成方法

①去除数组中的空值和签名参数(sign/sign_type)
②按键名升序排列数组
③把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
④把拼接后的字符串再与安全校验码直接连接起来

⑤MD5等加密函数,加密字符串
示例代码

class Sign {
 
    /**
     * 获取数据签名
     *
     * @param  array  $param  签名数组
     * @param  string $code      安全校验码
     * @param  string $sign_type 签名类型
     * @return string        签名字符串
     */
    public static function getSign($param, $code, $sign_type = 'MD5'){
        //去除数组中的空值和签名参数(sign/sign_type)
        $param = self::paramFilter($param);
        //按键名升序排列数组
        $param = self::paramSort($param);
        //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
        $param_str = self::createLinkstring($param);
        //把拼接后的字符串再与安全校验码直接连接起来
        $param_str = $param_str . $code;
        //创建签名字符串
        return self::createSign($param_str, $sign_type);
    }
    
    /**
     * 校验数据签名
     *
     * @param  string $sign  接口收到的签名
     * @param  array  $param  签名数组
     * @param  string $code      安全校验码
     * @param  string $sign_type 签名类型
     * @return boolean true正确,false失败
     */
    public static function checkSign($sign, $param, $code, $sign_type = 'MD5'){
        return $sign == self::getSign($param, $code, $sign_type);
    }
    
    /**
     * 去除数组中的空值和签名参数
     *
     * @param  array $param 签名数组
     * @return array        去掉空值与签名参数后的新数组
     */
    private static function paramFilter($param){
        $param_filter = array();
        foreach ($param as $key => $val) {
            if($key == 'sign' || $key == 'sign_type' || !strlen($val)){
                continue;
            }
            $param_filter[$key] = $val;
        }
        return $param_filter;
    }
    
    /**
     * 按键名升序排列数组
     *
     * @param  array $param 排序前的数组
     * @return array        排序后的数组
     */
    private static function paramSort($param){
        ksort($param);
        reset($param);
        return $param;
    }
    
    /**
     * 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
     *
     * @param  array $param 需要拼接的数组
     * @return string       拼接完成以后的字符串
     */
    private static function createLinkstring($param){
        $str = '';
        foreach ($param as $key => $val) {
            $str .= "{$key}={$val}&";
        }
        //去掉最后一个&字符
        $str = substr($str, 0, strlen($str) - 1);
        //如果存在转义字符,那么去掉转义
        if(get_magic_quotes_gpc()){
            $str = stripslashes($str);
        }
        return $str;
    }
    
    /**
     * 创建签名字符串
     *
     * @param  string $param 需要加密的字符串
     * @param  string $type  签名类型 默认值:MD5
     * @return string 签名结果
     */
    private static function createSign($param, $type = 'MD5'){
        $type = strtolower($type);
        if($type == 'md5'){
            return md5($param);
        }
        if($type == 'dsa'){
            exit('DSA 签名方法待后续开发,请先使用MD5签名方式');
        }
        exit("接口暂不支持" . $type . "类型的签名方式");
    }
}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)