Menu

#1 Incorrect convert of secret key from hex to byte

open
nobody
None
5
2009-02-18
2009-02-18
Igor Mukhin
No

byte[] secretAsBytesArray = new BigInteger(secret, 16).toByteArray();

is incorrect!!!
Try it with "e36b37caf493436e9ce5601ec8481503"
it will be incorrectly converted, i think because of sign bit flag in integer.

The correct way:

byte[] secretAsBytesArray = hexStringToByteArray(secret);

public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}

After this change the token with the secret i mentioned worked well.

Discussion


Log in to post a comment.

MongoDB Logo MongoDB