ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

HashBy

Implict hashing

Source

public static byte[] HashBy<T>(this byte[] x) where T : HashAlgorithm {
			HashAlgorithm algo;
			try {
				algo = typeof(T)
					.GetMethod("Create", BindingFlags.Public | BindingFlags.Static, null, new Type[] { }, null)
						.Invoke(null, null) as HashAlgorithm;
			}
			catch {
				algo = Activator.CreateInstance<T>();
			}
			return algo.ComputeHash(x);
		}

Example

Console.WriteLine("Hello World!".ToByteArray().HashBy<MD5>());

Author: cia48621793

Submitted on: 28 apr. 2016

Language: C#

Type: HashAlgorithm

Views: 3194