Skip to content

Commit 06fe1b0

Browse files
authored
[Storage] Remove MD5 related code from B/F upload cmdlet. (#27666)
1 parent e009e76 commit 06fe1b0

File tree

5 files changed

+15
-67
lines changed

5 files changed

+15
-67
lines changed

src/Storage/Storage.Management/ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Removed MD5 from blob and file upload in some scenarios
22+
- `Set-AzStorageFileContent`
23+
- `Set-AzStorageBlobContent`
2124

2225
## Version 8.4.0
2326
* When create Storage context, allowed user input StorageAccountName

src/Storage/Storage/Blob/Cmdlet/SetAzureStorageBlobContent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
using Azure.Storage.Blobs.Specialized;
3737
using Azure;
3838
using System.Linq;
39+
using System.Runtime.InteropServices;
3940

4041
namespace Microsoft.WindowsAzure.Commands.Storage.Blob
4142
{
@@ -699,8 +700,7 @@ public override void ExecuteCmdlet()
699700
ValidateBlobProperties(BlobProperties);
700701
}
701702

702-
// if FIPS policy is enabled, must use native MD5
703-
if (fipsEnabled)
703+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
704704
{
705705
CloudStorageAccount.UseV1MD5 = false;
706706
}

src/Storage/Storage/Blob/StorageDataMovementCmdletBase.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace Microsoft.WindowsAzure.Commands.Storage.Blob
2323
using OpContext = Microsoft.Azure.Storage.OperationContext;
2424
using System.Collections.Generic;
2525
using System.Security.Cryptography;
26+
using Microsoft.Azure.Documents;
2627

2728
public class StorageDataMovementCmdletBase : StorageCloudBlobCmdletBase, IDisposable
2829
{
@@ -199,18 +200,18 @@ public static string[] GetBlockIDs(long contentLength, long blockLength, string
199200
blockCount++;
200201
}
201202
List<string> blockIDs = new List<string>();
202-
string blockIdPrefix = Convert.ToBase64String(MD5.Create().ComputeHash(System.Text.Encoding.UTF8.GetBytes(blobname)));
203203
for (int i = 0; i < (int)blockCount; i++)
204204
{
205-
string idNo = i.ToString();
206-
while (idNo.Length < 5)
207-
{
208-
idNo = "0" + idNo;
209-
}
210-
string blockID = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(blockIdPrefix + idNo));
205+
string blockID = GenerateBlockId(i * blockLength);
211206
blockIDs.Add(blockID);
212207
}
213208
return blockIDs.ToArray();
214209
}
210+
public static string GenerateBlockId(long offset)
211+
{
212+
byte[] id = new byte[48]; // 48 raw bytes => 64 byte string once Base64 encoded
213+
BitConverter.GetBytes(offset).CopyTo(id, 0);
214+
return Convert.ToBase64String(id);
215+
}
215216
}
216217
}

src/Storage/Storage/Common/StorageCloudCmdletBase.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -670,23 +670,5 @@ protected override void StopProcessing()
670670
_cancellationTokenSource.Cancel();
671671
base.StopProcessing();
672672
}
673-
674-
/// <summary>
675-
/// true if FIPS policy is enabled on the current machine
676-
/// </summary>
677-
public static bool fipsEnabled { get; } = IsFIPSEnabled();
678-
679-
internal static bool IsFIPSEnabled()
680-
{
681-
try
682-
{
683-
System.Security.Cryptography.MD5.Create();
684-
return false;
685-
}
686-
catch (System.Reflection.TargetInvocationException)
687-
{
688-
return true;
689-
}
690-
}
691673
}
692674
}

