During an API 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 whether the response has been tampered with, ensuring the security of API calling.
The game public key is generated after the game service is enabled. For details, please refer to Enabling Account Kit.
The Java sample code is as follows:
/**
* Verify the signature.
* @param content String to be verified.
* @param sign Signature string.
* @param publicKey Public key.
* @param signType Encryption type.
* @param Whether the verification is successful.
*/
public static boolean doCheck(String content, String sign, String publicKey, String signType) {
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] encodedKey = decoder.decode(publicKey);
PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
java.security.Signature signature = null;
if ("RSA256".equals(signType)) {
signature = java.security.Signature.getInstance(SIGN_ALGORITHMS256);
} else {
signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);
}
signature.initVerify(pubKey);
signature.update(content.getBytes("utf-8"));
boolean bverify = signature.verify(decoder.decode(sign));
return bverify;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}