Skip to content

Instantly share code, notes, and snippets.

@BastienClement
Created January 22, 2016 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BastienClement/a08a55740639004c9c57 to your computer and use it in GitHub Desktop.
Save BastienClement/a08a55740639004c9c57 to your computer and use it in GitHub Desktop.
Blizzard Authenticator number generation
/// <summary>
/// 10^0..10^8 (NumDigits used in the HOTP)
/// </summary>
private static readonly int[] DigitsPower = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
private static int DynamicTruncate(byte[] hash)
{
int offset = hash[hash.Length - 1] & 15;
return ((hash[offset] & 127) << 24) + ((hash[offset + 1] & 255) << 16) + ((hash[offset + 2] & 255) << 8) +
(hash[offset + 3] & 255);
}
public string GenerateCode()
{
long time = (BlizzTime/30000);
byte[] timeBytes = BitConverter.GetBytes(time);
// Invert the endian-ness, giving us a big endian number
Array.Reverse(timeBytes);
var sha1 = new HMACSHA1(Token); // Secret Token
byte[] h = sha1.ComputeHash(timeBytes);
int code = DynamicTruncate(h)%DigitsPower[8];
return code.ToString().PadLeft(8, '0');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment