public static string GenerateSteamGuardCodeForTime(this SteamGuardData steamGuard, long time) { if (steamGuard.SharedSecret == null || steamGuard.SharedSecret.Length == 0) { return(""); } string sharedSecretUnescaped = Regex.Unescape(steamGuard.SharedSecret); byte[] sharedSecretArray = Convert.FromBase64String(sharedSecretUnescaped); byte[] timeArray = new byte[8]; time /= 30L; for (int i = 8; i > 0; i--) { timeArray[i - 1] = (byte)time; time >>= 8; } HMACSHA1 hmacGenerator = new HMACSHA1(); hmacGenerator.Key = sharedSecretArray; byte[] hashedData = hmacGenerator.ComputeHash(timeArray); byte[] codeArray = new byte[5]; try { byte b = (byte)(hashedData[19] & 0xF); int codePoint = (hashedData[b] & 0x7F) << 24 | (hashedData[b + 1] & 0xFF) << 16 | (hashedData[b + 2] & 0xFF) << 8 | (hashedData[b + 3] & 0xFF); for (int i = 0; i < 5; ++i) { codeArray[i] = steamGuardCodeTranslations[codePoint % steamGuardCodeTranslations.Length]; codePoint /= steamGuardCodeTranslations.Length; } } catch (Exception) { return(null); //Change later, catch-alls are bad! } return(Encoding.UTF8.GetString(codeArray)); }
public static string GenerateCode(this SteamGuardData guardData) { return(GenerateSteamGuardCodeForTime(guardData, SteamTime.GetSteamTime())); }