Allow hash type on GenerateHash and remove obsoleted ToMd5 and ToSHA1 methods (#6679)

This commit is contained in:
Ronald Barendse
2019-10-23 17:19:49 +02:00
committed by Sebastiaan Janssen
parent e3bc30c4ca
commit d0303f74fd
5 changed files with 37 additions and 46 deletions

View File

@@ -724,67 +724,56 @@ namespace Umbraco.Core
/// <summary>
/// Generates a hash of a string based on the FIPS compliance setting.
/// </summary>
/// <param name="str">Refers to itself</param>
/// <returns>The hashed string</returns>
/// <param name="str">The <see cref="string" /> to hash.</param>
/// <returns>
/// The hashed string.
/// </returns>
public static string GenerateHash(this string str)
{
return CryptoConfig.AllowOnlyFipsAlgorithms
? str.ToSHA1()
: str.ToMd5();
return str.GenerateHash(CryptoConfig.AllowOnlyFipsAlgorithms ? "SHA1" : "MD5");
}
/// <summary>
/// Converts the string to MD5
/// Generate a hash of a string based on the specified hash algorithm.
/// </summary>
/// <param name="stringToConvert">Refers to itself</param>
/// <returns>The MD5 hashed string</returns>
[Obsolete("Please use the GenerateHash method instead. This may be removed in future versions")]
internal static string ToMd5(this string stringToConvert)
/// <typeparam name="T">The hash algorithm implementation to use.</typeparam>
/// <param name="str">The <see cref="string" /> to hash.</param>
/// <returns>
/// The hashed string.
/// </returns>
internal static string GenerateHash<T>(this string str)
where T : HashAlgorithm
{
return stringToConvert.GenerateHash("MD5");
return str.GenerateHash(typeof(T).FullName);
}
/// <summary>
/// Converts the string to SHA1
/// Generate a hash of a string based on the specified <paramref name="hashType" />.
/// </summary>
/// <param name="stringToConvert">refers to itself</param>
/// <returns>The SHA1 hashed string</returns>
[Obsolete("Please use the GenerateHash method instead. This may be removed in future versions")]
internal static string ToSHA1(this string stringToConvert)
/// <param name="str">The <see cref="string" /> to hash.</param>
/// <param name="hashType">The hash algorithm implementation to use.</param>
/// <returns>
/// The hashed string.
/// </returns>
/// <exception cref="System.InvalidOperationException">No hashing type found by name <paramref name="hashType" />.</exception>
/// <seealso cref="https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.hashalgorithm.create#System_Security_Cryptography_HashAlgorithm_Create_System_String_" />
internal static string GenerateHash(this string str, string hashType)
{
return stringToConvert.GenerateHash("SHA1");
}
/// <summary>Generate a hash of a string based on the hashType passed in
/// </summary>
/// <param name="str">Refers to itself</param>
/// <param name="hashType">String with the hash type. See remarks section of the CryptoConfig Class in MSDN docs for a list of possible values.</param>
/// <returns>The hashed string</returns>
private static string GenerateHash(this string str, string hashType)
{
//create an instance of the correct hashing provider based on the type passed in
var hasher = HashAlgorithm.Create(hashType);
if (hasher == null) throw new InvalidOperationException("No hashing type found by name " + hashType);
if (hasher == null) throw new InvalidOperationException($"No hashing type found by name {hashType}.");
using (hasher)
{
//convert our string into byte array
var byteArray = Encoding.UTF8.GetBytes(str);
//get the hashed values created by our selected provider
var hashedByteArray = hasher.ComputeHash(byteArray);
//create a StringBuilder object
var stringBuilder = new StringBuilder();
//loop to each byte
var sb = new StringBuilder();
foreach (var b in hashedByteArray)
{
//append it to our StringBuilder
stringBuilder.Append(b.ToString("x2"));
sb.Append(b.ToString("x2"));
}
//return the hashed value
return stringBuilder.ToString();
return sb.ToString();
}
}