To ensure HTTPS transmission security and prevent request parameters from being tampered with, an app must include a signature string of the request parameters – cpSign – in the Verifying the Sign-in Signature interface called. The signature string is used by the Huawei game server to verify the request parameters.
The rules for generating a signature string are as follows:
1. Sort the request parameters that need to be signed in ascending order according to the ASCII codes of their names. If the initial letters of two names are the same, compare the second letter and so on.
The parameters that need to be signed are those labeled Y in the signature column of the interface description, included in the request, and with values other than null or "".
2. URL encode the values of the sorted parameters.
3. Use & to splice the parameters included in the signature and their URL encoded values to a string using the key-value pair format, for example, "a=xxxxxx&b=xxxxxxx&c=xxxxxxxxxxx...".
- private static String format(Map<String, String> params)
- {
- StringBuffer base = new StringBuffer();
- Map<String, String> tempMap = new TreeMap<String, String>(params);
- //Obtain the basic string for calculating the nsp_key
- try
- {
- for (Map.Entry<String, String> entry : tempMap.entrySet())
- {
- String k = entry.getKey();
- String v = entry.getValue();
- base.append(k).append("=").append(URLEncoder.encode(v, "UTF-8")).append("&");
- }
- }
- catch (UnsupportedEncodingException e)
- {
- System.out.println("Encode parameters failed.");
- e.printStackTrace();
- }
- String body = base.toString().substring(0, base.toString().length() - 1);
- //Convert spaces and asterisks
- body = body.replaceAll("\\+", "%20").replaceAll("\\*", "%2A");
- return body;
- }
4. Use the game's private key to sign the string to be signed with the RSA algorithm (SHA256WithRSA). Base64 encode the signed string. The obtained string is the signature string.
A Java code example is as follows:
- private static String generateCPSign(Map<String,String> requestParams,final String cpAuthKey)
- {
- //Sort the queried string in the message body in alphabetical order and URL encode them.
- String baseStr = format(requestParams);
- //Sign the encoded request string using the CP's private key for signature
- String cpSign = sign(baseStr.getBytes(Charset.forName("UTF-8")), cpAuthKey);
- return cpSign;
- }
- private static String sign(byte[] data, String privateKey)
- {
- try
- {
- byte[] e = Base64.decodeBase64(privateKey);
- PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(e);
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
- Signature signature = Signature.getInstance("SHA256WithRSA");
- signature.initSign(privateK);
- signature.update(data);
- String signs = Base64.encodeBase64String(signature.sign());
- return signs;
- }
- catch (Exception var)
- {
- System.out.println("SignUtil.sign error." + var);
- return "";
- }
- }