Wednesday, 15 September 2010

c# - Md5 return the raw bytes instead of the hex -



c# - Md5 return the raw bytes instead of the hex -

i have php code , want create in c#.

my code in php :

$md5raw = md5($str2hash, true);

my code in c# :

static string getmd5hash(md5 md5hash, string input) { byte[] info = md5hash.computehash(encoding.utf8.getbytes(input)); // create new stringbuilder collect bytes // , create string. stringbuilder sbuilder = new stringbuilder(); // loop through each byte of hashed info // , format each 1 hexadecimal string. (int = 0; < data.length; i++) { sbuilder.append(data[i].tostring("x2")); } // homecoming hexadecimal string. homecoming sbuilder.tostring(); }

the result of 2 codes not identical

php:�賞�}��+x�6� c#:0f9de8b39ee7187d92bb2b5817bb36ee

what should alter in c# code?

if want byte array, skip conversion:

static byte[] getmd5hash(md5 md5hash, string input) { homecoming md5hash.computehash(encoding.utf8.getbytes(input)); }

update:

you definitively should alter php function homecoming encoded info , utilize same encoding in .net (c#).

what want string binary data, lose info due reencoding of binary info during transfer.

static string getmd5hash(md5 md5hash, string input) { // bug: code lose info homecoming encoding.ascii.getstring(md5hash.computehash(encoding.utf8.getbytes(input))); }

the way should utilize c# function in question , utilize php function:

$md5raw = bin2hex(md5($str2hash, true));

c# php md5

No comments:

Post a Comment