Skip to content

Instantly share code, notes, and snippets.

@AndrewSav
Last active September 4, 2019 01:15
Show Gist options
  • Save AndrewSav/4363d01238213a66f3a7c8f5e8ec8a42 to your computer and use it in GitHub Desktop.
Save AndrewSav/4363d01238213a66f3a7c8f5e8ec8a42 to your computer and use it in GitHub Desktop.
Calculates SteamGuard code from your base64 shared secret
string GetSteamGuardCode(string base64SharedSecret)
{
long time = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds + 10;
byte[] timeHash = BitConverter.GetBytes(time/30).Reverse().ToArray();
HMACSHA1 hmac = new HMACSHA1(Convert.FromBase64String(base64SharedSecret));
hmac.Initialize();
byte[] hash = hmac.ComputeHash(timeHash);
int b = hash[19] & 0xF;
int codePoint = (hash[b] & 0x7F) << 24 | (hash[b + 1] & 0xFF) << 16 | (hash[b + 2] & 0xFF) << 8 | (hash[b + 3] & 0xFF);
string steamChars = "23456789BCDFGHJKMNPQRTVWXY";
return new string(Enumerable.Range(0,5).Select(x => steamChars[codePoint/((int)Math.Pow(steamChars.Length,x)) % steamChars.Length]).ToArray());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment