Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In the end I found that this was very well documented in <a href="http://tools.ietf.org/html/rfc4226" rel="nofollow noreferrer">RFC 4226</a> and regarding the integer conversion, this can be done using the bitwise operation <a href="http://tools.ietf.org/html/rfc4226#page-7" rel="nofollow noreferrer">shown on page 7</a>, essentially it is the same as that shown in the answer below.</p> <p>There was <a href="https://stackoverflow.com/questions/4308003/hmac-based-one-time-password-in-c-rfc-4226-hotp">another post on stackoverflow</a> regarding this in a C# context, which may be worth a read if you are in a similar position.</p> <p>In C# I basically, hashed a time identifier (i.e. the current time in seconds divided by 30 - to get a long which is valid for the current 30-second interval). Then hashed this using my secret key as the SALT.</p> <p>And then...</p> <pre><code>// Use a bitwise operation to get a representative binary code from the hash // Refer section 5.4 at http://tools.ietf.org/html/rfc4226#page-7 int offset = hashBytes[19] &amp; 0xf; int binaryCode = (hashBytes[offset] &amp; 0x7f) &lt;&lt; 24 | (hashBytes[offset + 1] &amp; 0xff) &lt;&lt; 16 | (hashBytes[offset + 2] &amp; 0xff) &lt;&lt; 8 | (hashBytes[offset + 3] &amp; 0xff); // Generate the OTP using the binary code. As per RFC 4426 [link above] "Implementations MUST extract a 6-digit code at a minimum // and possibly 7 and 8-digit code" int otp = binaryCode % (int)Math.Pow(10, 6); // where 6 is the password length return otp.ToString().PadLeft(6, '0'); </code></pre> <p>For those of you who didn't know, Google Authenticator is an open source project - you can <a href="http://code.google.com/p/google-authenticator/source/browse/?repo=android" rel="nofollow noreferrer">browse the source code here</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload