Byte to String

S

shapper

Hello,

I am using the following to hash a string:

public static string Hash(string text) {
MD5CryptoServiceProvider crypto = new
MD5CryptoServiceProvider();
byte[] hash = null;
UTF8Encoding encoder = new UTF8Encoding();
hash = crypto.ComputeHash(encoder.GetBytes(text));
return hash.ToString();
} // Hash

I debugged it but I still get a byte and I can't see the hashed
string ...

What am I missing?

Thanks,
Miguel
 
S

Stanimir Stoyanov

Hi Miguel,

byte[] is an array and ToString() will just return the type name. Here are a
few examples:

1) return BitConverter.ToString(hash); // e.g. A1-F3-BB...

2) return BitConverter.ToString(hash).Replace("-", ""); // e.g. A1F3BB...

3) string hashString = string.Empty;
Array.ForEach(hash, delegate(byte b) { hashString +=
b.ToString("X2"); });
return hashString; // same as 2)
 
S

shapper

Hi Miguel,

byte[] is an array and ToString() will just return the type name. Here are a
few examples:

1) return BitConverter.ToString(hash); // e.g. A1-F3-BB...

2) return BitConverter.ToString(hash).Replace("-", ""); // e.g. A1F3BB...

3) string hashString = string.Empty;
    Array.ForEach(hash, delegate(byte b) { hashString +=
b.ToString("X2"); });
    return hashString; // same as 2)
--
Stanimir Stoyanovhttp://stoyanoff.info


I am using the following to hash a string:
   public static string Hash(string text) {
     MD5CryptoServiceProvider crypto = new
MD5CryptoServiceProvider();
     byte[] hash = null;
     UTF8Encoding encoder = new UTF8Encoding();
     hash = crypto.ComputeHash(encoder.GetBytes(text));
     return hash.ToString();
   } // Hash
I debugged it but I still get a byte and I can't see the hashed
string ...
What am I missing?
Thanks,
Miguel


Thank You! It worked fine.
 
G

Göran Andersson

Stanimir said:
Hi Miguel,

byte[] is an array and ToString() will just return the type name. Here
are a few examples:

1) return BitConverter.ToString(hash); // e.g. A1-F3-BB...

2) return BitConverter.ToString(hash).Replace("-", ""); // e.g. A1F3BB...

3) string hashString = string.Empty;
Array.ForEach(hash, delegate(byte b) { hashString +=
b.ToString("X2"); });
return hashString; // same as 2)

Don't use += to concatenate strings in a loop. Use a StringBuilder instead:

StringBuilder builder = new StringBuilder(hash.Length * 2);
foreach (byte b in hash) builder.Append(b.ToString("x2"));
return builder.ToString();

As the size of the final string is specified in the constructor, it will
allocate a string buffer of the correct size, which then is returned as
a string by the ToString method. (To make it ever more efficient, you
could format the hex string yourself and append one character at a time.
That way it wouldn't create any temporary strings at all.)

4) return Convert.ToBase64String(hash); // e.g. dh1e5gxs4hh3j...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top