似乎无法弄清楚我错在哪里:

 private static String generateHashFromFile(String filePath) {
  try {
   final int BUFSZ = 32768;
   MessageDigest sha = MessageDigest.getInstance("SHA-256");
   FileInputStream in = new FileInputStream(filePath);
   BufferedInputStream is = new BufferedInputStream(in, BUFSZ);
   byte[] buffer = new byte[BUFSZ];
   int num = -1;
   while((num = is.read(buffer)) != -1) {
    sha.update(buffer, 0, num);
   }
   is.close();
   byte[] hash = sha.digest();
   return byteArrayToHex(hash);
  } catch (NoSuchAlgorithmException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null;
 }

 private static String byteArrayToHex(byte[] barray)
 {
     char[] c = new char[barray.length * 2];
     byte b;
     for (int i = 0; i < barray.length; ++i)
     {
         b = ((byte)(barray[i] >> 4));
         c[i * 2] = (char)(b > 9 ? b + 0x37 : b + 0x30);
         b = ((byte)(barray[i] & 0xF));
         c[i * 2 + 1] = (char)(b > 9 ? b + 0x37 : b + 0x30);
     }
     return new String(c);
 }

我收到的字符串如下:

")469.76F5941+31E25)6,9,C26)978)4*917180A4C(B7C,E,D+6,7133C705167"

显然不是十六进制!

问题:

  1. 哈希生成代码是否正确?
  2. 十六进制编码方法是否正确?
  3. 我是否遗漏了一些有关编码的内容?
有帮助吗?

解决方案

A byte 值是有符号的,并且您正在使用保留符号的右移。这将导致负值 b 计算高阶半字节时。

例如,考虑一下您的代码使用 byte 值-112 (0x90)。右移时,首先提升为 int 值,0xFFFFFF90。然后右移 4 位,保留符号,变为 0xFFFFFFF9。然后将其转换回一个字节,简单地丢弃高位 24 位,并将 0xF9(十进制 -7)分配给 b. b 不大于 9,因此结果字符为 (-7 + 48),或 ')'。

改为这样做:

int hi = (barray[i] & 0xF0) >>> 4, lo = barray[i] & 0xF;

使用一个 byte 因为局部变量在 32 位或 64 位机器上没有任何好处。事实上,演员阵容 byte 是一个浪费的指令。

其他提示

您的哈希生成代码是正确的。转换 byte[] barray 转换为十六进制字符串,您可以简单地执行以下操作:

String c = new String();
for(short i = 0; i < barray.length; i++) {
    c += Integer.toString((barray[i] & 255) + 256, 16).substring(1).toUpperCase();
}

您的哈希生成和十六进制编码代码都可以工作。我会仔细查看您正在阅读的文件的内容。

您还可以像这样编写十六进制编码方法:

public static String byteArrayToHex(byte[] barray) {
 StringBuffer sb = new StringBuffer();
 for (int i = 0; i < barray.length; i++) {
     String hex = Integer.toHexString(0xff & barray[i]);
     if (hex.length() == 1) sb.append('0');
     sb.append(hex);
 }
 return sb.toString();
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top