/// <summary> /// Computes the hash value for the supplied <see cref="Stream"/> using the specified <see cref="YahooMediaHashAlgorithm"/>. /// </summary> /// <param name="stream">The input to compute the hash code for.</param> /// <param name="algorithm">A <see cref="YahooMediaHashAlgorithm"/> enumeration value that indicates the algorithm to use.</param> /// <returns>The <b>base64</b> encoded result of the computed hash code.</returns> /// <exception cref="ArgumentNullException">The <paramref name="stream"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentException">The <paramref name="algorithm"/> is equal to <see cref="YahooMediaHashAlgorithm.None"/>.</exception> public static string GenerateHash(Stream stream, YahooMediaHashAlgorithm algorithm) { string base64EncodedHash = String.Empty; MD5 md5 = MD5.Create(); SHA1 sha1 = SHA1.Create(); Guard.ArgumentNotNull(stream, "stream"); if (algorithm == YahooMediaHashAlgorithm.None) { throw new ArgumentException(String.Format(null, "Unable to generate a hash value for the {0} algorithm.", algorithm), "algorithm"); } if (algorithm == YahooMediaHashAlgorithm.MD5) { byte[] hash = md5.ComputeHash(stream); base64EncodedHash = Convert.ToBase64String(hash); } else if (algorithm == YahooMediaHashAlgorithm.Sha1) { byte[] hash = sha1.ComputeHash(stream); base64EncodedHash = Convert.ToBase64String(hash); } return(base64EncodedHash); }
/// <summary> /// Loads this <see cref="YahooMediaHash"/> using the supplied <see cref="XPathNavigator"/>. /// </summary> /// <param name="source">The <see cref="XPathNavigator"/> to extract information from.</param> /// <returns><b>true</b> if the <see cref="YahooMediaHash"/> was initialized using the supplied <paramref name="source"/>, otherwise <b>false</b>.</returns> /// <remarks> /// This method expects the supplied <paramref name="source"/> to be positioned on the XML element that represents a <see cref="YahooMediaHash"/>. /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> public bool Load(XPathNavigator source) { bool wasLoaded = false; Guard.ArgumentNotNull(source, "source"); if (source.HasAttributes) { string algorithmAttribute = source.GetAttribute("algo", String.Empty); if (!String.IsNullOrEmpty(algorithmAttribute)) { YahooMediaHashAlgorithm algorithm = YahooMediaHash.HashAlgorithmByName(algorithmAttribute); if (algorithm != YahooMediaHashAlgorithm.None) { this.Algorithm = algorithm; wasLoaded = true; } } } if (!String.IsNullOrEmpty(source.Value)) { this.Value = source.Value; wasLoaded = true; } return(wasLoaded); }
/// <summary> /// Returns the <see cref="YahooMediaHashAlgorithm"/> enumeration value that corresponds to the specified hash algorithm name. /// </summary> /// <param name="name">The name of the hash algorithm.</param> /// <returns>A <see cref="YahooMediaHashAlgorithm"/> enumeration value that corresponds to the specified string, otherwise returns <b>YahooMediaHashAlgorithm.None</b>.</returns> /// <remarks>This method disregards case of specified hash algorithm name.</remarks> /// <exception cref="ArgumentNullException">The <paramref name="name"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="name"/> is an empty string.</exception> public static YahooMediaHashAlgorithm HashAlgorithmByName(string name) { YahooMediaHashAlgorithm hashAlgorithm = YahooMediaHashAlgorithm.None; Guard.ArgumentNotNullOrEmptyString(name, "name"); foreach (System.Reflection.FieldInfo fieldInfo in typeof(YahooMediaHashAlgorithm).GetFields()) { if (fieldInfo.FieldType == typeof(YahooMediaHashAlgorithm)) { YahooMediaHashAlgorithm algorithm = (YahooMediaHashAlgorithm)Enum.Parse(fieldInfo.FieldType, fieldInfo.Name); object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(EnumerationMetadataAttribute), false); if (customAttributes != null && customAttributes.Length > 0) { EnumerationMetadataAttribute enumerationMetadata = customAttributes[0] as EnumerationMetadataAttribute; if (String.Compare(name, enumerationMetadata.AlternateValue, StringComparison.OrdinalIgnoreCase) == 0) { hashAlgorithm = algorithm; break; } } } } return(hashAlgorithm); }
/// <summary> /// Returns the hash algorithm identifier for the supplied <see cref="YahooMediaHashAlgorithm"/>. /// </summary> /// <param name="algorithm">The <see cref="YahooMediaHashAlgorithm"/> to get the hash algorithm identifier for.</param> /// <returns>The hash algorithm identifier for the supplied <paramref name="algorithm"/>, otherwise returns an empty string.</returns> public static string HashAlgorithmAsString(YahooMediaHashAlgorithm algorithm) { string name = String.Empty; foreach (System.Reflection.FieldInfo fieldInfo in typeof(YahooMediaHashAlgorithm).GetFields()) { if (fieldInfo.FieldType == typeof(YahooMediaHashAlgorithm)) { YahooMediaHashAlgorithm hashAlgorithm = (YahooMediaHashAlgorithm)Enum.Parse(fieldInfo.FieldType, fieldInfo.Name); if (hashAlgorithm == algorithm) { object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(EnumerationMetadataAttribute), false); if (customAttributes != null && customAttributes.Length > 0) { EnumerationMetadataAttribute enumerationMetadata = customAttributes[0] as EnumerationMetadataAttribute; name = enumerationMetadata.AlternateValue; break; } } } } return(name); }
//============================================================ // PUBLIC METHODS //============================================================ #region Load(XPathNavigator source) /// <summary> /// Loads this <see cref="YahooMediaHash"/> using the supplied <see cref="XPathNavigator"/>. /// </summary> /// <param name="source">The <see cref="XPathNavigator"/> to extract information from.</param> /// <returns><b>true</b> if the <see cref="YahooMediaHash"/> was initialized using the supplied <paramref name="source"/>, otherwise <b>false</b>.</returns> /// <remarks> /// This method expects the supplied <paramref name="source"/> to be positioned on the XML element that represents a <see cref="YahooMediaHash"/>. /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> public bool Load(XPathNavigator source) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ bool wasLoaded = false; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(source, "source"); //------------------------------------------------------------ // Attempt to extract syndication information //------------------------------------------------------------ if (source.HasAttributes) { string algorithmAttribute = source.GetAttribute("algo", String.Empty); if (!String.IsNullOrEmpty(algorithmAttribute)) { YahooMediaHashAlgorithm algorithm = YahooMediaHash.HashAlgorithmByName(algorithmAttribute); if (algorithm != YahooMediaHashAlgorithm.None) { this.Algorithm = algorithm; wasLoaded = true; } } } if (!String.IsNullOrEmpty(source.Value)) { this.Value = source.Value; wasLoaded = true; } return(wasLoaded); }
/// <summary> /// Returns the <see cref="YahooMediaHashAlgorithm"/> enumeration value that corresponds to the specified hash algorithm name. /// </summary> /// <param name="name">The name of the hash algorithm.</param> /// <returns>A <see cref="YahooMediaHashAlgorithm"/> enumeration value that corresponds to the specified string, otherwise returns <b>YahooMediaHashAlgorithm.None</b>.</returns> /// <remarks>This method disregards case of specified hash algorithm name.</remarks> /// <exception cref="ArgumentNullException">The <paramref name="name"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="name"/> is an empty string.</exception> public static YahooMediaHashAlgorithm HashAlgorithmByName(string name) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ YahooMediaHashAlgorithm hashAlgorithm = YahooMediaHashAlgorithm.None; //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNullOrEmptyString(name, "name"); //------------------------------------------------------------ // Determine syndication content format based on supplied name //------------------------------------------------------------ foreach (System.Reflection.FieldInfo fieldInfo in typeof(YahooMediaHashAlgorithm).GetFields()) { if (fieldInfo.FieldType == typeof(YahooMediaHashAlgorithm)) { YahooMediaHashAlgorithm algorithm = (YahooMediaHashAlgorithm)Enum.Parse(fieldInfo.FieldType, fieldInfo.Name); object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(EnumerationMetadataAttribute), false); if (customAttributes != null && customAttributes.Length > 0) { EnumerationMetadataAttribute enumerationMetadata = customAttributes[0] as EnumerationMetadataAttribute; if (String.Compare(name, enumerationMetadata.AlternateValue, StringComparison.OrdinalIgnoreCase) == 0) { hashAlgorithm = algorithm; break; } } } } return hashAlgorithm; }
/// <summary> /// Returns the hash algorithm identifier for the supplied <see cref="YahooMediaHashAlgorithm"/>. /// </summary> /// <param name="algorithm">The <see cref="YahooMediaHashAlgorithm"/> to get the hash algorithm identifier for.</param> /// <returns>The hash algorithm identifier for the supplied <paramref name="algorithm"/>, otherwise returns an empty string.</returns> public static string HashAlgorithmAsString(YahooMediaHashAlgorithm algorithm) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ string name = String.Empty; //------------------------------------------------------------ // Return alternate value based on supplied protocol //------------------------------------------------------------ foreach (System.Reflection.FieldInfo fieldInfo in typeof(YahooMediaHashAlgorithm).GetFields()) { if (fieldInfo.FieldType == typeof(YahooMediaHashAlgorithm)) { YahooMediaHashAlgorithm hashAlgorithm = (YahooMediaHashAlgorithm)Enum.Parse(fieldInfo.FieldType, fieldInfo.Name); if (hashAlgorithm == algorithm) { object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(EnumerationMetadataAttribute), false); if (customAttributes != null && customAttributes.Length > 0) { EnumerationMetadataAttribute enumerationMetadata = customAttributes[0] as EnumerationMetadataAttribute; name = enumerationMetadata.AlternateValue; break; } } } } return name; }
/// <summary> /// Computes the hash value for the supplied <see cref="Stream"/> using the specified <see cref="YahooMediaHashAlgorithm"/>. /// </summary> /// <param name="stream">The input to compute the hash code for.</param> /// <param name="algorithm">A <see cref="YahooMediaHashAlgorithm"/> enumeration value that indicates the algorithm to use.</param> /// <returns>The <b>base64</b> encoded result of the computed hash code.</returns> /// <exception cref="ArgumentNullException">The <paramref name="stream"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentException">The <paramref name="algorithm"/> is equal to <see cref="YahooMediaHashAlgorithm.None"/>.</exception> public static string GenerateHash(Stream stream, YahooMediaHashAlgorithm algorithm) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ string base64EncodedHash = String.Empty; MD5 md5 = MD5.Create(); SHA1 sha1 = SHA1.Create(); //------------------------------------------------------------ // Validate parameters //------------------------------------------------------------ Guard.ArgumentNotNull(stream, "stream"); if (algorithm == YahooMediaHashAlgorithm.None) { throw new ArgumentException(String.Format(null, "Unable to generate a hash value for the {0} algorithm.", algorithm), "algorithm"); } if(algorithm == YahooMediaHashAlgorithm.MD5) { byte[] hash = md5.ComputeHash(stream); base64EncodedHash = Convert.ToBase64String(hash); } else if (algorithm == YahooMediaHashAlgorithm.Sha1) { byte[] hash = sha1.ComputeHash(stream); base64EncodedHash = Convert.ToBase64String(hash); } return base64EncodedHash; }