KMSインスタンスSDKクライアントが初期化されると、デジタル署名の作成と検証のためにSignおよびVerifyインターフェイスを呼び出すために使用できます。 このトピックでは、両方のプロセスのコードサンプルを示します。
完全なコード例
Signインターフェイスを呼び出して、非対称キーでデジタル署名を実行します。Verifyインターフェイスを呼び出して、非対称キーを使用して署名の有効性を確認します。
ソースコードGitHubリンク: Sha256AsymmetricSignVerifySample.java
コード例分析
クライアントの初期化
詳細については、「クライアントの初期化」をご参照ください。
import com.aliyun.dkms.gcs.openapi.models.Config;
import com.aliyun.dkms.gcs.sdk.Client;
public static void initClient() throws Exception {
// The connection protocol. Set the value to https. The KMS instance service only allows access through the HTTPS protocol.
Config config = new Config();
config.setProtocol("https");
// Client key.
config.setClientKeyFile("<CLIENT_KEY_FILE>");
// Client key security token.
config.setPassword("<PASSWORD>");
// The endpoint of your KMS instance. Set the value in the following format: <ID of your KMS instance >.cryptoservice.kms.aliyuncs.com.
config.setEndpoint("<ENDPOINT>");
// The certificate authority (CA) certificate of the KMS instance. You can specify the path to the CA certificate file or enter the content of the CA certificate.
config.setCaFilePath("<CA_CERTIFICATE_PATH>");
// Alternatively, set the content of the CA certificate of the KMS instance
//config.setCa("<CA_CERTIFICATE_CONTENT");
client = new Client(config);
}Signインターフェイスを呼び出して、非対称キーを使用してデジタル署名を実行する
/**
* 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 need to ignore the server certificate, you can use the commented code here to call
//RuntimeOptions runtimeOptions = new RuntimeOptions();
//runtimeOptions.setIgnoreSSL(true);
//SignResponse signResponse = client.signWithOptions(signRequest, runtimeOptions);
SignResponse signResponse = client.sign(signRequest);
// 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);
}
}
Verifyインターフェイスを呼び出して、非対称キーを使用してデジタル署名を検証する
KMSの署名作成および検証プロセスは、関連するアルゴリズム標準に準拠しています。 したがって、Verifyインターフェイスを使用するだけでなく、KMSから公開鍵を取得し、他の暗号ライブラリでデジタル署名を検証することもできます。
/**
* Use KMS to verify the message signature
*
* @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 need to ignore the server certificate, you can use the commented code here to call
//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();
}
}