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> /// 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; } }