【发布时间】:2012-12-10 08:37:02
【问题描述】:
我有 UTF-8 hash_file 的输出,我需要在我的 java 客户端上计算和检查。基于hash_file manual,我正在提取文件的内容并在Java 上创建MD5 哈希十六进制,但我无法使它们匹配。我尝试了关于 [this question] 的建议,但没有成功2。
这是我在 Java 上的做法:
public static String calculateStringHash(String text, String encoding)
throws NoSuchAlgorithmException, UnsupportedEncodingException{
MessageDigest md = MessageDigest.getInstance("MD5");
return getHex(md.digest(text.getBytes(encoding)));
}
我的结果与this page 的结果一致。
例如:
字符串杰克:1200cf8ad328a60559cf5e7c5f46ee6d
来自我的 Java 代码:1200CF8AD328A60559CF5E7C5F46EE6D
但是在尝试文件时它不起作用。以下是文件功能的代码:
public static String calculateHash(File file) throws NoSuchAlgorithmException,
FileNotFoundException, IOException {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(file));
while ((sCurrentLine = br.readLine()) != null) {
sb.append(sCurrentLine);
}
} catch (IOException ex) {
LOG.log(Level.SEVERE, null, ex);
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ex) {
LOG.log(Level.SEVERE, null, ex);
}
}
return calculateStringHash(sb.toString(),"UTF-8");
}
我验证了在 PHP 端使用 hash_file 并且 UTF-8 是加密。有什么想法吗?
【问题讨论】:
-
UTF-8 是encoding,而不是加密。