Converting a md5 hash byte array to a string

87,440

Solution 1

   public static string ToHex(this byte[] bytes, bool upperCase)
    {
        StringBuilder result = new StringBuilder(bytes.Length*2);

        for (int i = 0; i < bytes.Length; i++)
            result.Append(bytes[i].ToString(upperCase ? "X2" : "x2"));

        return result.ToString();
    }

You can then call it as an extension method:

string hexString = byteArray.ToHex(false);

Solution 2

I always found this to be the most convenient:

string hashPassword = BitConverter.ToString(byteHashedPassword).Replace("-","");

For some odd reason BitConverter likes to put dashes between bytes, so the replace just removes them.

Update: If you prefer "lowercase" hex, just do a .ToLower() and boom.

Do note that if you are doing this as a tight loop and many ops this could be expensive since there are at least two implicit string casts and resizes going on.

Solution 3

You can use Convert.ToBase64String and Convert.FromBase64String to easily convert byte arrays into strings.

Solution 4

If you're in the 'Hex preference' camp you can do this. This is basically a minimal version of the answer by Philippe Leybaert.

string.Concat(hash.Select(x => x.ToString("X2")))

B1DB2CC0BAEE67EA47CFAEDBF2D747DF

Solution 5

Well as it is a hash, it has possibly values that cannot be shown in a normal string, so the best bet is to convert it to Base64 encoded string.

string s = Convert.ToBase64String(bytes);

and use

byte[] bytes = Convert.FromBase64(s);

to get the bytes back.

Share:
87,440

Related videos on Youtube

Blankman
Author by

Blankman

... .. . blank

Updated on May 29, 2020

Comments

  • Blankman
    Blankman almost 4 years

    How can I convert the hashed result, which is a byte array, to a string?

    byte[] bytePassword = Encoding.UTF8.GetBytes(password);
    
    using (MD5 md5 = MD5.Create())
    {
        byte[] byteHashedPassword = md5.ComputeHash(bytePassword);
    } 
    

    I need to convert byteHashedPassword to a string.

    • Kerrmiter
      Kerrmiter about 3 years
      MD5 is deprecated. It is inherently broken as it does not reach the standards of collision or preimage resistance. For passwords, it's better to use iterated key-derivation functions such as Argon2 or PDKDF2.
  • Blankman
    Blankman about 14 years
    what is the significance of upper casing?
  • Philippe Leybaert
    Philippe Leybaert about 14 years
    a matter of preference. That's why I added a parameter to my method, so the caller can choose
  • John Kugelman
    John Kugelman about 14 years
    It's the difference between c0de and C0DE, that's all.
  • Eric Petroelje
    Eric Petroelje about 14 years
    No need to reinvent the wheel when you already have Convert.ToBase64String()
  • Blankman
    Blankman about 14 years
    oh ok, I thought there was more too it! how does your answer differ from Convert.ToBase64String(bytes)?
  • Philippe Leybaert
    Philippe Leybaert about 14 years
    A Base64 string is shorter. It uses all letters of the alphabet, digits and a few punctuation characters, so it's not hexadecimal. Base64 uses 4 characters for 3 bytes, while a hex string uses 6 characters for 3 bytes.
  • Philippe Leybaert
    Philippe Leybaert about 14 years
    @Eric: there are situations where Base64 is not a good choice because of the extra punctuation characters that are used (passing it in a URL for example)
  • sonjz
    sonjz almost 11 years
    does the same as @PhilippeLeybaert solution, but in one-line.