C#中的UTF16LE的MD5(无BOM和0字节结束)

我有以下问题; 我需要创建一个方法,生成一个字符串的MD5哈希。 该字符串例如是“1234567z-äbc”(与变音符号一起使用)。

这个实际的MD5哈希是:935fe44e659beb5a3bb7a4564fba0513

MD5哈希,我需要的是(100%肯定):9e224a41eeefa284df7bb0f26c2913e2

我的文档说,它必须是没有BOM的UTF16LE转换和字符串的0-Byte End。 问题是转换到这个。 我在Javascript中有一个工作示例,但是为了推送字节,我仍然有点缺乏经验。

/* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as defined in RFC 1321. * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet * Distributed under the BSD License * See http://pajhome.org.uk/crypt/md5 for more info. */ /* * Configurable variables. You may need to tweak these to be compatible with * the server-side, but the defaults work in most cases. */ var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */ /* var chrsz = 8; */ /* bits per input character. 8 - ASCII; 16 - Unicode */ var chrsz = 16; /* bits per input character. 8 - ASCII; 16 - Unicode */ /* * These are the functions you'll usually want to call * They take string arguments and return either hex or base-64 encoded strings */ function hex_md5(s) { return binl2hex(core_md5(str2binl(s), s.length * chrsz)); } function b64_md5(s) { return binl2b64(core_md5(str2binl(s), s.length * chrsz)); } function str_md5(s) { return binl2str(core_md5(str2binl(s), s.length * chrsz)); } function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); } function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); } function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); } /* * Perform a simple self-test to see if the VM is working */ function md5_vm_test() { return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72"; } /* * Calculate the MD5 of an array of little-endian words, and a bit length */ function core_md5(x, len) { /* append padding */ x[len >> 5] |= 0x80 <>> 9) << 4) + 14] = len; var a = 1732584193; var b = -271733879; var c = -1732584194; var d = 271733878; for (var i = 0; i  16) bkey = core_md5(bkey, key.length * chrsz); var ipad = Array(16), opad = Array(16); for (var i = 0; i > 16) + (y >> 16) + (lsw >> 16); return (msw << 16) | (lsw & 0xFFFF); } /* * Bitwise rotate a 32-bit number to the left. */ function bit_rol(num, cnt) { return (num <>> (32 - cnt)); } /* * Convert a string to an array of little-endian words * If chrsz is ASCII, characters >255 have their hi-byte silently ignored. */ function str2binl(str) { var bin = Array(); var mask = (1 << chrsz) - 1; for (var i = 0; i > 5] |= (str.charCodeAt(i / chrsz) & mask) << (i % 32); return bin; } /* * Convert an array of little-endian words to a string */ function binl2str(bin) { var str = ""; var mask = (1 << chrsz) - 1; for (var i = 0; i > 5] >>> (i % 32)) & mask); return str; } /* * Convert an array of little-endian words to a hex string. */ function binl2hex(binarray) { var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; var str = ""; for (var i = 0; i > 2] >> ((i % 4) * 8 + 4)) & 0xF) + hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8)) & 0xF); } return str; } /* * Convert an array of little-endian words to a base-64 string */ function binl2b64(binarray) { var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var str = ""; for (var i = 0; i > 2] >> 8 * (i % 4)) & 0xFF) <> 2] >> 8 * ((i + 1) % 4)) & 0xFF) <> 2] >> 8 * ((i + 2) % 4)) & 0xFF); for (var j = 0; j  binarray.length * 32) str += b64pad; else str += tab.charAt((triplet >> 6 * (3 - j)) & 0x3F); } } return str; } 

那么,这在C#中如何运作?

您可以使用UnicodeEncoding类及其GetBytes方法将字符串转换为UTF16-LE格式的字节:

 var bytes = Encoding.Unicode.GetBytes("1234567z-äbc"); 

然后,您可以使用MD5Cng类及其ComputeHash方法来计算字节的MD5哈希值:

 var hash = new MD5Cng().ComputeHash(bytes); 

最后,您可以将哈希转换为字符串,如下所示:

 var result = hash.Aggregate( new StringBuilder(), (sb, x) => sb.Append(x.ToString("x2"))).ToString(); // result == "9e224a41eeefa284df7bb0f26c2913e2"