// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Encapsulates a series of time saving extension methods to the class. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Extensions { #region Using using System; using System.Globalization; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; #endregion /// /// Encapsulates a series of time saving extension methods to the class. /// public static class StringExtensions { #region Cryptography /// /// Creates an MD5 fingerprint of the String. /// /// The String instance that this method extends. /// An MD5 fingerprint of the String. public static string ToMD5Fingerprint(this string expression) { byte[] bytes = Encoding.Unicode.GetBytes(expression.ToCharArray()); using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) { byte[] hash = md5.ComputeHash(bytes); // Concatenate the hash bytes into one long String. return hash.Aggregate( new StringBuilder(32), (sb, b) => sb.Append(b.ToString("X2", CultureInfo.InvariantCulture))) .ToString().ToLowerInvariant(); } } /// /// Creates an SHA1 fingerprint of the String. /// /// The String instance that this method extends. /// An SHA1 fingerprint of the String. public static string ToSHA1Fingerprint(this string expression) { byte[] bytes = Encoding.ASCII.GetBytes(expression.ToCharArray()); using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider()) { byte[] hash = sha1.ComputeHash(bytes); // Concatenate the hash bytes into one long String. return hash.Aggregate( new StringBuilder(40), (sb, b) => sb.Append(b.ToString("X2", CultureInfo.InvariantCulture))) .ToString().ToLowerInvariant(); } } /// /// Creates an SHA256 fingerprint of the String. /// /// The String instance that this method extends. /// An SHA256 fingerprint of the String. public static string ToSHA256Fingerprint(this string expression) { byte[] bytes = Encoding.ASCII.GetBytes(expression.ToCharArray()); using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider()) { byte[] hash = sha256.ComputeHash(bytes); // Concatenate the hash bytes into one long String. return hash.Aggregate( new StringBuilder(64), (sb, b) => sb.Append(b.ToString("X2", CultureInfo.InvariantCulture))) .ToString().ToLowerInvariant(); } } /// /// Creates an SHA512 fingerprint of the String. /// /// The String instance that this method extends. /// An SHA256 fingerprint of the String. public static string ToSHA512Fingerprint(this string expression) { byte[] bytes = Encoding.ASCII.GetBytes(expression.ToCharArray()); using (SHA512CryptoServiceProvider sha512 = new SHA512CryptoServiceProvider()) { byte[] hash = sha512.ComputeHash(bytes); // Concatenate the hash bytes into one long String. return hash.Aggregate( new StringBuilder(70), (sb, b) => sb.Append(b.ToString("X2", CultureInfo.InvariantCulture))) .ToString().ToLowerInvariant(); } } #endregion #region Numbers /// /// Creates an array of integers scraped from the String. /// /// The String instance that this method extends. /// An array of integers scraped from the String. public static int[] ToPositiveIntegerArray(this string expression) { if (string.IsNullOrWhiteSpace(expression)) { throw new ArgumentNullException("expression"); } Regex regex = new Regex(@"\d+", RegexOptions.Compiled); MatchCollection matchCollection = regex.Matches(expression); // Get the collections. int count = matchCollection.Count; int[] matches = new int[count]; // Loop and parse the int values. for (int i = 0; i < count; i++) { matches[i] = int.Parse(matchCollection[i].Value); } return matches; } #endregion #region Files and Paths /// /// Checks the string to see whether the value is a valid virtual path name. /// /// The String instance that this method extends. /// True if the given string is a valid virtual path name public static bool IsValidVirtualPathName(this string expression) { Uri uri; return Uri.TryCreate(expression, UriKind.Relative, out uri) && uri.IsWellFormedOriginalString(); } #endregion } }