Skip to content

Commit

Permalink
Updated methods to use ReadOnlyMemory<byte> instead of byte[] - updat…
Browse files Browse the repository at this point in the history
…ed implementations to use low-allocation spans where possible (though ToArray is necessary to wrap with MemoryStream).

Signed-off-by: Whit Waldo <[email protected]>
  • Loading branch information
WhitWaldo committed Jan 3, 2024
1 parent 80e4539 commit bd49006
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/Dapr.Client/DaprClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ public abstract Task<UnsubscribeConfigurationResponse> UnsubscribeConfiguration(
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the operation.</param>
/// <returns>An array of encrypted bytes.</returns>
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public abstract Task<byte[]> EncryptAsync(string vaultResourceName, byte[] plaintextBytes,
public abstract Task<ReadOnlyMemory<byte>> EncryptAsync(string vaultResourceName, ReadOnlyMemory<byte> plaintextBytes,
KeyWrapAlgorithm algorithm, string keyName, DataEncryptionCipher dataEncryptionCipher = DataEncryptionCipher.AesGcm,
CancellationToken cancellationToken = default);

Expand All @@ -969,7 +969,7 @@ public abstract Task<byte[]> EncryptAsync(string vaultResourceName, byte[] plain
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the operation.</param>
/// <returns>An array of encrypted bytes.</returns>
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public abstract Task<byte[]> EncryptAsync(string vaultResourceName, byte[] plaintextBytes,
public abstract Task<ReadOnlyMemory<byte>> EncryptAsync(string vaultResourceName, ReadOnlyMemory<byte> plaintextBytes,
KeyWrapAlgorithm algorithm, string keyName, string decryptionKeyName, DataEncryptionCipher dataEncryptionCipher = DataEncryptionCipher.AesGcm,
CancellationToken cancellationToken = default);

Expand All @@ -984,7 +984,7 @@ public abstract Task<byte[]> EncryptAsync(string vaultResourceName, byte[] plain
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the operation.</param>
/// <returns>An array of encrypted bytes.</returns>
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public abstract Task<byte[]> EncryptAsync(string vaultResourceName, Stream plainTextStream,
public abstract Task<ReadOnlyMemory<byte>> EncryptAsync(string vaultResourceName, Stream plainTextStream,
KeyWrapAlgorithm algorithm, string keyName, DataEncryptionCipher dataEncryptionCipher = DataEncryptionCipher.AesGcm,
CancellationToken cancellationToken = default);

Expand All @@ -1000,7 +1000,7 @@ public abstract Task<byte[]> EncryptAsync(string vaultResourceName, Stream plain
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the operation.</param>
/// <returns>An array of encrypted bytes.</returns>
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public abstract Task<byte[]> EncryptAsync(string vaultResourceName, Stream plainTextStream,
public abstract Task<ReadOnlyMemory<byte>> EncryptAsync(string vaultResourceName, Stream plainTextStream,
KeyWrapAlgorithm algorithm, string keyName, string decryptionKeyName, DataEncryptionCipher dataEncryptionCipher = DataEncryptionCipher.AesGcm,
CancellationToken cancellationToken = default);

Expand All @@ -1013,7 +1013,7 @@ public abstract Task<byte[]> EncryptAsync(string vaultResourceName, Stream plain
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the operation.</param>
/// <returns>An array of decrypted bytes.</returns>
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public abstract Task<byte[]> DecryptAsync(string vaultResourceName, byte[] cipherTextBytes, string keyName,
public abstract Task<ReadOnlyMemory<byte>> DecryptAsync(string vaultResourceName, ReadOnlyMemory<byte> cipherTextBytes, string keyName,
CancellationToken cancellationToken = default);

/// <summary>
Expand All @@ -1025,7 +1025,7 @@ public abstract Task<byte[]> DecryptAsync(string vaultResourceName, byte[] ciphe
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the operation.</param>
/// <returns>An array of decrypted bytes.</returns>
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public abstract Task<byte[]> DecryptAsync(string vaultResourceName, Stream cipherTextStream, string keyName,
public abstract Task<ReadOnlyMemory<byte>> DecryptAsync(string vaultResourceName, Stream cipherTextStream, string keyName,
CancellationToken cancellationToken = default);

#endregion
Expand Down
25 changes: 13 additions & 12 deletions src/Dapr.Client/DaprClientGrpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ public override async Task<UnsubscribeConfigurationResponse> UnsubscribeConfigur

#region Cryptography

private async Task<byte[]> EncryptStreamAsync(string vaultResourceName, Stream stream,
private async Task<ReadOnlyMemory<byte>> EncryptStreamAsync(string vaultResourceName, Stream stream,
KeyWrapAlgorithm algorithm, string keyName, DataEncryptionCipher dataEncryptionCipher,
string decryptionKeyName, bool omitDecryptionKey, CancellationToken cancellationToken = default)
{
Expand Down Expand Up @@ -1448,15 +1448,15 @@ await duplexStream.RequestStream.WriteAsync(new Autogenerated.EncryptRequest

/// <inheritdoc />
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public override Task<byte[]> EncryptAsync(
public override Task<ReadOnlyMemory<byte>> EncryptAsync(
string vaultResourceName, Stream plainTextStream, KeyWrapAlgorithm algorithm,
string keyName, DataEncryptionCipher dataEncryptionCipher = DataEncryptionCipher.AesGcm,
CancellationToken cancellationToken = default) => EncryptStreamAsync(vaultResourceName, plainTextStream,
algorithm, keyName, dataEncryptionCipher, string.Empty, true, cancellationToken);

/// <inheritdoc />
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public override Task<byte[]> EncryptAsync(
public override Task<ReadOnlyMemory<byte>> EncryptAsync(
string vaultResourceName, Stream plainTextStream, KeyWrapAlgorithm algorithm,
string keyName, string decryptionKeyName,
DataEncryptionCipher dataEncryptionCipher = DataEncryptionCipher.AesGcm,
Expand All @@ -1466,25 +1466,25 @@ public override Task<byte[]> EncryptAsync(

/// <inheritdoc />
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public override Task<byte[]> EncryptAsync(
string vaultResourceName, byte[] plaintextBytes, KeyWrapAlgorithm algorithm,
public override Task<ReadOnlyMemory<byte>> EncryptAsync(
string vaultResourceName, ReadOnlyMemory<byte> plaintextBytes, KeyWrapAlgorithm algorithm,
string keyName, DataEncryptionCipher dataEncryptionCipher = DataEncryptionCipher.AesGcm,
CancellationToken cancellationToken = default) => EncryptStreamAsync(vaultResourceName, new MemoryStream(plaintextBytes),
CancellationToken cancellationToken = default) => EncryptStreamAsync(vaultResourceName, new MemoryStream(plaintextBytes.ToArray()),
algorithm, keyName, dataEncryptionCipher, string.Empty, true, cancellationToken);

/// <inheritdoc />
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public override Task<byte[]> EncryptAsync(
string vaultResourceName, byte[] plaintextBytes, KeyWrapAlgorithm algorithm,
public override Task<ReadOnlyMemory<byte>> EncryptAsync(
string vaultResourceName, ReadOnlyMemory<byte> plaintextBytes, KeyWrapAlgorithm algorithm,
string keyName, string decryptionKeyName,
DataEncryptionCipher dataEncryptionCipher = DataEncryptionCipher.AesGcm,
CancellationToken cancellationToken = default) => EncryptStreamAsync(vaultResourceName, new MemoryStream(plaintextBytes),
CancellationToken cancellationToken = default) => EncryptStreamAsync(vaultResourceName, new MemoryStream(plaintextBytes.ToArray()),
algorithm, keyName, dataEncryptionCipher,
decryptionKeyName, false, cancellationToken);

/// <inheritdoc />
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public override async Task<byte[]> DecryptAsync(string vaultResourceName, byte[] cipherTextBytes, string keyName, CancellationToken cancellationToken = default)
public override async Task<ReadOnlyMemory<byte>> DecryptAsync(string vaultResourceName, ReadOnlyMemory<byte> cipherTextBytes, string keyName, CancellationToken cancellationToken = default)
{
ArgumentVerifier.ThrowIfNullOrEmpty(vaultResourceName, nameof(vaultResourceName));
ArgumentVerifier.ThrowIfNullOrEmpty(keyName, nameof(keyName));
Expand Down Expand Up @@ -1517,7 +1517,8 @@ await duplexStream.RequestStream.WriteAsync(
{
Payload = new Autogenerated.StreamPayload
{
Data = ByteString.CopyFrom(cipherTextBytes, offset, count), Seq = (ulong)a
Data = ByteString.CopyFrom(cipherTextBytes.Span),
Seq = (ulong)a
}
}, cancellationToken);
}
Expand All @@ -1538,7 +1539,7 @@ await duplexStream.RequestStream.WriteAsync(

/// <inheritdoc />
[Obsolete("The API is currently not stable as it is in the Alpha stage. This attribute will be removed once it is stable.")]
public override async Task<byte[]> DecryptAsync(string vaultResourceName, Stream cipherTextStream, string keyName, CancellationToken cancellationToken = default)
public override async Task<ReadOnlyMemory<byte>> DecryptAsync(string vaultResourceName, Stream cipherTextStream, string keyName, CancellationToken cancellationToken = default)
{
ArgumentVerifier.ThrowIfNullOrEmpty(vaultResourceName, nameof(vaultResourceName));
ArgumentVerifier.ThrowIfNullOrEmpty(keyName, nameof(keyName));
Expand Down

0 comments on commit bd49006

Please sign in to comment.