1

I have a class where it is responsible for generating a hash according to what I send to it. However, the return of the SHA256Managed.Create(text) method is always returning null.

The following is my code:

using System.Security.Cryptography;
using System.Text;

namespace Autenticacao_no_ASP_.NET_MVC.Utils
{
    public class Hash
    {
        public static string GerarHash(string texto)
        {
            SHA256 sha256 = SHA256Managed.Create(texto);
            byte[] bytes = Encoding.UTF8.GetBytes(texto);
            byte[] hash = sha256.ComputeHash(bytes);
            StringBuilder result = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
            {
                result.Append(hash[i].ToString("X"));
            }

            return result.ToString();
        }
    }
}

sha256 which is being returned null.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • Is FIPS mode active? – Robert Harvey Dec 31 '18 at 19:27
  • Where can I check this? – Andrew Marques Dec 31 '18 at 19:29
  • Good question. Is this ASP.NET Core? – Robert Harvey Dec 31 '18 at 19:29
  • No. It's ASP.NET MVC 4 – Andrew Marques Dec 31 '18 at 19:31
  • What string is in `texto`? – Robert Harvey Dec 31 '18 at 19:31
  • I pass the parameter to the class, like this: `Hash.GerarHash(cadastroUsuarioViewModels.Senha)` – Andrew Marques Dec 31 '18 at 19:33
  • 1
    OK, the string that you pass to Create() can be one of: `SHA256Managed`, `SHA256Cng`, `SHA256CryptoServiceProvider`. See https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha256.create?view=netframework-4.7.2 – Robert Harvey Dec 31 '18 at 19:34
  • 1
    `SHA256Managed.Create(texto)` is wrong. You're telling it the name of the algorithm to use here, but `texto` isn't the name of an algorithm. –  Dec 31 '18 at 19:34
  • Why are you using `SHA256.Create(string)` to create the algorithm instance. Why not just new up an `SHA256Managed` or use `SHA256.Create()`. My guess is that you are specifying the algorithm in the string incorrectly – Flydog57 Dec 31 '18 at 19:35
  • The "text" would be the parameter I get in the Hash class to then generate it. – Andrew Marques Dec 31 '18 at 19:36
  • 1
    Yeah, you might try just removing `texto` from your `Create()` call. – Robert Harvey Dec 31 '18 at 19:36
  • If you really want to be able to pick the algorithm using a string like that, use an `enum` to make it _"pick from list"_ (and then calling `.ToString()` on the enum – Flydog57 Dec 31 '18 at 19:38
  • Read https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sha256.create?view=netframework-4.7.2. SHA256Managed doesn't actually have a `Create()` method; it's getting that from the base class, which is SHA256. The create method overload you're using requires you to specify the algorithm you want. – Robert Harvey Dec 31 '18 at 19:38
  • Got it. Thanks a lot for the help. The problem was this same variable within `SHA256Managed.Create(texto)`. The correct one was just `SHA256Managed.Create()`. Thank you! Happy New Year to all! – Andrew Marques Dec 31 '18 at 19:42
  • It's worth noting that `SHA256Managed.Create()` may not create an `SHA256Managed` at all. You are really calling `SHA256.Create()`, and it creates an algorithm as it sees fit (it may create the `Cng` version or the `CryptoServiceProvider` version. If you really want an `SHA256Managed`, just new it up – Flydog57 Dec 31 '18 at 19:45
  • https://stackoverflow.com/questions/16999361/obtain-sha-256-string-of-a-string/17001289#17001289 – Dmitry Bychenko Dec 31 '18 at 19:47

1 Answers1

3

Preferred way to create SHA256 (use SHA256 to pick implementation):

SHA256 sha256 = SHA256.Create();

Parameter of Create is name of algorithm - you don't really need to pass one unless you use base class to pick which one to use. To fix you code remove or use correct parameter:

SHA256 sha256 = SHA256Managed.Create(); 

or

SHA256 sha256 = SHA256Managed.Create("SHA256");

Note that both calls actually are implemented by base SHA256 class and first just pick "default" implementation.

See SHA256.Create for reference.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Yes, it was happening like this: SHA256Managed.Create (text) and the correct one was just SHA256Managed.Create (). Thank you! Happy New Year to all! – Andrew Marques Dec 31 '18 at 19:43