PHP Interface Development Data Signature program code

Source: Internet
Author: User

Now commonly used in the application development of the interface, its data is through the open Internet transmission, the data security requirements, in order to prevent the data in the transmission process was tampered with, commonly used data signature (sign) method to verify.

Data Signature Sign Generation method

① to remove null values and signature parameters in an array (Sign/sign_type)
② key names in ascending order array
③ all elements of the array into strings using the "&" character in the mode "parameter = parameter value"
④ the concatenation of the string with the security check code directly linked up

Encryption functions such as ⑤MD5, encrypt strings
Sample code

Class Sign {

/**
* Get Data signatures
*
* $param Signature Array @param array
* @param string $code Security check Code
* @param string $sign _type signature Type
* @return String Signature strings
*/
public static function Getsign ($param, $code, $sign _type = ' MD5 ') {
To remove null values and signature parameters in an array (Sign/sign_type)
$param = self::p aramfilter ($param);
Key names in ascending order array
$param = self::p aramsort ($param);
Put all elements of an array into a string using the "&" character in the mode "parameter = parameter value"
$param _str = self::createlinkstring ($param);
The concatenation string is then connected directly with the security check code.
$param _str = $param _str. $code;
To create a signature string
Return Self::createsign ($param _str, $sign _type);
}

/**
* Verify data signature
*
* @param signature received by string $sign interface
* $param Signature Array @param array
* @param string $code Security check Code
* @param string $sign _type signature Type
* @return Boolean true correctly, 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 Signature Array @param array
* @return Array removes the new array after null and signature parameters
*/
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;
}

/**
* Key name in ascending order array
*
* Array of @param array $param before sorting
* Array sorted @return Array
*/
private static function Paramsort ($param) {
Ksort ($param);
Reset ($param);
return $param;
}

/**
* The array of all elements, according to "parameter = parameter value" mode with "&" character stitching into a string
*
* @param array $param need to be spliced
* @return string concatenation completes subsequent strings
*/
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
if (GET_MAGIC_QUOTES_GPC ()) {
$str = Stripslashes ($STR);
}
return $str;
}

/**
* Create signature string
*
* @param string $param strings that need to be encrypted
* @param string $type signature Type default value: 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 for subsequent development, please use the MD5 signature ');
}
Exit ("interface is temporarily unsupported". $type. "Type of signature method");
}
}

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.