![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
just getting Vista up and running now for some dev testing, seems great so far. i have an app that uses MD5 hashing on the passwords. when i run the app in Vista, the hashing code below gives a different result to what XP/Server 2003 computes, it is probably because i'm using GetString on binary content but i'm sure i got this code off an MS sample somewhere... public static string EncryptMd5(string text) { UTF8Encoding encoder = new UTF8Encoding(); MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); byte[] hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(text)); return encoder.GetString(hashedBytes); } |
#4
| |||
| |||
|
|
i've done some more testing and found that it wasn't safe to discard anything above ASCII 179. |
#5
| |||
| |||
|
|
Tim_Mac <tim.mackey (AT) community (DOT) nospam> wrote: just getting Vista up and running now for some dev testing, seems great so far. i have an app that uses MD5 hashing on the passwords. when i run the app in Vista, the hashing code below gives a different result to what XP/Server 2003 computes, it is probably because i'm using GetString on binary content but i'm sure i got this code off an MS sample somewhere... public static string EncryptMd5(string text) { UTF8Encoding encoder = new UTF8Encoding(); MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); byte[] hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(text)); return encoder.GetString(hashedBytes); } Well, that code is broken to start with. ComputeHash returns arbitrary binary data, which is unlikely to be a valid UTF-8 encoded string. You should use something like base64 encoding to convert *arbitrary* binary data into a string. See http://www.pobox.com/~skeet/csharp/unicode.html for more information. -- Jon Skeet - <skeet (AT) pobox (DOT) com http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too |
#6
| |||
| |||
|
|
[Broken Code on Vista] public static string EncryptMd5(string text) { UTF8Encoding encoder = new UTF8Encoding(); MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); byte[] hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(text)); return encoder.GetString(hashedBytes); } |
#7
| |||
| |||
|
|
hey guys thanks for the clarification. i'll have to keep the code operational because i can't re-encode the passwords with a correct algorithm, without knowing the passwords. jon i suppose you mean 'arbitrary' in a technical sense that i'm not aware of. |
|
since it's MD5 it always returns an identical hash of a given string. according to the SDK, UTF8Encoding.GetString "Decodes a sequence of bytes into a string", which is what i want, and it has worked correctly for years except for this new platform difference between Vista and previous windows versions. |
|
i have another version of the code which yields Hex digits, and is obviously much safer to use but i'll have to live with the current setup. |
#8
| |||
| |||
|
|
Tim_Mac <tim.mackey (AT) community (DOT) nospam> wrote: hey guys thanks for the clarification. i'll have to keep the code operational because i can't re-encode the passwords with a correct algorithm, without knowing the passwords. jon i suppose you mean 'arbitrary' in a technical sense that i'm not aware of. I mean that it's binary data with no significance as far as the UTF-8 encoding is concerned. It could be *any* binary data. Not every sequence of binary data is a valid UTF-8 string. since it's MD5 it always returns an identical hash of a given string. according to the SDK, UTF8Encoding.GetString "Decodes a sequence of bytes into a string", which is what i want, and it has worked correctly for years except for this new platform difference between Vista and previous windows versions. It didn't work "correctly". It may have done something repeatable when presented with a sequence of bytes which was not a valid UTF-8 encoded string, but I don't believe that behaviour was documented, and I don't think it's unreasonable to change it in Vista. It's like relying on the results of GetHashcode from one framework version (or even one run) to another, or accessing the UI from a different thread: you may get away with it for a while, but that doesn't mean the code is correct, or that you should rely on it working in the future. i have another version of the code which yields Hex digits, and is obviously much safer to use but i'll have to live with the current setup. I would start migrating code away from the flawed behaviour ASAP, if I were you. You may need to support two formats or something like that for a while, but relying on *just* the band-aid could well cause more problems down the line. -- Jon Skeet - <skeet (AT) pobox (DOT) com http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |