Ejemplo n.º 1
0
        public static AsymmetricSignResponse Unmarshall(UnmarshallerContext context)
        {
            AsymmetricSignResponse asymmetricSignResponse = new AsymmetricSignResponse();

            asymmetricSignResponse.HttpResponse = context.HttpResponse;
            asymmetricSignResponse._Value       = context.StringValue("AsymmetricSign.Value");
            asymmetricSignResponse.KeyId        = context.StringValue("AsymmetricSign.KeyId");
            asymmetricSignResponse.RequestId    = context.StringValue("AsymmetricSign.RequestId");
            asymmetricSignResponse.KeyVersionId = context.StringValue("AsymmetricSign.KeyVersionId");

            return(asymmetricSignResponse);
        }
Ejemplo n.º 2
0
    public byte[] SignAsymmetric(
        string projectId = "my-project", string locationId = "us-east1", string keyRingId = "my-key-ring", string keyId = "my-key", string keyVersionId = "123",
        string message   = "Sample message")
    {
        // Create the client.
        KeyManagementServiceClient client = KeyManagementServiceClient.Create();

        // Build the key version name.
        CryptoKeyVersionName keyVersionName = new CryptoKeyVersionName(projectId, locationId, keyRingId, keyId, keyVersionId);

        // Convert the message into bytes. Cryptographic plaintexts and
        // ciphertexts are always byte arrays.
        byte[] plaintext = Encoding.UTF8.GetBytes(message);

        // Calculate the digest.
        SHA256 sha256 = SHA256.Create();

        byte[] hash = sha256.ComputeHash(plaintext);

        // Build the digest.
        //
        // Note: Key algorithms will require a varying hash function. For
        // example, EC_SIGN_P384_SHA384 requires SHA-384.
        Digest digest = new Digest
        {
            Sha256 = ByteString.CopyFrom(hash),
        };

        // Call the API.
        AsymmetricSignResponse result = client.AsymmetricSign(keyVersionName, digest);

        // Get the signature.
        byte[] signature = result.Signature.ToByteArray();

        // Return the result.
        return(signature);
    }