PHP interface development data signature program code-PHP source code

Source: Internet
Author: User
The data signature for PHP interface development is very important not only to have good algorithms but also to keep them confidential. Let's take a look at an example of data signature for php interface development implemented by PHP, the details are as follows. The data signature for PHP interface development is very important not only to have good algorithms but also to keep them confidential. Let's take a look at an example of data signature for php interface development implemented by PHP, the details are as follows.

Script ec (2); script

APIs are usually used in application development. The data is transmitted over the open Internet, which requires data security. To prevent data tampering during transmission, common Data signature (sign) method for verification.

Data signature sign Generation Method

① Remove null values and signature parameters in the array (sign/sign_type)
② Sort the keys in ascending order
③ Concatenate all elements of the array into strings using the "&" character in "parameter = parameter value" Mode
④ Connect the spliced string directly with the security verification code

⑤ MD5 encryption functions, encrypted strings
Sample Code

Class Sign {

/**
* Obtain the data signature.
*
* @ Param array $ param signature array
* @ Param string $ code security verification code
* @ Param string $ sign_type signature type
* @ Return string signature string
*/
Public static function getSign ($ param, $ code, $ sign_type = 'md5 '){
// Remove null values and signature parameters in the array (sign/sign_type)
$ Param = self: paramFilter ($ param );
// Sort the keys in ascending order
$ Param = self: paramSort ($ param );
// Concatenates all elements of the array into strings using the "&" character in "parameter = parameter value" Mode
$ Param_str = self: createLinkstring ($ param );
// Connect the spliced string directly with the security verification code
$ Param_str = $ param_str. $ code;
// Create a signature string
Return self: createSign ($ param_str, $ sign_type );
}

/**
* Verify the data signature
*
* @ Param string $ sign the signature received by the interface
* @ Param array $ param signature array
* @ Param string $ code security verification code
* @ Param string $ sign_type signature type
* @ Return boolean true, false failed
*/
Public static function checkSign ($ sign, $ param, $ code, $ sign_type = 'md5 '){
Return $ sign = self: getSign ($ param, $ code, $ sign_type );
}

/**
* Remove null values and signature parameters from the array.
*
* @ Param array $ param signature array
* @ Return array removes the null value and the new array after the signature Parameter
*/
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;
}

/**
* Sort the keys in ascending order
*
* @ Param array $ array before param sorting
* @ Return array the sorted array
*/
Private static function paramSort ($ param ){
Ksort ($ param );
Reset ($ param );
Return $ param;
}

/**
* Concatenates all elements of the array into strings using the "&" character in "parameter = parameter value" Mode
*
* @ Param array $ array to be spliced by param
* @ Return string the string after Splicing
*/
Private static function createLinkstring ($ param ){
$ Str = '';
Foreach ($ param as $ key => $ val ){
$ Str. = "{$ key }={ $ val }&";
}
// Remove the last & character
$ Str = substr ($ str, 0, strlen ($ str)-1 );
// If an escape character exists, remove the escape character.
If (get_magic_quotes_gpc ()){
$ Str = stripslashes ($ str );
}
Return $ str;
}

/**
* Create a signature string
*
* @ Param string $ string to be encrypted by param
* @ Param string $ type default value of signature type: MD5
* @ Return string signature result
*/
Private static function createSign ($ param, $ type = 'md5 '){
$ Type = strtolower ($ type );
If ($ type = 'md5 '){
Return md5 ($ param );
}
If ($ type = 'dsa '){
Exit ('dsa signature method to be developed in the future. Use the MD5 Signature Method first ');
}
Exit ("the interface does not support". $ type. "type signature method ");
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.