Why is & 0xff
applied to a byte variable in the reference implementation of the time-based OTP (TOTP, RFC 6238)? In my opinion, this does not change the value since the byte data type has a length of 8 bits.
byte[] hash = hmac_sha(crypto, k, msg);
int offset = hash[hash.length - 1] & 0xf;
int binary =
((hash[offset] & 0x7f) << 24) |
((hash[offset + 1] & 0xff) << 16) |
((hash[offset + 2] & 0xff) << 8) |
(hash[offset + 3] & 0xff);
int otp = binary % DIGITS_POWER[codeDigits];
Besides, what is the reason to apply the XAND
operator to the first element with 0x7f
? It can still result in a number with 10 digits, which is larger than the largest entry in DIGITS_POWER
, which is 100 000 000
.
(https://www.rfc-editor.org/rfc/rfc6238, page 13)