This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Added MD5 support

Added the ComputeMD5Hash and ComputeHMACMD5Hash extension methods to the ByteArrayExtensions
  • Loading branch information...
PedroLamas committed Jun 1, 2012
1 parent e03c325 commit 8311f64ce37eb1b454250e6755278d173d2b928d
@@ -79,6 +79,9 @@
<Compile Include="Extensions\IntExtensions.cs" />
<Compile Include="Extensions\StreamReaderExtensions.cs" />
<Compile Include="Extensions\StreamWriterExtensions.cs" />
+ <Compile Include="System\Security\Cryptography\HMACMD5.cs" />
+ <Compile Include="System\Security\Cryptography\MD5.cs" />
+ <Compile Include="System\Security\Cryptography\MD5Managed.cs" />
<Compile Include="Services\ApplicationLifecycleService.cs" />
<Compile Include="Services\ApplicationManifestService.cs" />
<Compile Include="Services\ApplicationSettingsService.cs" />
@@ -171,6 +174,7 @@
<SubType>Designer</SubType>
</None>
</ItemGroup>
+ <ItemGroup />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight for Phone\$(TargetFrameworkVersion)\Microsoft.Silverlight.$(TargetFrameworkProfile).Overrides.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight for Phone\$(TargetFrameworkVersion)\Microsoft.Silverlight.CSharp.targets" />
<ProjectExtensions />
@@ -14,6 +14,7 @@
// ****************************************************************************
using System;
+using System.Linq;
using System.Security.Cryptography;
using System.Text;
@@ -101,6 +102,34 @@ public static byte[] ComputeSHA256Hash(this byte[] input, int offset, int count)
}
}
+ /// <summary>
+ /// Computes the <see cref="MD5"/> hash for the current byte array using the managed library.
+ /// </summary>
+ /// <param name="input">An array of 8-bit unsigned integers.</param>
+ /// <returns>The computed hash code.</returns>
+ public static byte[] ComputeMD5Hash(this byte[] input)
+ {
+ using (var hash = new MD5Managed())
+ {
+ return hash.ComputeHash(input);
+ }
+ }
+
+ /// <summary>
+ /// Computes the <see cref="MD5"/> hash for the current byte array using the managed library.
+ /// </summary>
+ /// <param name="input">An array of 8-bit unsigned integers.</param>
+ /// <param name="offset">The offset into the byte array from which to begin using data.</param>
+ /// <param name="count">The number of bytes in the array to use as data.</param>
+ /// <returns>The computed hash code.</returns>
+ public static byte[] ComputeMD5Hash(this byte[] input, int offset, int count)
+ {
+ using (var hash = new MD5Managed())
+ {
+ return hash.ComputeHash(input, offset, count);
+ }
+ }
+
/// <summary>
/// Computes the <see cref="HMACSHA1"/> hash for the current byte array using the managed library.
/// </summary>
@@ -168,5 +197,38 @@ public static byte[] ComputeHMACSHA256Hash(this byte[] input, byte[] key, int of
return hash.ComputeHash(input, offset, count);
}
}
+
+ /// <summary>
+ /// Computes the <see cref="HMACMD5"/> hash for the current byte array using the managed library.
+ /// </summary>
+ /// <param name="input">An array of 8-bit unsigned integers.</param>
+ /// <param name="key">The key to use in the hash algorithm.</param>
+ /// <returns>The computed hash code.</returns>
+ public static byte[] ComputeHMACMD5Hash(this byte[] input, byte[] key)
+ {
+ var hash = new HMACMD5(key);
+
+ return hash.ComputeHash(input);
+ }
+
+ /// <summary>
+ /// Computes the <see cref="HMACMD5"/> hash for the current byte array using the managed library.
+ /// </summary>
+ /// <param name="input">An array of 8-bit unsigned integers.</param>
+ /// <param name="key">The key to use in the hash algorithm.</param>
+ /// <param name="offset">The offset into the byte array from which to begin using data.</param>
+ /// <param name="count">The number of bytes in the array to use as data.</param>
+ /// <returns>The computed hash code.</returns>
+ public static byte[] ComputeHMACMD5Hash(this byte[] input, byte[] key, int offset, int count)
+ {
+ var hash = new HMACMD5(key);
+
+ input = input
+ .Skip(offset)
+ .Take(count)
+ .ToArray();
+
+ return hash.ComputeHash(input);
+ }
}
}
@@ -0,0 +1,211 @@
+// ****************************************************************************
+// <copyright file="HMACMD5.cs" company="xBrainLab">
+// Copyright (c) 2011 xBrainLab. All rights reserved.
+// </copyright>
+// <author>Sebastien Warin</author>
+// ****************************************************************************
+// <auto-generated />
+
+namespace System.Security.Cryptography
+{
+ using System;
+ using System.Text;
+
+ /// <summary>
+ /// Computes a Hash-based Message Authentication Code (HMAC) using the <see cref="T:xBrainLab.Security.Cryptography.MD5" /> hash function
+ /// </summary>
+ public sealed class HMACMD5
+ {
+ private const int BLOCK_SIZE = 64;
+
+ private byte[] m_Key = null;
+ private byte[] m_inner = null;
+ private byte[] m_outer = null;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="HMACMD5"/> class using the supplied key with UT8 encoding.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ public HMACMD5(string key)
+ : this(key, Encoding.UTF8)
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="HMACMD5"/> class using the supplied key with supplied encoding.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ /// <param name="encoding">The encoding used to read the key.</param>
+ public HMACMD5(string key, Encoding encoding)
+ : this(encoding.GetBytes(key))
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="HMACMD5"/> class the supplied key.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ public HMACMD5(byte[] key)
+ {
+ this.InitializeKey(key);
+ }
+
+ /// <summary>
+ /// Gets or sets the key.
+ /// </summary>
+ /// <value>
+ /// The key.
+ /// </value>
+ public byte[] Key
+ {
+ get
+ {
+ return this.m_Key;
+ }
+ set
+ {
+ this.InitializeKey(value);
+ }
+ }
+
+ /// <summary>
+ /// Computes the hash value for the specified string (UTF8 default encoding).
+ /// </summary>
+ /// <param name="buffer">The input to compute the hash code for. </param>
+ /// <returns>The computed hash code</returns>
+ public byte[] ComputeHash(string buffer)
+ {
+ return this.ComputeHash(buffer, Encoding.UTF8);
+ }
+
+ /// <summary>
+ /// Computes the hash value for the specified string.
+ /// </summary>
+ /// <param name="buffer">The input to compute the hash code for.</param>
+ /// <param name="encoding">The encoding.</param>
+ /// <returns>
+ /// The computed hash code
+ /// </returns>
+ public byte[] ComputeHash(string buffer, Encoding encoding)
+ {
+ return this.ComputeHash(encoding.GetBytes(buffer));
+ }
+
+ /// <summary>
+ /// Computes the hash value for the specified byte array.
+ /// </summary>
+ /// <param name="buffer">The input to compute the hash code for.</param>
+ /// <returns>
+ /// The computed hash code
+ /// </returns>
+ public byte[] ComputeHash(byte[] buffer)
+ {
+ if (buffer == null)
+ {
+ throw new ArgumentNullException("buffer", "The input cannot be null.");
+ }
+
+ return MD5Core.GetHash(this.Combine(this.m_outer, MD5Core.GetHash(this.Combine(this.m_inner, buffer))));
+ }
+
+ /// <summary>
+ /// Computes the hash for the specified string (UTF8 default encoding) to base64 string.
+ /// </summary>
+ /// <param name="buffer">The input to compute the hash code for.</param>
+ /// <returns>The computed hash code in base64 string</returns>
+ public string ComputeHashToBase64String(string buffer)
+ {
+ return Convert.ToBase64String(this.ComputeHash(buffer, Encoding.UTF8));
+ }
+
+ /// <summary>
+ /// Computes the hash for the specified string to base64 string.
+ /// </summary>
+ /// <param name="buffer">The input to compute the hash code for.</param>
+ /// <param name="encoding">The encoding.</param>
+ /// <returns>
+ /// The computed hash code in base64 string
+ /// </returns>
+ public string ComputeHashToBase64String(string buffer, Encoding encoding)
+ {
+ return Convert.ToBase64String(this.ComputeHash(buffer, encoding));
+ }
+
+ /// <summary>
+ /// Initializes the key.
+ /// </summary>
+ /// <param name="key">The key.</param>
+ private void InitializeKey(byte[] key)
+ {
+ if (key == null)
+ {
+ throw new ArgumentNullException("key", "The Key cannot be null.");
+ }
+
+ if (key.Length > BLOCK_SIZE)
+ {
+ this.m_Key = MD5Core.GetHash(key);
+ }
+ else
+ {
+ this.m_Key = key;
+ }
+
+ this.UpdateIOPadBuffers();
+ }
+
+ /// <summary>
+ /// Updates the IO pad buffers.
+ /// </summary>
+ private void UpdateIOPadBuffers()
+ {
+ if (this.m_inner == null)
+ {
+ this.m_inner = new byte[BLOCK_SIZE];
+ }
+
+ if (this.m_outer == null)
+ {
+ this.m_outer = new byte[BLOCK_SIZE];
+ }
+
+ for (int i = 0; i < BLOCK_SIZE; i++)
+ {
+ this.m_inner[i] = 54;
+ this.m_outer[i] = 92;
+ }
+
+ for (int i = 0; i < this.Key.Length; i++)
+ {
+ byte[] s1 = this.m_inner;
+ int s2 = i;
+ s1[s2] ^= this.Key[i];
+ byte[] s3 = this.m_outer;
+ int s4 = i;
+ s3[s4] ^= this.Key[i];
+ }
+ }
+
+ /// <summary>
+ /// Combines two array (a1 and a2).
+ /// </summary>
+ /// <param name="a1">The Array 1.</param>
+ /// <param name="a2">The Array 2.</param>
+ /// <returns>Combinaison of a1 and a2</returns>
+ private byte[] Combine(byte[] a1, byte[] a2)
+ {
+ byte[] final = new byte[a1.Length + a2.Length];
+ for (int i = 0; i < a1.Length; i++)
+ {
+ final[i] = a1[i];
+ }
+
+ for (int i = 0; i < a2.Length; i++)
+ {
+ final[i + a1.Length] = a2[i];
+ }
+
+ return final;
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 8311f64

Please sign in to comment.