SHA512 hexadecimal return

Asked

Viewed 132 times

4

I found an example of encrypting a string with SHA512.

public static string HashedString(string text)
{
    SHA512Managed sha512 = new SHA512Managed();
    byte[] hash = sha512.ComputeHash(Encoding.UTF8.GetBytes(text));
    StringBuilder result = new StringBuilder();

    foreach (byte b in hash)
        result.Append(b);

    return result.ToString();
}

But when I have print on the screen the return of this method I realize that the result is in binary and I want in hexadecimal.

  • And what is your doubt?

  • PQ the method is not as in the example?

  • And why should?

  • Maniero I hope to receive the result as in the example. If you go in any generator of sha512 online, as that for example, the result you will receive is a string like the one in the example. What’s missing or what’s wrong code so I can get an equal result?

2 answers

4


  • Ummm, convert byte per byte to string. It might actually be that, I’m just not very familiar with the string you passed to the Append method()

  • When I saw the result I thought it could be hexa but I wasn’t sure, I thought it could be just a random sequence of numbers and letters generated by the encryption algorithm, until pq had not read about how the hash process works sha512. I’ll edit the question.

-1

Where is

StringBuilder result = new StringBuilder();

foreach (byte b in hash)
    result.Append(b);

return result.ToString();

replace with

return BitConverter.ToString(hash).Replace("-", "");

That way you won’t have n+1 strings in the heap, but only 2.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.