コード例 #1
0
    public byte[] SignMac(
        string projectId = "my-project", string locationId = "us-east1", string keyRingId = "my-key-ring", string keyId = "my-key", string keyVersionId = "123",
        string data      = "Sample data")
    {
        // Create the client.
        KeyManagementServiceClient client = KeyManagementServiceClient.Create();

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

        // Convert the data into a ByteString.
        ByteString dataByteString = ByteString.CopyFromUtf8(data);

        // Call the API.
        MacSignResponse result = client.MacSign(keyVersionName, dataByteString);

        // The data comes back as raw bytes, which may include non-printable
        // characters. To print the result, you could encode it as base64.
        // string encodedSignature = result.Mac.ToBase64();

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

        // Return the result.
        return(signature);
    }
コード例 #2
0
    public void EncryptsData()
    {
        var data = "testing1234";

        // Create the signature.
        KeyManagementServiceClient client         = KeyManagementServiceClient.Create();
        CryptoKeyVersionName       keyVersionName = new CryptoKeyVersionName(_fixture.ProjectId, _fixture.LocationId, _fixture.KeyRingId, _fixture.MacKeyId, "1");
        MacSignResponse            result         = client.MacSign(keyVersionName, ByteString.CopyFromUtf8(data));

        // Run the sample code.
        var success = _sample.VerifyMac(
            projectId: _fixture.ProjectId, locationId: _fixture.LocationId, keyRingId: _fixture.KeyRingId, keyId: _fixture.MacKeyId, keyVersionId: "1",
            data: data, signature: result.Mac.ToByteArray());

        Assert.True(success);
    }