-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added overloads for encrypting/decrypting streams instead of just fix…
…ed byte arrays. Added example demonstrating the same encrypting a file via a FileStream and decrypting from a MemoryStream. Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
- Loading branch information
Showing
8 changed files
with
292 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
examples/Client/Cryptography/Examples/EncryptDecryptFileStreamExample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2023 The Dapr Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ------------------------------------------------------------------------ | ||
|
||
using Dapr.Client; | ||
|
||
namespace Cryptography.Examples | ||
{ | ||
internal class EncryptDecryptFileStreamExample : 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"; | ||
|
||
Console.WriteLine("Original file contents:"); | ||
foreach (var line in await File.ReadAllLinesAsync(fileName, cancellationToken)) | ||
{ | ||
Console.WriteLine(line); | ||
} | ||
Console.WriteLine(); | ||
|
||
//Encrypt the file | ||
await using var encryptFs = new FileStream(fileName, FileMode.Open); | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
var encryptedBytesResult = await client.EncryptAsync(componentName, encryptFs, KeyWrapAlgorithm.Rsa, keyName, | ||
DataEncryptionCipher.AesGcm, cancellationToken); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
Console.WriteLine($"Encrypted bytes: '{Convert.ToBase64String(encryptedBytesResult)}'"); | ||
Console.WriteLine(); | ||
|
||
//Decrypt the temp file from a memory stream this time instead of a file | ||
await using var ms = new MemoryStream(encryptedBytesResult); | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
var decryptedBytes = await client.DecryptAsync(componentName, ms, keyName, cancellationToken); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
|
||
Console.WriteLine("Decrypted value:"); | ||
await using var decryptedMs = new MemoryStream(decryptedBytes); | ||
using var sr = new StreamReader(decryptedMs); | ||
Console.WriteLine(await sr.ReadToEndAsync(cancellationToken)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# The Road Not Taken | ||
## By Robert Lee Frost | ||
|
||
Two roads diverged in a yellow wood, | ||
And sorry I could not travel both | ||
And be one traveler, long I stood | ||
And looked down one as far as I could | ||
To where it bent in the undergrowth; | ||
|
||
Then took the other, as just as fair | ||
And having perhaps the better claim, | ||
Because it was grassy and wanted wear; | ||
Though as for that, the passing there | ||
Had worn them really about the same, | ||
|
||
And both that morning equally lay | ||
In leaves no step had trodden black | ||
Oh, I kept the first for another day! | ||
Yet knowing how way leads on to way, | ||
I doubted if I should ever come back. | ||
|
||
I shall be telling this with a sigh | ||
Somewhere ages and ages hence: | ||
Two roads diverged in a wood, and I, | ||
I took the one less traveled by, | ||
And that has made all the difference. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.