I am attempting to wrap my brain around generating a 6 digit/character non case sensitive expiring one-time password.
My source is http://tools.ietf.org/html/rfc4226#sec
You have two issues here:
If you are generating alpha-numeric, you are not conforming to the RFC - at this point, you can simply take any N bytes and turn them to a hex string and get alpha-numeric. Or, convert them to base 36 if you want a-z and 0-9. Section 5.4 of the RFC is giving you the standard HOTP calc for a set Digit
parameter (notice that Digit
is a parameter along with C
, K
, and T
). If you are choosing to ignore this section, then you don't need to convert the code - just use what you want.
Your "result" byte array has the expiration time simply stuffed in the first 8 bytes after hashing. If your truncation to 6-digit alphanumeric does not collect these along with parts of the hash, it may as well not be calculated at all. It is also very easy to "fake" or replay - hash the secret once, then put whatever ticks you want in front of it - not really a one time password. Note that parameter C
in the RFC is meant to fulfill the expiring window and should be added to the input prior to computing the hash code.