Now I understand java doesn't have unsigned bytes, but I'm not sure how to solve this if not. I'm trying to implement SHA256 hashing in java, and i'm in the processing of converting the message to 512-bit.
int l = bytes.length; //total amount of bytes in the original message
int k = 0;
while((l+1+k) % 512 != 448) {
k++;
}
//k is the total amount of 0's to be padded
int rest = k % 8; //get the amount of 0's to be added in the byte with the 1
byte tmp =(byte) Math.pow(2, rest);
So the key instruction is the last row, if rest = 7 the resulting int is 128, but the bytes are signed in java and so the byte becomes 0x80 instead of 0xF0. How can I achieve this in Java?
If anyone has a idea on how to implement this part please let me know.
0xF0?restshould be a number from 0 to 7, which are all positive already and fit inside abyte's range. I think you may have another issue.1<<restinstead ofMath.pow(2, rest)(byte)128is still the correct result, even though it looks like -128 in the debugger. It's 0x80 either way.