我在Android上,所以它只是java,我有相同的輸入字符串,但每次獲得不同的值。我錯過了什麼?由於MD5爲相同的輸入返回不同的值
private String getShortenedKey(String key) {
String shortenedKey=null;
MessageDigest md = null;
LogUtils.LOGD(HASH_ALGO, "before key: "+ System.currentTimeMillis());
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
shortenedKey = key;
}
LogUtils.LOGD(HASH_ALGO, "after key: "+ System.currentTimeMillis());
md.update(key.getBytes());
byte[] shortenedBytes = md.digest();
shortenedKey = String.valueOf(shortenedBytes);
return shortenedKey;
}
輸入字符串:
{"config":{"wp":"(1.000000,1.000000,1.000000,1.000000)","v":"8","unit":"auto","ef":true,"ws":1,"tt":0,"cs":1},"items":[{"startTime":1409180400,"id":"[email protected]_1409180400","class":"event","endTime":1409209200,"location":{"lng":151.20785,"lat":-33.85926},"priority":0},{"startTime":1409148000,"id":"[email protected]_1409148000","class":"event","endTime":1409234340,"location":{"lng":151.18089,"lat":-33.89153},"priority":0}]}
更新:這麼多有效的答覆,謝謝。我選擇最容易改變的那個。乾杯。
由於使用Base64編碼,我已經跟蹤'NoSuchAlgorithmException'和使用原始密鑰如果出現這種情況。 – 2014-08-28 13:53:53