目录

During an interface call, the requester may receive a response that contains a signature string from the recipient. In this case, the requester can verify the signature string in order to check if the response has been tampered with. This helps secure the interface call. Generally, only the payment public key is used for signature verification. The response to a query request can be verified on the app client. Only the callback message indicating successful payment, which sent to the developer server from the Huawei payment server, needs to be verified on the app server.

1. Obtain the signature string in the returned result, that is, the rtnSign field returned by the interface.

2. Sort the returned parameters rtnCode and ts that need to be signed in ascending order according to the ASCII codes of their names.

3. Splice the sorted parameters into a string using the URL key value pair format, for example rtnCode=xxxxxx&ts=xxxxxxx.

Notice:
Note the following when splicing the string:
● Parameter names and values must not be UrlEncoded.
● Parameter values must be the same as those in the response.

4. Verify the string that is generated and the string that is returned by using the game public key.

Note:
The game public key is generated after the HUAWEI Game service is available. For details about how to query the game public key, see Querying Game Information.

See the following for the sample code in Java:

  1. public static boolean doCheck(String content, String sign, String publicKey, String signtype) {
  2.   try {
  3.      KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  4.      byte[] encodedKey = decoder.decode(publicKey);
  5.      PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
  6.      java.security.Signature signature = null;
  7.      if ("RSA256".equals(signtype)) {
  8.        signature = java.security.Signature.getInstance(SIGN_ALGORITHMS256);
  9.      } else {
  10.        signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);
  11.      }      
  12.      signature.initVerify(pubKey);
  13.      signature.update(content.getBytes("utf-8"));
  14.      boolean bverify = signature.verify(decoder.decode(sign));
  15.      return bverify;
  16.   } catch (Exception e) {
  17.      e.printStackTrace();
  18.   }
  19.   return false;
  20. }