开发者

How does the call to the hashing algorithm work, particularly the use of bit shifting 0xff?

The following code snippet, sourced from Core Java Vol 2 (7th Ed), shows how to create an SHA1 and an MD5 fingerprint using Java.

It turns out that the only function that works is when I load the cleartext from a textfile.

How does MessageDigest开发者_JAVA百科Frame.computeDigest() work out the fingerprint, and, particularly the use of the bit shifting pattern (Line 171 - 172)?

public void computeDigest(byte[] b)
{  
  currentAlgorithm.reset();
  currentAlgorithm.update(b);
  byte[] hash = currentAlgorithm.digest();
  String d = "";
  for (int i = 0; i < hash.length; i++)
  {  
     int v = hash[i] & 0xFF;
     if (v < 16) d += "0";
     d += Integer.toString(v, 16).toUpperCase() + " ";
  }
  digest.setText(d);
} 


The method should work fine whatever you give it - if you're getting the wrong results, I suspect you're loading the file incorrectly. Please show that code, and we can help you work out what's going wrong.

In terms of the code, this line:

int v = hash[i] & 0xFF;

is basically used to treat a byte as unsigned. Bytes are signed in Java - an acknowledged design mistake in the language - but we want to print out the hex value as if it were an unsigned integer. The bitwise AND with just the bottom 8 bits effectively converts it to the integer value of the byte treated as unsigned.

(There are better ways to convert a byte array to a hex string, but that's a separate matter.)


It is not bit shifting, it is bit masking. hash[i] is a byte. When it is widened to integer you need to mask off the higher integer bits because of possible sign extension.

byte b = (byte)0xEF;
System.out.println("No masking: " + (int)b);
System.out.println("Masking:    " + (int)(b & 0xFF));


This snipped:

 int v = hash[i] & 0xFF;
 if (v < 16) d += "0";
 d += Integer.toString(v, 16).toUpperCase() + " ";

First you set all but the lowest 8 bits of v to 0 (because 0xFF is 11111111 in binary). Then if the resulting number is only one digit in hex (< 16) you add a leading "0". Finally convert the result to hex and add it to the string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