After initializing the KMS instance SDK client, you can use it to call the Sign and Verify APIs for signing and verification. This topic provides code examples for this.
Complete example
Example walkthrough
Initialize the client
import com.aliyun.dkms.gcs.openapi.models.Config;
import com.aliyun.dkms.gcs.sdk.Client;
public static void initClient() throws Exception {
// The protocol must be "https". The KMS instance service only allows access over HTTPS.
Config config = new Config();
config.setProtocol("https");
// Path to the ClientKey file.
config.setClientKeyFile("<YOUR_CLIENT_KEY_FILE>");
// Password for the ClientKey.
config.setPassword("<YOUR_PASSWORD>");
// Set the endpoint to <your-kms-instance-id>.cryptoservice.kms.aliyuncs.com.
config.setEndpoint("<YOUR_ENDPOINT>");
// The CA certificate of the KMS instance. You can specify a file path or the content directly.
config.setCaFilePath("<PATH_TO_YOUR_CACERT>");
// Alternatively, you can set the CA certificate content.
//config.setCa("<your-ca-certificate-content");
client = new Client(config);
}Call the Sign API to sign data by using an asymmetric key
/**
* Use KMS to sign the message.
*
* @param keyId
* @param algorithm
* @param message
* @param messageType
* @return
* @throws Exception
*/
public static SignContext asymmetricSign(String keyId, String algorithm, String message, String messageType) throws Exception {
SignRequest signRequest = new SignRequest();
signRequest.setKeyId(keyId);
signRequest.setAlgorithm(algorithm);
signRequest.setMessage(getDigest(message));
signRequest.setMessageType(messageType);
try {
// If you want to ignore the server certificate, you can use the following commented-out code to call the operation.
//RuntimeOptions runtimeOptions = new RuntimeOptions();
//runtimeOptions.setIgnoreSSL(true);
//SignResponse signResponse = client.signWithOptions(signRequest, runtimeOptions);
SignResponse signResponse = client.sign(signRequest);
// The signature value.
byte[] signature = signResponse.getSignature();
System.out.println("================sign================");
System.out.printf("KeyId: %s%n", signResponse.getKeyId());
System.out.printf("Signature: %s%n", Arrays.toString(signature));
System.out.println("================sign================");
return new SignContext(signResponse.getKeyId(), signResponse.getSignature(), signResponse.getAlgorithm(), signResponse.getMessageType());
} catch (TeaException e) {
System.out.printf("Code: %s%n", ((TeaException) e).getCode());
System.out.printf("Message: %s%n", ((TeaException) e).getMessage());
System.out.printf("HttpCode: %s%n", ((TeaException) e).getData().get("httpCode"));
System.out.printf("HostId: %s%n", ((TeaException) e).getData().get("hostId"));
System.out.printf("RequestId: %s%n", ((TeaException) e).getData().get("requestId"));
e.printStackTrace();
throw new RuntimeException(e);
} catch (Exception e) {
System.out.printf("sign errMsg: %s%n", e.getMessage());
e.printStackTrace();
throw new RuntimeException(e);
}
}
Call the Verify API to verify the signature by using an asymmetric key
The results of signing and verification by using KMS comply with the corresponding algorithm standards. Therefore, apart from calling the Verify API to verify the signature, you can download the public key from KMS and verify the signature by using other cryptographic libraries.
/**
* Use KMS to verify the signature of the message.
*
* @param signContext
* @throws Exception
*/
public static void asymmetricVerify(final SignContext signContext, String message) throws Exception {
VerifyRequest verifyRequest = new VerifyRequest();
verifyRequest.setKeyId(signContext.getKeyId());
verifyRequest.setAlgorithm(signContext.getAlgorithm());
verifyRequest.setMessage(getDigest(message));
verifyRequest.setMessageType(signContext.getMessageType());
verifyRequest.setSignature(signContext.getSignature());
try {
// If you want to ignore the server certificate, you can use the following commented-out code to call the operation.
//RuntimeOptions runtimeOptions = new RuntimeOptions();
//runtimeOptions.setIgnoreSSL(true);
//VerifyResponse verifyResponse = client.verifyWithOptions(verifyRequest, runtimeOptions);
VerifyResponse verifyResponse = client.verify(verifyRequest);
System.out.println("================verify================");
System.out.printf("KeyId: %s%n", verifyResponse.getKeyId());
System.out.printf("Value: %s%n", verifyResponse.getValue());
System.out.println("================verify================");
} catch (TeaException e) {
System.out.printf("Code: %s%n", ((TeaException) e).getCode());
System.out.printf("Message: %s%n", ((TeaException) e).getMessage());
System.out.printf("HttpCode: %s%n", ((TeaException) e).getData().get("httpCode"));
System.out.printf("HostId: %s%n", ((TeaException) e).getData().get("hostId"));
System.out.printf("RequestId: %s%n", ((TeaException) e).getData().get("requestId"));
e.printStackTrace();
} catch (Exception e) {
System.out.printf("verify errMsg: %s%n", e.getMessage());
e.printStackTrace();
}
}