MD5 哈希在 IOS 和 windows 中相同,但在 java 中不同
MD5 hashing is same in IOS and windows but different in java
我在 IOS 和 Windows md5 散列中得到相同的值,但在 java 的情况下,我得到不同的值,
IOS md5 哈希代码
- (NSString*)md5HexDigest:(NSString*)input
{
NSData *data = [input dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5([data bytes], (CC_LONG)[data length], result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
[ret appendFormat:@"%02x",result[i]];
}
return ret;
}
Windows md5 哈希代码
private static string GetMD5(string text)
{
UnicodeEncoding UE = new UnicodeEncoding();
byte[] hashValue;
byte[] message = UE.GetBytes(text);
MD5 hashString = new MD5CryptoServiceProvider();
string hex = "";
hashValue = hashString.ComputeHash(message);
foreach (byte x in hashValue)
{
hex += String.Format("{0:x2}", x);
}
return hex;
}
Java md5 hasing 代码:尝试使用 UTF-8,16,32 ,但未使用 IOS 和 Windows
public String MD5(String md5) {
try {
String dat1 = md5.trim();
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
byte[] array = md.digest(dat1.getBytes("UTF-16"));
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
}
System.out.println("Digest(in hex format):: " + sb.toString());
return sb.toString();
} catch (java.security.NoSuchAlgorithmException e) {
}
catch(UnsupportedEncodingException e)
{
}
return null;
}
感谢
这里简要概述了 getBytes()
returns 与指定字符集的相关内容(所有学分归于@Kayaman)
"123".getBytes("UTF-8") : 31 32 33
"123".getBytes("UTF-16") : FE FF 00 31 00 32 00 33
"123".getBytes("UTF-16LE"): 31 00 32 00 33 00
"123".getBytes("UTF-16BE"): 00 31 00 32 00 33
说明只有在不指定字节序的情况下才添加BOM。那么就看你的架构是用LE
还是BE
了。
我在 IOS 和 Windows md5 散列中得到相同的值,但在 java 的情况下,我得到不同的值,
IOS md5 哈希代码
- (NSString*)md5HexDigest:(NSString*)input
{
NSData *data = [input dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5([data bytes], (CC_LONG)[data length], result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
[ret appendFormat:@"%02x",result[i]];
}
return ret;
}
Windows md5 哈希代码
private static string GetMD5(string text)
{
UnicodeEncoding UE = new UnicodeEncoding();
byte[] hashValue;
byte[] message = UE.GetBytes(text);
MD5 hashString = new MD5CryptoServiceProvider();
string hex = "";
hashValue = hashString.ComputeHash(message);
foreach (byte x in hashValue)
{
hex += String.Format("{0:x2}", x);
}
return hex;
}
Java md5 hasing 代码:尝试使用 UTF-8,16,32 ,但未使用 IOS 和 Windows
public String MD5(String md5) {
try {
String dat1 = md5.trim();
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
byte[] array = md.digest(dat1.getBytes("UTF-16"));
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
}
System.out.println("Digest(in hex format):: " + sb.toString());
return sb.toString();
} catch (java.security.NoSuchAlgorithmException e) {
}
catch(UnsupportedEncodingException e)
{
}
return null;
}
感谢
这里简要概述了 getBytes()
returns 与指定字符集的相关内容(所有学分归于@Kayaman)
"123".getBytes("UTF-8") : 31 32 33
"123".getBytes("UTF-16") : FE FF 00 31 00 32 00 33
"123".getBytes("UTF-16LE"): 31 00 32 00 33 00
"123".getBytes("UTF-16BE"): 00 31 00 32 00 33
说明只有在不指定字节序的情况下才添加BOM。那么就看你的架构是用LE
还是BE
了。