Reading in a file and then hashing it using SHA-256 I found a tutorial which showed two separate ways to do it.
When comparing the file that I hashed (Which was a PDF) against both methods, they did not match up. I am following the code properly, not understanding why it isn't matching up.
Here is my result:
Hex format: b050692edb134da209adf76347f6c5e49db8734edeaa44876606ec8e5559ab4e Hex format: b050692edb134da29adf76347f6c5e49db8734edeaa4487666ec8e5559ab4e
It looks like it is lopping off the two zeros in the middle, I just don't understand why
Java Code
import java.io.FileInputStream;
import java.security.MessageDigest;
public class SHAHash{
public static void main(String[] args)throws Exception{
MessageDigest md = MessageDigest.getInstance("SHA-256");
FileInputStream fis = new FileInputStream("myfile");
byte[] dataBytes = new byte[1024];
int nread = 0;
while((nread = fis.read(dataBytes))!= -1){
md.update(dataBytes, 0, nread);
};
byte[] mdbytes = md.digest();
StringBuffer sb1 = new StringBuffer();
for(int i = 0; i < mdbytes.length; i++){
sb1.append(Integer.toString((mdbytes[i] & 0xFF) + 0x100, 16).substring(1));
}
System.out.println("Hex format: " + sb1.toString());
StringBuffer sb2 = new StringBuffer();
for(int i = 0; i < mdbytes.length; i++){
sb2.append(Integer.toHexString(0xFF & mdbytes[i]));
}
System.out.println("Hex format: " + sb2.toString());
}
}