src/Storage/Storage/File/Cmdlet/SetAzureStorageFileContent.cs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ namespace Microsoft.WindowsAzure.Commands.Storage.File.Cmdlet
2222
using Microsoft.Azure.Storage.DataMovement;
2323
using Microsoft.Azure.Storage.File;
2424
using Microsoft.WindowsAzure.Commands.Common;
25-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2625
using Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel;
2726
using Microsoft.WindowsAzure.Commands.Storage.Common;
2827
using Microsoft.WindowsAzure.Commands.Utilities.Common;
@@ -37,7 +36,6 @@ namespace Microsoft.WindowsAzure.Commands.Storage.File.Cmdlet
3736
using System.Threading.Tasks;
3837
using LocalConstants = Microsoft.WindowsAzure.Commands.Storage.File.Constants;
3938

40-
[CmdletOutputBreakingChangeWithVersion(typeof(AzureStorageFile), "14.0.0", "9.0.0", ChangeDescription = "The ContentHash properties will be removed from the uploaded Azure file when file size > 1TB, or upload with Oauth credencial, or with -DisAllowTrailingDot.")]
4139
[Cmdlet("Set", Azure.Commands.ResourceManager.Common.AzureRMConstants.AzurePrefix + "StorageFileContent", SupportsShouldProcess = true, DefaultParameterSetName = LocalConstants.ShareNameParameterSetName), OutputType(typeof(AzureStorageFile))]
4240
public class SetAzureStorageFileContent : StorageFileDataManagementCmdletBase, IDynamicParameters
4341
{
@@ -130,17 +128,9 @@ public override void ExecuteCmdlet()
130128
}
131129
long fileSize = localFile.Length;
132130

133-
// if FIPS policy is enabled, must use native MD5 for DMlib.
134-
if (fipsEnabled)
131+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
135132
{
136-
if (fileSize < sizeTB)
137-
{
138-
CloudStorageAccount.UseV1MD5 = false;
139-
}
140-
else // use Track2 SDK
141-
{
142-
WriteWarning("The uploaded file won't have Content MD5 hash, since caculate MD5 hash fail, most possiblly caused by FIPS is enabled on this machine.");
143-
}
133+
CloudStorageAccount.UseV1MD5 = false;
144134
}
145135

146136
bool isDirectory;
@@ -251,12 +241,6 @@ await fileClient.CreateAsync(fileSize,
251241

252242
List<Task> runningTasks = new List<Task>();
253243

254-
IncrementalHash hash = null;
255-
if (!fipsEnabled)
256-
{
257-
hash = IncrementalHash.CreateHash(HashAlgorithmName.MD5);
258-
}
259-
260244
using (FileStream stream = File.OpenRead(localFile.FullName))
261245
{
262246
byte[] buffer = null;
@@ -268,10 +252,6 @@ await fileClient.CreateAsync(fileSize,
268252
buffer = new byte[targetBlockSize];
269253

270254
int actualBlockSize = await stream.ReadAsync(buffer: buffer, offset: 0, count: (int)targetBlockSize);
271-
if (!fipsEnabled && hash != null)
272-
{
273-
hash.AppendData(buffer, 0, actualBlockSize);
274-
}
275255

276256
Task task = UploadFileRangAsync(fileClient,
277257
new HttpRange(offset, actualBlockSize),
@@ -305,24 +285,6 @@ await fileClient.CreateAsync(fileSize,
305285
await Task.WhenAll(runningTasks).ConfigureAwait(false);
306286
}
307287

308-
// Need set file ContentHash
309-
if ((!fipsEnabled && hash != null))
310-
{
311-
ShareFileHttpHeaders header = null;
312-
if (!fipsEnabled && hash != null)
313-
{
314-
header = new ShareFileHttpHeaders();
315-
header.ContentHash = hash.GetHashAndReset();
316-
}
317-
318-
// set file header and attributes to the file
319-
ShareFileSetHttpHeadersOptions httpHeadersOptions = new ShareFileSetHttpHeadersOptions
320-
{
321-
HttpHeaders = header,
322-
};
323-
fileClient.SetHttpHeaders(httpHeadersOptions);
324-
}
325-
326288
if (this.PassThru)
327289
{
328290
// TODO: should make sure track1 file object attributes get?

0 commit comments

Comments
 (0)