public void MD5ComparisonTest() { CloudStorageAccount.UseV1MD5 = false; try { using (MD5Wrapper nativeHash = new MD5Wrapper()) { nativeHash.UpdateHash(data, 0, data.Length); string nativeResult = nativeHash.ComputeHash(); MD5 hash = MD5.Create(); hash.TransformBlock(data, 0, data.Length, null, 0); hash.TransformFinalBlock(new byte[0], 0, 0); byte[] bytes = hash.Hash; string result = Convert.ToBase64String(bytes); Assert.AreEqual(nativeResult, result); } } finally { CloudStorageAccount.UseV1MD5 = true; } }
private string GenerateBlockIdPrefix() { //var blockIdPrefix = Guid.NewGuid().ToString("N") + "-"; // Originally the blockId is an GUID + "-". It will cause some problem when switch machines or jnl get cleaned // to upload to the same block blob - block id is not shared between the 2 DMLib instances // and it may result in reaching the limitation of maximum 50000 uncommited blocks + 50000 committed blocks. // // Change it to hash based prefix to make it preditable and can be shared between multiple DMLib instances string blobNameHash; using (var md5 = new MD5Wrapper()) { var blobNameBytes = Encoding.UTF8.GetBytes(this.destLocation.Blob.Name); md5.UpdateHash(blobNameBytes, 0, blobNameBytes.Length); blobNameHash = md5.ComputeHash(); } // The original GUID format prefix's length is 32 + 1 ("-") // As the service requires the blockid has the same size of each block, // To keep the compatibility, add 9 chars to the end of the hash ( 33 - 24) var blockIdPrefix = blobNameHash + "12345678-"; return(blockIdPrefix); }
public void MD5SingleByteTest() { MD5Wrapper nativeHash = new MD5Wrapper(); nativeHash.UpdateHash(data, 3, 2); string nativeResult = nativeHash.ComputeHash(); MD5 hash = MD5.Create(); hash.TransformBlock(data, 3, 2, null, 0); hash.TransformFinalBlock(new byte[0], 0, 0); var bytes = hash.Hash; string result = Convert.ToBase64String(bytes); Assert.AreEqual(nativeResult, result); }
public void MD5V1ComparisonTest() { using (MD5Wrapper nativeHash = new MD5Wrapper()) { nativeHash.UpdateHash(data, 0, data.Length); string nativeResult = nativeHash.ComputeHash(); MD5 hash = MD5.Create(); hash.TransformBlock(data, 0, data.Length, null, 0); hash.TransformFinalBlock(new byte[0], 0, 0); byte[] bytes = hash.Hash; string result = Convert.ToBase64String(bytes); Assert.AreEqual(nativeResult, result); } }
public void MD5EmptyArrayTest() { byte[] data = new byte[] { }; MD5Wrapper nativeHash = new MD5Wrapper(); nativeHash.UpdateHash(data, 0, data.Length); string nativeResult = nativeHash.ComputeHash(); MD5 hash = MD5.Create(); hash.ComputeHash(data, 0, data.Length); var varResult = hash.Hash; string result = Convert.ToBase64String(varResult); Assert.AreEqual(nativeResult, result); }
/// <summary> /// Computes the hash value for this stream. /// </summary> /// <returns>String representation of the computed hash value.</returns> public string ComputeMD5Hash() { using (MD5Wrapper md5 = new MD5Wrapper()) { // Maximum amount you can read is from current spot to the end. long leftToRead = this.Length - this.Position; while (leftToRead != 0) { ArraySegment <byte> currentBlock = this.GetCurrentBlock(); // Update hash with the block int blockReadLength = (int)Math.Min(leftToRead, currentBlock.Count); md5.UpdateHash(currentBlock.Array, currentBlock.Offset, blockReadLength); this.AdvancePosition(ref leftToRead, blockReadLength); } return(md5.ComputeHash()); } }
/// <summary> /// Uploads the block list. /// </summary> /// <param name="blocks">The blocks to upload.</param> /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the access conditions for the blob. If <c>null</c>, no condition is used.</param> /// <param name="options">A <see cref="BlobRequestOptions"/> object that specifies any additional options for the request.</param> /// <returns>A <see cref="RESTCommand"/> that uploads the block list.</returns> internal RESTCommand<NullType> PutBlockListImpl(IEnumerable<PutBlockListItem> blocks, AccessCondition accessCondition, BlobRequestOptions options) { MemoryStream memoryStream = new MemoryStream(); BlobRequest.WriteBlockListBody(blocks, memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); string contentMD5; using (MD5Wrapper md5 = new MD5Wrapper()) { contentMD5 = md5.ComputeHash(memoryStream); } RESTCommand<NullType> putCmd = new RESTCommand<NullType>(this.ServiceClient.Credentials, this.Uri); putCmd.ApplyRequestOptions(options); putCmd.Handler = this.ServiceClient.AuthenticationHandler; putCmd.BuildClient = HttpClientFactory.BuildHttpClient; putCmd.BuildContent = (cmd, ctx) => HttpContentFactory.BuildContentFromStream(memoryStream, 0, memoryStream.Length, contentMD5, cmd, ctx); putCmd.BuildRequest = (cmd, cnt, ctx) => { HttpRequestMessage msg = BlobHttpRequestMessageFactory.PutBlockList(cmd.Uri, cmd.ServerTimeoutInSeconds, this.Properties, accessCondition, cnt, ctx); BlobHttpRequestMessageFactory.AddMetadata(msg, this.Metadata); return msg; }; putCmd.PreProcessResponse = (cmd, resp, ex, ctx) => { HttpResponseParsers.ProcessExpectedStatusCodeNoException(HttpStatusCode.Created, resp, NullType.Value, cmd, ex, ctx); CloudBlobSharedImpl.ParseSizeAndLastModified(this.attributes, resp); this.Properties.Length = 0; return NullType.Value; }; return putCmd; }
/// <summary> /// Uploads the block list. /// </summary> /// <param name="blocks">The blocks to upload.</param> /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the access conditions for the blob. If <c>null</c>, no condition is used.</param> /// <param name="options">A <see cref="BlobRequestOptions"/> object that specifies any additional options for the request.</param> /// <returns>A <see cref="RESTCommand{T}"/> that uploads the block list.</returns> internal RESTCommand<NullType> PutBlockListImpl(IEnumerable<PutBlockListItem> blocks, AccessCondition accessCondition, BlobRequestOptions options) { MemoryStream memoryStream = new MemoryStream(); BlobRequest.WriteBlockListBody(blocks, memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); string contentMD5; using (MD5Wrapper md5 = new MD5Wrapper()) { contentMD5 = md5.ComputeHash(memoryStream); } memoryStream.Seek(0, SeekOrigin.Begin); RESTCommand<NullType> putCmd = new RESTCommand<NullType>(this.ServiceClient.Credentials, this.Uri); putCmd.ApplyRequestOptions(options); putCmd.BuildRequestDelegate = (uri, builder, serverTimeout, ctx) => BlobHttpWebRequestFactory.PutBlockList(uri, serverTimeout, this.Properties, accessCondition, ctx); putCmd.SetHeaders = (r, ctx) => { r.Headers.Set(HttpRequestHeader.ContentMd5, contentMD5); BlobHttpWebRequestFactory.AddMetadata(r, this.Metadata); }; putCmd.SendStream = memoryStream; putCmd.RecoveryAction = RecoveryActions.RewindStream; putCmd.SignRequest = this.ServiceClient.AuthenticationHandler.SignRequest; putCmd.PreProcessResponse = (cmd, resp, ex, ctx) => { HttpResponseParsers.ProcessExpectedStatusCodeNoException(HttpStatusCode.Created, resp, NullType.Value, cmd, ex, ctx); CloudBlobSharedImpl.ParseSizeAndLastModified(this.attributes, resp); this.Properties.Length = 0; return NullType.Value; }; return putCmd; }
/// <summary> /// Computes the hash value for this stream. /// </summary> /// <returns>String representation of the computed hash value.</returns> public string ComputeMD5Hash() { using (MD5Wrapper md5 = new MD5Wrapper()) { // Maximum amount you can read is from current spot to the end. long leftToRead = this.Length - this.Position; while (leftToRead != 0) { ArraySegment<byte> currentBlock = this.GetCurrentBlock(); // Update hash with the block int blockReadLength = (int)Math.Min(leftToRead, currentBlock.Count); md5.UpdateHash(currentBlock.Array, currentBlock.Offset, blockReadLength); this.AdvancePosition(ref leftToRead, blockReadLength); } return md5.ComputeHash(); } }
public void MD5BigDataTest() { byte[] data = new byte[10000]; for (int i = 1; i < 10000; i++) { data[i] = 1; } using (MD5Wrapper nativeHash = new MD5Wrapper()) { MD5 hash = MD5.Create(); for (int i = 0; i < 999; i++) { int index = 10 * i; nativeHash.UpdateHash(data, 0, 10); hash.TransformBlock(data, 0, 10, null, 0); } string nativeResult = nativeHash.ComputeHash(); hash.TransformFinalBlock(new byte[0], 0, 0); var varResult = hash.Hash; String result = Convert.ToBase64String(varResult); Assert.AreEqual(nativeResult, result); } }
public void MD5LastByteTest() { using (MD5Wrapper nativeHash = new MD5Wrapper()) { nativeHash.UpdateHash(data, 8, 1); string nativeResult = nativeHash.ComputeHash(); MD5 hash = MD5.Create(); hash.ComputeHash(data, 8, 1); var varResult = hash.Hash; string result = Convert.ToBase64String(varResult); Assert.AreEqual(nativeResult, result); } }
public void MD5LastByteTest() { CloudStorageAccount.UseV1MD5 = false; try { using (MD5Wrapper nativeHash = new MD5Wrapper()) { nativeHash.UpdateHash(data, 8, 1); string nativeResult = nativeHash.ComputeHash(); MD5 hash = MD5.Create(); hash.ComputeHash(data, 8, 1); byte[] varResult = hash.Hash; string result = Convert.ToBase64String(varResult); Assert.AreEqual(nativeResult, result); } } finally { CloudStorageAccount.UseV1MD5 = true; } }