20

I have a string which under all circumstances satisfies ([a-zA-Z0-9])*, and I want to let it run through sha1.

So how do I convert the string (or the char array obtained using ToCharArray()) to a byte array?

All answers I found so far have a big bunch of comments why the conversion from string to byte array is evil, they provide links to character encoding tutorials, and include a bunch of character encodings bloating the code.

Under my circumstances, conversion should be a LINQ oneliner, safe and neat.

I tried:

sha.ComputeHash(validator.ToCharArray().ToArray<byte>())

and I played around as far as my LINQ knowledge goes:

sha.ComputeHash(validator.ToCharArray().ToArray<byte>(c => (byte)c))
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Alexander
  • 19,906
  • 19
  • 75
  • 162
  • http://stackoverflow.com/questions/12527694/c-sharp-convert-char-to-byte-hex-representation should be all you need I'd think... – Chris Mar 21 '14 at 14:27

4 Answers4

23
validator.Select(c => (byte)c).ToArray()

Will also work. The "string" type supports "IEnumerable", so you can use LINQ directly with one.

The "Select" method allows you specify a lambda to customize your output. This replaces what you were trying to do with the "ToArray(c => (byte)c))".

Yves Dubois
  • 933
  • 4
  • 7
19
Encoding.GetEncoding("UTF-8").GetBytes(chararray);
Konstantin
  • 3,254
  • 15
  • 20
  • I'm not sure about this but, two strings with different normalization might encode to the same bytes. This may or may not be desirable. (I think this point is moot within the range specified in the question.) – Jodrell Mar 21 '14 at 14:33
  • @Alexander, with your range of input, this will generate the same hash as `.Select(c => (byte)c).ToArray()` – Jodrell Mar 21 '14 at 15:17
  • 1
    `Encoding.UTF8.GetBytes(chararray);` – Vinigas Feb 23 '21 at 15:17
7

For your purposes encoding is unnecessary, although it may be more convienient.

You could do instead,

sha.ComputeHash(validator.SelectMany(BitConverter.GetBytes).ToArray());
Marcel
  • 15,039
  • 20
  • 92
  • 150
Jodrell
  • 34,946
  • 5
  • 87
  • 124
6

I like the version:

 System.Text.Encoding.UTF8.GetBytes(chararray);

More or less the same as above

Stephan Baltzer
  • 193
  • 1
  • 8