As far as security is concerned, all client and server-side communications should be transmitted over encrypted channel (HTTPS), and the plaintext HTTP channel will be a hotbed of man-in-the-middle and its various variants. The so-called man-in-the-middle attack simply refers to malicious hackers can be in the client and the server side of the clear-text communication channels, hackers can listen to communications, steal confidential information, even can tamper with the communication content, and through the encrypted communication content theoretically can not be deciphered.
URL Signature Generation rules
|
The API's valid access URL includes the following three sections:
1. Resource access paths, such as/v1/deal/find_deals;
2. Request parameter: The API corresponding to the required parameter name and parameter value Param=value, multiple request parameters with & connection such as deal_id=1-85462&appid=00000;
3. Signature string, generated by signature algorithm |
The signature algorithm is as follows:
|
1. Dictionary of all request parameters in ascending order;
2. Concatenate the above-ordered parameter table as a string, such as Key1value1key2value2key3value3...keynvaluen;
3. The app secret as a suffix, the string is SHA-1 calculated, and converted to 16 binary encoding;
4. Get the signature string after converting to full capitalization |
Note: Ensure that the HTTP request data encoding must be in the UTF-8 format, and the URL must be in UTF-8 encoded format.
To cite an example:
PHP Server to the Developer (APP) to assign a AppID and Appsecret (under normal circumstances, developers to the service provider's official website application), as a client, need to keep the official issued AppID & Appsecret
AppID will be used as an application in the request to identify the parameter passed in the interface request, Appsecret will not need to pass as the only parameter, but it will be used as a key parameter to verify the current request, only the application developer and the issued server to know. Because the signature is based on the same algorithm encryption implementation, so the application side and the server can calculate the same signature value, the actual meaning of the signature is the server-side access to the client authentication. In a sense, the signature mechanism is somewhat similar to the signature of the public key method, with each application corresponding to the private key value to decrypt, but the essence of this decryption process is to check the signature parameter value process.
Assumed allocations:
$appid =5288971; $appsecret = ' R5e2t85tyu142u665698fzu ';
Mobile client, need to request a list of services (the following code can be written for mobile end such as Java or SF)
Request Address: Http://web.com/server/list
Parameters:
$array =[' AppID ' =>5288971, ' menu ' = ' customer Service list ', ' lat ' =>21.223, ' LNG ' =>131.334 ];
corresponding Signature Algorithm
//1. Dictionary ordering of encrypted arraysforeach ($arrayAs$key =$value){$arr [$key] =$key; }Sort$arr);//The function of dictionary sorting is to prevent the following stitching encryption different//2 because of inconsistent parameter order. Splicing Key and value$str = ""; foreach ( $arr as $k = $v ) { $str = $str. $arr [ $K]. $array [ $v ];} //3. Encrypted by SHA1 and converted to uppercase//4. Get a signature in uppercase $restr = $str. $appsecret; $sign = strtoupper (SHA1 ( Span style= "COLOR: #800080" > $restr))
Writes the sign signature of the production to the array and sends the parameters to the request interface through a well-appointed method
$array [' sign ']=$sign;
Print $array
Array( [AppID] = 5288971 [Menu] + customer Service list [lat] = 21.223 [LNG] + = 131.334 [Sign] = c096d7811e944386ce880597ba334a5ab640b088)
The client sends the data encapsulated XML or JSON to the server, which is parsed by the server first
{"AppID": 5288971, "menu": "\u5ba2\u6237\u670d\u52a1\u5217\u8868", "lat": 21.223, "LNG": 131.334, "sign": " c096d7811e944386ce880597ba334a5ab640b088 "}
$serverArray = Json_decode ($json,TRUE);
Server-side query AppID the corresponding key
$model =model::find ()->where ("Appid=:appid")->params ([": AppID" =$serverArray [' AppID ']])if ($model$serverSecret =appsecret $model;}
According to the same dictionary sort and algorithm to generate the service side of the $sign, determine whether the $sign is the same.
$clientSign =$array [' Sign '];unset$serverArray [' Sign ']);#Generate service-side STR$serverstr = "";foreach ($serverArrayAs$k =$v) {$serverstr =$str.$k. $v ;} $reserverstr = $str. $ Serversecret; $reserverSign = strtoupper (SHA1 ( $reserverstr if ( $clientSign!=$ Reserversigndie (' illegal request ') else{// your code continue;}
When only the SMS login to do mobile app, you can set secret expiration time, SMS login, save AppID (userid) and key secret, each time the user opens the app, the first network request login is expired, expired re-SMS login to get a new secret.
Additional:
Sometimes we use HASH_HMAC for encryption (used in our project ...). )
/** Generate signature, $args as request parameter, $key as private key*/function Makesignature ($args,$key){IfIsset$args [' Sign '])) {$oldSign =$args [' Sign '];unset$args [' Sign ']); }Else{$oldSign = "; }Ksort ( $args $requestString = "; foreach ( $args as $k = $v ) { $requestString. = $k. ' = '. UrlEncode ( $v) ;} $newSign = Hash_hmac ("MD5", strtolower ( $requestString), $key return $newSign
PHP Development API Interface Signature Verification