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 client
You can create a KMS instance SDK client object using either ClientKey content or a ClientKey file path.
import (
dedicatedkmsopenapi "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/openapi"
dedicatedkmssdk "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/sdk"
"github.com/alibabacloud-go/tea/tea"
)
// getDkmsClientByClientKeyFile creates a KMS instance SDK client using
// the ClientKey file path.
func getDkmsClientByClientKeyFile() *dedicatedkmssdk.Client {
config := &dedicatedkmsopenapi.Config{
// KMS instance service only allows HTTPS.
Protocol: tea.String("https"),
// Path to the ClientKey file (clientKey_****.json).
ClientKeyFile: tea.String("<CLIENT_KEY_FILE>"),
// Password for the ClientKey (from clientKey_****_Password.txt).
Password: tea.String("<CLIENT_KEY_PASSWORD>"),
// Endpoint format: <KMS_INSTANCE_ID>.cryptoservice.kms.aliyuncs.com
Endpoint: tea.String("<ENDPOINT>"),
}
client, err := dedicatedkmssdk.NewClient(config)
if err != nil {
panic(err)
}
return client
}Call the Sign API to perform digital signing using an asymmetric key
// Signing example
func signSample(client *dedicatedkmssdk.Client, keyId string, message []byte, messageType string) *SignatureContext {
signRequest := &dedicatedkmssdk.SignRequest{
KeyId: tea.String(keyId),
Message: message,
MessageType: tea.String(messageType),
}
// Verify the server certificate.
ca, err := ioutil.ReadFile("path/to/caCert.pem")
if err != nil {
panic(err)
}
runtimeOptions := &dedicatedkmsopenapiutil.RuntimeOptions{
Verify: tea.String(string(ca)),
}
// Or, ignore the certificate.
//runtimeOptions := &dedicatedkmsopenapiutil.RuntimeOptions{
// IgnoreSSL: tea.Bool(true),
//}
// Call the signing API for signing.
signResponse, err := client.SignWithOptions(signRequest, runtimeOptions)
if err != nil {
panic(err)
}
// Key ID.
_keyId := tea.StringValue(signResponse.KeyId)
// Signature value.
_signature := signResponse.Signature
// Message type.
_messageType := tea.StringValue(signResponse.MessageType)
// Signature algorithm.
_algorithm := tea.StringValue(signResponse.Algorithm)
fmt.Println("KeyId:", _keyId)
fmt.Println("Signature:", _signature)
fmt.Println("MessageType:", _messageType)
fmt.Println("RequestId:", tea.StringValue(signResponse.RequestId))
return &SignatureContext{
KeyId: _keyId,
Signature: _signature,
MessageType: messageType,
Algorithm: _algorithm,
}
}Call the Verify API to verify the digital signature using an asymmetric key
// Signature verification example.
func verifySample(client *dedicatedkmssdk.Client, message []byte, ctx *SignatureContext) (_value bool) {
verifyRequest := &dedicatedkmssdk.VerifyRequest{
KeyId: tea.String(ctx.KeyId),
Message: message,
MessageType: tea.String(ctx.MessageType),
Signature: ctx.Signature,
Algorithm: tea.String(ctx.Algorithm),
}
// Verify the server certificate.
ca, err := ioutil.ReadFile("path/to/caCert.pem")
if err != nil {
panic(err)
}
runtimeOptions := &dedicatedkmsopenapiutil.RuntimeOptions{
Verify: tea.String(string(ca)),
}
// Or, ignore the certificate.
//runtimeOptions := &dedicatedkmsopenapiutil.RuntimeOptions{
// IgnoreSSL: tea.Bool(true),
//}
// Call the signature verification API for signature verification.
verifyResponse, err := client.VerifyWithOptions(verifyRequest, runtimeOptions)
if err != nil {
panic(err)
}
// Signature verification result.
_value = tea.BoolValue(verifyResponse.Value)
// Message type
_messageType := verifyResponse.MessageType
fmt.Println("KeyId:", tea.StringValue(verifyResponse.KeyId))
fmt.Println("Value:", _value)
fmt.Println("MessageType:", tea.StringValue(_messageType))
fmt.Println("RequestId:", tea.StringValue(verifyResponse.RequestId))
return tea.BoolValue(verifyResponse.Value)
}