Ver Mensaje Individual
  #2154  
Antiguo 24-07-2024
unomasmas unomasmas is offline
Miembro
 
Registrado: dic 2019
Posts: 194
Reputación: 7
unomasmas Va por buen camino
Cita:
Empezado por bmfranky Ver Mensaje
Código:
   public class Huella
    {
        /// <summary>
        /// Clase que devuelve el hash en mayusculas como nos indica la web
        /// </summary>
        /// <param name="source">Cadena a encriptar.</param>
        /// <returns>Hash de la cadena.</returns>public string creaHuella(string source)
        {
            string hash = "";
            using (SHA256 sha256Hash = SHA256.Create())
            {
                hash = GetHash(sha256Hash, source);
                return hash.ToUpper();
              }
        }

         private static string GetHash(HashAlgorithm hashAlgorithm, string input)
        {

            // Convert the input string to a byte array and compute the hash.
            byte[] data = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            var sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        // Verify a hash against a string.
        private static bool VerifyHash(HashAlgorithm hashAlgorithm, string input, string hash)
        {
            // Hash the input.
            var hashOfInput = GetHash(hashAlgorithm, input);

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            return comparer.Compare(hashOfInput, hash) == 0;
        }
    }
¡Muchísimas gracias! ¡Estupendo aporte!