Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplified example so it doesn't require an Azure Key Vault instance …
…and just uses a local set of keys

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
  • Loading branch information
WhitWaldo committed Jan 14, 2025
commit 0597ad34c19312d5bce94f7e919a0ad59cde391e
25 changes: 0 additions & 25 deletions examples/Client/Cryptography/Components/azurekeyvault.yaml

This file was deleted.

This file was deleted.

11 changes: 11 additions & 0 deletions examples/Client/Cryptography/Components/local-storage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: localstorage
spec:
type: crypto.dapr.localstorage
version: v1
metadata:
- name: path
# Path is relative to the folder where the example is located
value: ./keys
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@

namespace Cryptography.Examples
{
internal class EncryptDecryptFileStreamExample : Example
internal class EncryptDecryptFileStreamExample(string componentName, string keyName) : Example
{
public override string DisplayName => "Use Cryptography to encrypt and decrypt a file";
public override async Task RunAsync(CancellationToken cancellationToken)
{
using var client = new DaprClientBuilder().Build();

const string componentName = "azurekeyvault"; // Change this to match the name of the component containing your vault
const string keyName = "myKey";

// The name of the file we're using as an example
const string fileName = "file.txt";

Expand All @@ -35,7 +32,6 @@ public override async Task RunAsync(CancellationToken cancellationToken)
{
Console.WriteLine(line);
}
Console.WriteLine();

//Encrypt from a file stream and buffer the resulting bytes to an in-memory buffer
await using var encryptFs = new FileStream(fileName, FileMode.Open);
Expand All @@ -48,8 +44,8 @@ public override async Task RunAsync(CancellationToken cancellationToken)
bufferedEncryptedBytes.Write(bytes.Span);
}

Console.WriteLine($"Encrypted bytes: {Convert.ToBase64String(bufferedEncryptedBytes.WrittenMemory.ToArray())}");
Console.WriteLine();
Console.WriteLine("Encrypted bytes:");
Console.WriteLine(Convert.ToBase64String(bufferedEncryptedBytes.WrittenMemory.ToArray()));

//We'll write to a temporary file via a FileStream
var tempDecryptedFile = Path.GetTempFileName();
Expand All @@ -67,7 +63,7 @@ public override async Task RunAsync(CancellationToken cancellationToken)

//Let's confirm the value as written to the file
var decryptedValue = await File.ReadAllTextAsync(tempDecryptedFile, cancellationToken);
Console.WriteLine($"Decrypted value: ");
Console.WriteLine("Decrypted value: ");
Console.WriteLine(decryptedValue);

//And some cleanup to delete our temp file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@

namespace Cryptography.Examples
{
internal class EncryptDecryptStringExample : Example
internal class EncryptDecryptStringExample(string componentName, string keyName) : Example
{
public override string DisplayName => "Using Cryptography to encrypt and decrypt a string";

public override async Task RunAsync(CancellationToken cancellationToken)
{
using var client = new DaprClientBuilder().Build();

const string componentName = "azurekeyvault"; //Change this to match the name of the component containing your vault
const string keyName = "myKey"; //Change this to match the name of the key in your Vault


const string plaintextStr = "This is the value we're going to encrypt today";
Console.WriteLine($"Original string value: '{plaintextStr}'");
Expand All @@ -40,7 +36,7 @@ public override async Task RunAsync(CancellationToken cancellationToken)
Console.WriteLine($"Encrypted bytes: '{Convert.ToBase64String(encryptedBytesResult.Span)}'");

//Decrypt the string
var decryptedBytes = await client.DecryptAsync(componentName, encryptedBytesResult, keyName, new DecryptionOptions(), cancellationToken);
var decryptedBytes = await client.DecryptAsync(componentName, encryptedBytesResult, keyName, cancellationToken);
Console.WriteLine($"Decrypted string: '{Encoding.UTF8.GetString(decryptedBytes.ToArray())}'");
}
}
Expand Down
9 changes: 6 additions & 3 deletions examples/Client/Cryptography/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ namespace Cryptography
{
class Program
{
private const string ComponentName = "localstorage";
private const string KeyName = "rsa-private-key.pem"; //This should match the name of your generated key - this sample expects an RSA symmetrical key.

private static readonly Example[] Examples = new Example[]
{
new EncryptDecryptStringExample(),
new EncryptDecryptFileStreamExample()
new EncryptDecryptStringExample(ComponentName, KeyName),
new EncryptDecryptFileStreamExample(ComponentName, KeyName)
};

static async Task<int> Main(string[] args)
Expand All @@ -34,7 +37,7 @@ static async Task<int> Main(string[] args)
return 0;
}

Console.WriteLine("Hello, please choose a sample to run:");
Console.WriteLine("Hello, please choose a sample to run by passing your selection's number into the arguments, e.g. 'dotnet run 0':");
for (var i = 0; i < Examples.Length; i++)
{
Console.WriteLine($"{i}: {Examples[i].DisplayName}");
Expand Down