| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.IO; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Azure.Storage.Cryptography; |
| | 10 | | using Azure.Storage.Cryptography.Models; |
| | 11 | | using Metadata = System.Collections.Generic.IDictionary<string, string>; |
| | 12 | |
|
| | 13 | | namespace Azure.Storage.Blobs |
| | 14 | | { |
| | 15 | | internal class BlobClientSideEncryptor |
| | 16 | | { |
| | 17 | | private readonly ClientSideEncryptor _encryptor; |
| | 18 | |
|
| 0 | 19 | | public BlobClientSideEncryptor(ClientSideEncryptor encryptor) |
| | 20 | | { |
| 0 | 21 | | _encryptor = encryptor; |
| 0 | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Applies client-side encryption to the data for upload. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="content"> |
| | 28 | | /// Content to encrypt. |
| | 29 | | /// </param> |
| | 30 | | /// <param name="metadata"> |
| | 31 | | /// Metadata to add encryption metadata to. |
| | 32 | | /// </param> |
| | 33 | | /// <param name="async"> |
| | 34 | | /// Whether to perform this operation asynchronously. |
| | 35 | | /// </param> |
| | 36 | | /// <param name="cancellationToken"> |
| | 37 | | /// Cancellation token. |
| | 38 | | /// </param> |
| | 39 | | /// <returns>Transformed content stream and metadata.</returns> |
| | 40 | | public async Task<(Stream, Metadata)> ClientSideEncryptInternal( |
| | 41 | | Stream content, |
| | 42 | | Metadata metadata, |
| | 43 | | bool async, |
| | 44 | | CancellationToken cancellationToken) |
| | 45 | | { |
| 0 | 46 | | (Stream nonSeekableCiphertext, EncryptionData encryptionData) = await _encryptor.EncryptInternal( |
| 0 | 47 | | content, |
| 0 | 48 | | async, |
| 0 | 49 | | cancellationToken).ConfigureAwait(false); |
| | 50 | |
|
| 0 | 51 | | metadata ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 0 | 52 | | metadata[Constants.ClientSideEncryption.EncryptionDataKey] = EncryptionDataSerializer.Serialize(encryptionDa |
| | 53 | |
|
| 0 | 54 | | return (nonSeekableCiphertext, metadata); |
| 0 | 55 | | } |
| | 56 | | } |
| | 57 | | } |